diff options
| author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-26 15:58:47 +0200 |
|---|---|---|
| committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-26 15:58:47 +0200 |
| commit | 4c46d854690595bb2c5487aedccebab8de8aafda (patch) | |
| tree | d1d43599b4bc99ca05a082e5cd994b955f5fab3b /coreutils | |
| parent | a43e969574fca5ba17bb866c2aa249fe16ac0530 (diff) | |
| download | busybox-w32-4c46d854690595bb2c5487aedccebab8de8aafda.tar.gz busybox-w32-4c46d854690595bb2c5487aedccebab8de8aafda.tar.bz2 busybox-w32-4c46d854690595bb2c5487aedccebab8de8aafda.zip | |
mv: implement -n option
function old new delta
mv_longopts 23 36 +13
mv_main 510 520 +10
packed_usage 27218 27225 +7
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/mv.c | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/coreutils/mv.c b/coreutils/mv.c index 7f49d5bab..245639bd0 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
| @@ -16,15 +16,30 @@ | |||
| 16 | #include "libbb.h" | 16 | #include "libbb.h" |
| 17 | #include "libcoreutils/coreutils.h" | 17 | #include "libcoreutils/coreutils.h" |
| 18 | 18 | ||
| 19 | //usage:#define mv_trivial_usage | ||
| 20 | //usage: "[-fin] SOURCE DEST\n" | ||
| 21 | //usage: "or: mv [-fin] SOURCE... DIRECTORY" | ||
| 22 | //usage:#define mv_full_usage "\n\n" | ||
| 23 | //usage: "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY\n" | ||
| 24 | //usage: "\nOptions:" | ||
| 25 | //usage: "\n -f Don't prompt before overwriting" | ||
| 26 | //usage: "\n -i Interactive, prompt before overwrite" | ||
| 27 | //usage: "\n -n Don't overwrite an existing file" | ||
| 28 | //usage: | ||
| 29 | //usage:#define mv_example_usage | ||
| 30 | //usage: "$ mv /tmp/foo /bin/bar\n" | ||
| 31 | |||
| 19 | #if ENABLE_FEATURE_MV_LONG_OPTIONS | 32 | #if ENABLE_FEATURE_MV_LONG_OPTIONS |
| 20 | static const char mv_longopts[] ALIGN1 = | 33 | static const char mv_longopts[] ALIGN1 = |
| 21 | "interactive\0" No_argument "i" | 34 | "interactive\0" No_argument "i" |
| 22 | "force\0" No_argument "f" | 35 | "force\0" No_argument "f" |
| 36 | "no-clobber\0" No_argument "n" | ||
| 23 | ; | 37 | ; |
| 24 | #endif | 38 | #endif |
| 25 | 39 | ||
| 26 | #define OPT_FILEUTILS_FORCE 1 | 40 | #define OPT_FILEUTILS_FORCE 1 |
| 27 | #define OPT_FILEUTILS_INTERACTIVE 2 | 41 | #define OPT_FILEUTILS_INTERACTIVE 2 |
| 42 | #define OPT_FILEUTILS_NOCLOBBER 4 | ||
| 28 | 43 | ||
| 29 | int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 44 | int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 30 | int mv_main(int argc, char **argv) | 45 | int mv_main(int argc, char **argv) |
| @@ -40,10 +55,11 @@ int mv_main(int argc, char **argv) | |||
| 40 | #if ENABLE_FEATURE_MV_LONG_OPTIONS | 55 | #if ENABLE_FEATURE_MV_LONG_OPTIONS |
| 41 | applet_long_options = mv_longopts; | 56 | applet_long_options = mv_longopts; |
| 42 | #endif | 57 | #endif |
| 43 | // Need at least two arguments | 58 | /* Need at least two arguments. |
| 44 | // -f unsets -i, -i unsets -f | 59 | * If more than one of -f, -i, -n is specified , only the final one |
| 45 | opt_complementary = "-2:f-i:i-f"; | 60 | * takes effect (it unsets previous options). */ |
| 46 | flags = getopt32(argv, "fi"); | 61 | opt_complementary = "-2:f-in:i-fn:n-fi"; |
| 62 | flags = getopt32(argv, "fin"); | ||
| 47 | argc -= optind; | 63 | argc -= optind; |
| 48 | argv += optind; | 64 | argv += optind; |
| 49 | last = argv[argc - 1]; | 65 | last = argv[argc - 1]; |
| @@ -68,18 +84,22 @@ int mv_main(int argc, char **argv) | |||
| 68 | } | 84 | } |
| 69 | 85 | ||
| 70 | DO_MOVE: | 86 | DO_MOVE: |
| 71 | if (dest_exists | 87 | if (dest_exists) { |
| 72 | && !(flags & OPT_FILEUTILS_FORCE) | 88 | if (flags & OPT_FILEUTILS_NOCLOBBER) |
| 73 | && ((access(dest, W_OK) < 0 && isatty(0)) | ||
| 74 | || (flags & OPT_FILEUTILS_INTERACTIVE)) | ||
| 75 | ) { | ||
| 76 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { | ||
| 77 | goto RET_1; /* Ouch! fprintf failed! */ | ||
| 78 | } | ||
| 79 | if (!bb_ask_confirmation()) { | ||
| 80 | goto RET_0; | 89 | goto RET_0; |
| 90 | if (!(flags & OPT_FILEUTILS_FORCE) | ||
| 91 | && ((access(dest, W_OK) < 0 && isatty(0)) | ||
| 92 | || (flags & OPT_FILEUTILS_INTERACTIVE)) | ||
| 93 | ) { | ||
| 94 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { | ||
| 95 | goto RET_1; /* Ouch! fprintf failed! */ | ||
| 96 | } | ||
| 97 | if (!bb_ask_confirmation()) { | ||
| 98 | goto RET_0; | ||
| 99 | } | ||
| 81 | } | 100 | } |
| 82 | } | 101 | } |
| 102 | |||
| 83 | if (rename(*argv, dest) < 0) { | 103 | if (rename(*argv, dest) < 0) { |
| 84 | struct stat source_stat; | 104 | struct stat source_stat; |
| 85 | int source_exists; | 105 | int source_exists; |
