Initial revision
[oweals/busybox.git] / mv.c
1 #include "internal.h"
2 #include <stdio.h>
3 #include <errno.h>
4
5 const char      mv_usage[] = "mv source-file destination-file\n"
6 "\t\tmv source-file [source-file ...] destination-directory\n"
7 "\n"
8 "\tMove the source files to the destination.\n"
9 "\n";
10
11 extern int
12 mv_fn(const struct FileInfo * i)
13 {
14         struct stat             destination_stat;
15         char                    d[1024];
16         struct FileInfo n;
17
18         if ( stat(i->destination, &destination_stat) == 0 ) {
19                 if ( i->stat.st_ino == destination_stat.st_ino
20                  &&  i->stat.st_dev == destination_stat.st_dev )
21                         return 0;       /* Move file to itself. */
22         }
23         if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) {
24                 n = *i;
25                 n.destination = join_paths(d, i->destination, basename(i->source));
26                 i = &n;
27         }
28         if ( rename(i->source, i->destination) == 0 )
29                 return 0;
30         else if ( errno == EXDEV && is_a_directory(i->source) ) {
31                 fprintf(stderr
32                 ,"%s: Can't move directory across filesystems.\n"
33                 ,i->source);
34                 return 1;
35         }
36         else
37                 return cp_fn(i);
38 }