diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-24 21:46:24 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-24 21:46:24 +0000 |
commit | 6666ac42a5cd63a8b18ec72f6c1364f31ef53921 (patch) | |
tree | 64110c3cfad98cada86b87d1e6059173ca33dbd7 /coreutils/mv.c | |
parent | 2062fc415524e966862c60a6e35d03285f366cbb (diff) | |
download | busybox-w32-6666ac42a5cd63a8b18ec72f6c1364f31ef53921.tar.gz busybox-w32-6666ac42a5cd63a8b18ec72f6c1364f31ef53921.tar.bz2 busybox-w32-6666ac42a5cd63a8b18ec72f6c1364f31ef53921.zip |
cp,mv: simpler arg[cv] handling -> smallish code savings
Diffstat (limited to 'coreutils/mv.c')
-rw-r--r-- | coreutils/mv.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/coreutils/mv.c b/coreutils/mv.c index 553bb6ecb..1d2977060 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
@@ -47,16 +47,15 @@ int mv_main(int argc, char **argv) | |||
47 | #if ENABLE_FEATURE_MV_LONG_OPTIONS | 47 | #if ENABLE_FEATURE_MV_LONG_OPTIONS |
48 | applet_long_options = mv_longopts; | 48 | applet_long_options = mv_longopts; |
49 | #endif | 49 | #endif |
50 | opt_complementary = "f-i:i-f"; | 50 | // Need at least two arguments |
51 | // -f unsets -i, -i unsets -f | ||
52 | opt_complementary = "-2:f-i:i-f"; | ||
51 | flags = getopt32(argv, "fi"); | 53 | flags = getopt32(argv, "fi"); |
52 | if (optind + 2 > argc) { | 54 | argc -= optind; |
53 | bb_show_usage(); | ||
54 | } | ||
55 | |||
56 | last = argv[argc - 1]; | ||
57 | argv += optind; | 55 | argv += optind; |
56 | last = argv[argc - 1]; | ||
58 | 57 | ||
59 | if (optind + 2 == argc) { | 58 | if (argc == 2) { |
60 | dest_exists = cp_mv_stat(last, &dest_stat); | 59 | dest_exists = cp_mv_stat(last, &dest_stat); |
61 | if (dest_exists < 0) { | 60 | if (dest_exists < 0) { |
62 | return 1; | 61 | return 1; |
@@ -75,11 +74,11 @@ int mv_main(int argc, char **argv) | |||
75 | goto RET_1; | 74 | goto RET_1; |
76 | } | 75 | } |
77 | 76 | ||
78 | DO_MOVE: | 77 | DO_MOVE: |
79 | 78 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) | |
80 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && | 79 | && ((access(dest, W_OK) < 0 && isatty(0)) |
81 | ((access(dest, W_OK) < 0 && isatty(0)) || | 80 | || (flags & OPT_FILEUTILS_INTERACTIVE)) |
82 | (flags & OPT_FILEUTILS_INTERACTIVE))) { | 81 | ) { |
83 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { | 82 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { |
84 | goto RET_1; /* Ouch! fprintf failed! */ | 83 | goto RET_1; /* Ouch! fprintf failed! */ |
85 | } | 84 | } |
@@ -91,8 +90,9 @@ DO_MOVE: | |||
91 | struct stat source_stat; | 90 | struct stat source_stat; |
92 | int source_exists; | 91 | int source_exists; |
93 | 92 | ||
94 | if (errno != EXDEV || | 93 | if (errno != EXDEV |
95 | (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) { | 94 | || (source_exists = cp_mv_stat(*argv, &source_stat)) < 1 |
95 | ) { | ||
96 | bb_perror_msg("cannot rename '%s'", *argv); | 96 | bb_perror_msg("cannot rename '%s'", *argv); |
97 | } else { | 97 | } else { |
98 | if (dest_exists) { | 98 | if (dest_exists) { |
@@ -116,15 +116,16 @@ DO_MOVE: | |||
116 | #if ENABLE_SELINUX | 116 | #if ENABLE_SELINUX |
117 | copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT; | 117 | copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT; |
118 | #endif | 118 | #endif |
119 | if ((copy_file(*argv, dest, copy_flag) >= 0) && | 119 | if ((copy_file(*argv, dest, copy_flag) >= 0) |
120 | (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) { | 120 | && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0) |
121 | ) { | ||
121 | goto RET_0; | 122 | goto RET_0; |
122 | } | 123 | } |
123 | } | 124 | } |
124 | RET_1: | 125 | RET_1: |
125 | status = 1; | 126 | status = 1; |
126 | } | 127 | } |
127 | RET_0: | 128 | RET_0: |
128 | if (dest != last) { | 129 | if (dest != last) { |
129 | free((void *) dest); | 130 | free((void *) dest); |
130 | } | 131 | } |