diff options
author | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 1999-10-18 19:02:32 +0000 |
---|---|---|
committer | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 1999-10-18 19:02:32 +0000 |
commit | b109c99699c7fc68924354ffa937b111d5896d2d (patch) | |
tree | cdba32234f059656b0279b324ae28c742692cd0c /mv.c | |
parent | c45e09cde722eae8210c601491285aabd26307c3 (diff) | |
download | busybox-w32-b109c99699c7fc68924354ffa937b111d5896d2d.tar.gz busybox-w32-b109c99699c7fc68924354ffa937b111d5896d2d.tar.bz2 busybox-w32-b109c99699c7fc68924354ffa937b111d5896d2d.zip |
More fixes
git-svn-id: svn://busybox.net/trunk/busybox@30 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'mv.c')
-rw-r--r-- | mv.c | 76 |
1 files changed, 36 insertions, 40 deletions
@@ -21,63 +21,59 @@ | |||
21 | 21 | ||
22 | #include "internal.h" | 22 | #include "internal.h" |
23 | #include <stdio.h> | 23 | #include <stdio.h> |
24 | #include <sys/types.h> | 24 | #include <time.h> |
25 | #include <sys/stat.h> | ||
26 | #include <fcntl.h> | ||
27 | #include <utime.h> | 25 | #include <utime.h> |
28 | #include <errno.h> | 26 | #include <dirent.h> |
29 | 27 | ||
30 | const char mv_usage[] = "source-file [source-file ...] destination-file\n" | ||
31 | "\n" "\tMove the source files to the destination.\n" "\n"; | ||
32 | 28 | ||
29 | static const char mv_usage[] = "mv SOURCE DEST\n" | ||
30 | " or: mv SOURCE... DIRECTORY\n" | ||
31 | "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; | ||
33 | 32 | ||
34 | 33 | ||
35 | extern int mv_main (int argc, char **argv) | 34 | static const char *srcName; |
35 | static const char *destName; | ||
36 | static const char *skipName; | ||
37 | static int dirFlag = FALSE; | ||
38 | |||
39 | |||
40 | extern int mv_main(int argc, char **argv) | ||
36 | { | 41 | { |
37 | const char *srcName; | 42 | char newdestName[NAME_MAX]; |
38 | const char *destName; | ||
39 | const char *lastArg; | ||
40 | int dirFlag; | ||
41 | 43 | ||
42 | if (argc < 3) { | 44 | if (argc < 3) { |
43 | fprintf (stderr, "Usage: %s %s", *argv, mv_usage); | 45 | fprintf(stderr, "Usage: %s", mv_usage); |
44 | exit (FALSE); | 46 | exit (FALSE); |
45 | } | 47 | } |
46 | lastArg = argv[argc - 1]; | 48 | argc--; |
49 | argv++; | ||
47 | 50 | ||
48 | dirFlag = isDirectory (lastArg); | 51 | destName = argv[argc - 1]; |
52 | dirFlag = isDirectory(destName); | ||
49 | 53 | ||
50 | if ((argc > 3) && !dirFlag) { | 54 | if ((argc > 3) && dirFlag==FALSE) { |
51 | fprintf (stderr, "%s: not a directory\n", lastArg); | 55 | fprintf(stderr, "%s: not a directory\n", destName); |
52 | exit (FALSE); | 56 | exit (FALSE); |
53 | } | 57 | } |
54 | 58 | ||
55 | while (argc-- > 2) { | 59 | while (argc-- > 1) { |
56 | srcName = *(++argv); | 60 | srcName = *(argv++); |
57 | 61 | skipName = strrchr(srcName, '/'); | |
58 | if (access (srcName, 0) < 0) { | 62 | if (skipName) |
59 | perror (srcName); | 63 | skipName++; |
60 | continue; | 64 | strcpy(newdestName, destName); |
65 | if (dirFlag==TRUE) { | ||
66 | strcat(newdestName, "/"); | ||
67 | if ( skipName != NULL) | ||
68 | strcat(newdestName, strstr(srcName, skipName)); | ||
61 | } | 69 | } |
62 | 70 | if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) { | |
63 | destName = lastArg; | 71 | exit( FALSE); |
64 | |||
65 | if (dirFlag==TRUE) | ||
66 | destName = buildName (destName, srcName); | ||
67 | |||
68 | if (rename (srcName, destName) >= 0) | ||
69 | continue; | ||
70 | |||
71 | if (errno != EXDEV) { | ||
72 | perror (destName); | ||
73 | continue; | ||
74 | } | 72 | } |
75 | 73 | if (unlink (srcName) < 0) { | |
76 | if (!copyFile (srcName, destName, TRUE, FALSE)) | ||
77 | continue; | ||
78 | |||
79 | if (unlink (srcName) < 0) | ||
80 | perror (srcName); | 74 | perror (srcName); |
75 | exit( FALSE); | ||
76 | } | ||
81 | } | 77 | } |
82 | exit (TRUE); | 78 | exit( TRUE); |
83 | } | 79 | } |