diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-06-20 09:01:58 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-06-20 09:01:58 +0000 |
commit | 8876fb2f59a0b515b3121d5894933eef88ce566a (patch) | |
tree | f67de9320202043aca8ded20fb80d668c3b0c2d8 /coreutils/mv.c | |
parent | dfce3536ace2bcd38bdd3731841998ce344d786e (diff) | |
download | busybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.tar.gz busybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.tar.bz2 busybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.zip |
last_patch89 from vodz:
Manuel,
I rewrite bb_getopt_ulflags() function for more universal usage.
My version support now:
- options with arguments (optional arg as GNU extension also)
- complementaly and/or incomplementaly and/or incongruously and/or list
options
- long_opt (all applets may have long option, add supporting is trivial)
This realisation full compatibile from your version.
Code size grow 480 bytes, but only coreutils/* over compensate this size
after using new function. Last patch reduced over 800 bytes and not full
applied to all. "mkdir" and "mv" applets have long_opt now for demonstrate
trivial addition support long_opt with usage new bb_getopt_ulflags().
Complementaly and/or incomplementaly and/or incongruously and/or list options
logic is not trivial, but new "cut" and "grep" applets using this logic
for examples with full demostrating. New "grep" applet reduced over 300
bytes.
Mark,
Also. I removed bug from "grep" applet.
$ echo a b | busybox grep -e a b
a b
a b
But right is printing one only.
--w
vodz
Diffstat (limited to 'coreutils/mv.c')
-rw-r--r-- | coreutils/mv.c | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/coreutils/mv.c b/coreutils/mv.c index ae0ee92e4..55da2cc68 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
@@ -31,10 +31,21 @@ | |||
31 | #include <dirent.h> | 31 | #include <dirent.h> |
32 | #include <errno.h> | 32 | #include <errno.h> |
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <getopt.h> | ||
34 | #include "busybox.h" | 35 | #include "busybox.h" |
35 | #include "libcoreutils/coreutils.h" | 36 | #include "libcoreutils/coreutils.h" |
36 | 37 | ||
37 | static const char *fmt = "cannot overwrite %sdirectory with %sdirectory"; | 38 | static const struct option mv_long_options[] = { |
39 | { "interactive", 0, NULL, 'i' }, | ||
40 | { "force", 0, NULL, 'f' }, | ||
41 | { 0, 0, 0, 0 } | ||
42 | }; | ||
43 | |||
44 | static const char mv_getopt_short_option[] = "fi"; | ||
45 | #define OPT_FILEUTILS_FORCE 1 | ||
46 | #define OPT_FILEUTILS_INTERACTIVE 2 | ||
47 | |||
48 | static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory"; | ||
38 | 49 | ||
39 | extern int mv_main(int argc, char **argv) | 50 | extern int mv_main(int argc, char **argv) |
40 | { | 51 | { |
@@ -44,20 +55,12 @@ extern int mv_main(int argc, char **argv) | |||
44 | const char *dest; | 55 | const char *dest; |
45 | int dest_exists; | 56 | int dest_exists; |
46 | int source_exists; | 57 | int source_exists; |
47 | int opt; | 58 | unsigned long flags; |
48 | int flags = 0; | ||
49 | int status = 0; | 59 | int status = 0; |
50 | 60 | ||
51 | while ((opt = getopt(argc, argv, "fi")) > 0) { | 61 | bb_applet_long_options = mv_long_options; |
52 | flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE); | 62 | bb_opt_complementaly = "f-i:i-f"; |
53 | if (opt == 'i') { | 63 | flags = bb_getopt_ulflags(argc, argv, mv_getopt_short_option); |
54 | flags |= FILEUTILS_INTERACTIVE; | ||
55 | } else if (opt == 'f') { | ||
56 | flags |= FILEUTILS_FORCE; | ||
57 | } else { | ||
58 | bb_show_usage(); | ||
59 | } | ||
60 | } | ||
61 | 64 | ||
62 | if (optind + 2 > argc) | 65 | if (optind + 2 > argc) |
63 | bb_show_usage(); | 66 | bb_show_usage(); |
@@ -77,8 +80,7 @@ extern int mv_main(int argc, char **argv) | |||
77 | } | 80 | } |
78 | 81 | ||
79 | do { | 82 | do { |
80 | dest = concat_path_file(last, | 83 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
81 | bb_get_last_path_component(*argv)); | ||
82 | 84 | ||
83 | if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) { | 85 | if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) { |
84 | goto RET_1; | 86 | goto RET_1; |
@@ -86,9 +88,9 @@ extern int mv_main(int argc, char **argv) | |||
86 | 88 | ||
87 | DO_MOVE: | 89 | DO_MOVE: |
88 | 90 | ||
89 | if (dest_exists && !(flags & FILEUTILS_FORCE) && | 91 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && |
90 | ((access(dest, W_OK) < 0 && isatty(0)) || | 92 | ((access(dest, W_OK) < 0 && isatty(0)) || |
91 | (flags & FILEUTILS_INTERACTIVE))) { | 93 | (flags & OPT_FILEUTILS_INTERACTIVE))) { |
92 | if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) { | 94 | if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) { |
93 | goto RET_1; /* Ouch! fprintf failed! */ | 95 | goto RET_1; /* Ouch! fprintf failed! */ |
94 | } | 96 | } |