aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-06-20 09:01:58 +0000
committerEric Andersen <andersen@codepoet.org>2003-06-20 09:01:58 +0000
commit8876fb2f59a0b515b3121d5894933eef88ce566a (patch)
treef67de9320202043aca8ded20fb80d668c3b0c2d8 /coreutils
parentdfce3536ace2bcd38bdd3731841998ce344d786e (diff)
downloadbusybox-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')
-rw-r--r--coreutils/cut.c68
-rw-r--r--coreutils/date.c65
-rw-r--r--coreutils/dd.c4
-rw-r--r--coreutils/df.c32
-rw-r--r--coreutils/du.c93
-rw-r--r--coreutils/env.c17
-rw-r--r--coreutils/mkdir.c26
-rw-r--r--coreutils/mv.c36
-rw-r--r--coreutils/rm.c22
-rw-r--r--coreutils/stty.c71
-rw-r--r--coreutils/test.c6
11 files changed, 199 insertions, 241 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index c24cf6611..8ae762fb3 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -22,20 +22,22 @@
22 22
23#include <stdio.h> 23#include <stdio.h>
24#include <stdlib.h> 24#include <stdlib.h>
25#include <unistd.h> /* getopt */ 25#include <getopt.h>
26#include <unistd.h>
26#include <string.h> 27#include <string.h>
27#include <limits.h> 28#include <limits.h>
28#include "busybox.h" 29#include "busybox.h"
29 30
30 31
31/* globals from other files */
32extern int optind;
33extern char *optarg;
34
35
36/* option vars */ 32/* option vars */
37static char part = 0; /* (b)yte, (c)har, (f)ields */ 33static const char optstring[] = "b:c:f:d:sn";
38static unsigned int supress_non_delimited_lines = 0; 34#define OPT_BYTE_FLGS 1
35#define OPT_CHAR_FLGS 2
36#define OPT_FIELDS_FLGS 4
37#define OPT_DELIM_FLGS 8
38#define OPT_SUPRESS_FLGS 16
39static char part; /* (b)yte, (c)har, (f)ields */
40static unsigned int supress_non_delimited_lines;
39static char delim = '\t'; /* delimiter, default is tab */ 41static char delim = '\t'; /* delimiter, default is tab */
40 42
41struct cut_list { 43struct cut_list {
@@ -270,11 +272,11 @@ static void cut_file(FILE *file)
270 while ((line = bb_get_chomped_line_from_file(file)) != NULL) { 272 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
271 273
272 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ 274 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
273 if (part == 'c' || part == 'b') 275 if ((part & (OPT_CHAR_FLGS | OPT_BYTE_FLGS)))
274 cut_line_by_chars(line); 276 cut_line_by_chars(line);
275 277
276 /* cut based on fields */ 278 /* cut based on fields */
277 else if (part == 'f') { 279 else {
278 if (delim == '\n') 280 if (delim == '\n')
279 cut_file_by_lines(line, linenum); 281 cut_file_by_lines(line, linenum);
280 else 282 else
@@ -289,46 +291,32 @@ static void cut_file(FILE *file)
289 291
290extern int cut_main(int argc, char **argv) 292extern int cut_main(int argc, char **argv)
291{ 293{
292 int opt; 294 unsigned long opt;
293 295 char *sopt, *sdopt;
294 while ((opt = getopt(argc, argv, "b:c:d:f:ns")) > 0) { 296
295 switch (opt) { 297 bb_opt_complementaly = "b~bcf:c~bcf:f~bcf";
296 case 'b': 298 opt = bb_getopt_ulflags(argc, argv, optstring, &sopt, &sopt, &sopt, &sdopt);
297 case 'c': 299 part = opt & (OPT_BYTE_FLGS|OPT_CHAR_FLGS|OPT_FIELDS_FLGS);
298 case 'f': 300 if(part == 0)
299 /* make sure they didn't ask for two types of lists */ 301 bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
300 if (part != 0) { 302 if(opt & 0x80000000UL)
301 bb_error_msg_and_die("only one type of list may be specified"); 303 bb_error_msg_and_die("only one type of list may be specified");
302 } 304 parse_lists(sopt);
303 part = (char)opt; 305 if((opt & (OPT_DELIM_FLGS))) {
304 parse_lists(optarg); 306 if (strlen(sdopt) > 1) {
305 break;
306 case 'd':
307 if (strlen(optarg) > 1) {
308 bb_error_msg_and_die("the delimiter must be a single character"); 307 bb_error_msg_and_die("the delimiter must be a single character");
309 } 308 }
310 delim = optarg[0]; 309 delim = sdopt[0];
311 break;
312 case 'n':
313 /* no-op */
314 break;
315 case 's':
316 supress_non_delimited_lines++;
317 break;
318 }
319 }
320
321 if (part == 0) {
322 bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
323 } 310 }
311 supress_non_delimited_lines = opt & OPT_SUPRESS_FLGS;
324 312
325 /* non-field (char or byte) cutting has some special handling */ 313 /* non-field (char or byte) cutting has some special handling */
326 if (part != 'f') { 314 if (part != OPT_FIELDS_FLGS) {
327 if (supress_non_delimited_lines) { 315 if (supress_non_delimited_lines) {
328 bb_error_msg_and_die("suppressing non-delimited lines makes sense" 316 bb_error_msg_and_die("suppressing non-delimited lines makes sense"
329 " only when operating on fields"); 317 " only when operating on fields");
330 } 318 }
331 if (delim != '\t' && part != 'f') { 319 if (delim != '\t') {
332 bb_error_msg_and_die("a delimiter may be specified only when operating on fields"); 320 bb_error_msg_and_die("a delimiter may be specified only when operating on fields");
333 } 321 }
334 } 322 }
diff --git a/coreutils/date.c b/coreutils/date.c
index afbedb90d..6e7aa1f0c 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -120,74 +120,65 @@ int date_main(int argc, char **argv)
120 char *date_str = NULL; 120 char *date_str = NULL;
121 char *date_fmt = NULL; 121 char *date_fmt = NULL;
122 char *t_buff; 122 char *t_buff;
123 int c; 123 int set_time;
124 int set_time = 0; 124 int rfc822;
125 int rfc822 = 0; 125 int utc;
126 int utc = 0;
127 int use_arg = 0; 126 int use_arg = 0;
128 time_t tm; 127 time_t tm;
128 unsigned long opt;
129 struct tm tm_time; 129 struct tm tm_time;
130 130
131#ifdef CONFIG_FEATURE_DATE_ISOFMT 131#ifdef CONFIG_FEATURE_DATE_ISOFMT
132 int ifmt = 0; 132 int ifmt = 0;
133 char *isofmt_arg;
133 134
134# define GETOPT_ISOFMT "I::" 135# define GETOPT_ISOFMT "I::"
135#else 136#else
136# define GETOPT_ISOFMT 137# define GETOPT_ISOFMT
137#endif 138#endif
138 139 bb_opt_complementaly = "d~ds:s~ds";
139 /* Interpret command line args */ 140 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:" GETOPT_ISOFMT,
140 while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT)) != EOF) { 141 &date_str, &date_str
141 switch (c) { 142#ifdef CONFIG_FEATURE_DATE_ISOFMT
142 case 'R': 143 , &isofmt_arg
143 rfc822 = 1; 144#endif
144 break; 145 );
145 case 's': 146 rfc822 = opt & 1;
146 set_time = 1; 147 set_time = opt & 2;
147 if ((date_str != NULL) || ((date_str = optarg) == NULL)) { 148 utc = opt & 4;
148 bb_show_usage(); 149 if(utc) {
149 }
150 break;
151 case 'u':
152 utc = 1;
153 if (putenv("TZ=UTC0") != 0) 150 if (putenv("TZ=UTC0") != 0)
154 bb_error_msg_and_die(bb_msg_memory_exhausted); 151 bb_error_msg_and_die(bb_msg_memory_exhausted);
155 break; 152 }
156 case 'd': 153 use_arg = opt & 8;
157 use_arg = 1; 154 if(opt & 0x80000000UL)
158 if ((date_str != NULL) || ((date_str = optarg) == NULL))
159 bb_show_usage(); 155 bb_show_usage();
160 break;
161#ifdef CONFIG_FEATURE_DATE_ISOFMT 156#ifdef CONFIG_FEATURE_DATE_ISOFMT
162 case 'I': 157 if(opt & 16) {
163 if (!optarg) 158 if (!isofmt_arg)
164 ifmt = 1; 159 ifmt = 1;
165 else { 160 else {
166 int ifmt_len = bb_strlen(optarg); 161 int ifmt_len = bb_strlen(isofmt_arg);
167 162
168 if ((ifmt_len <= 4) 163 if ((ifmt_len <= 4)
169 && (strncmp(optarg, "date", ifmt_len) == 0)) { 164 && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
170 ifmt = 1; 165 ifmt = 1;
171 } else if ((ifmt_len <= 5) 166 } else if ((ifmt_len <= 5)
172 && (strncmp(optarg, "hours", ifmt_len) == 0)) { 167 && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
173 ifmt = 2; 168 ifmt = 2;
174 } else if ((ifmt_len <= 7) 169 } else if ((ifmt_len <= 7)
175 && (strncmp(optarg, "minutes", ifmt_len) == 0)) { 170 && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
176 ifmt = 3; 171 ifmt = 3;
177 } else if ((ifmt_len <= 7) 172 } else if ((ifmt_len <= 7)
178 && (strncmp(optarg, "seconds", ifmt_len) == 0)) { 173 && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
179 ifmt = 4; 174 ifmt = 4;
180 } 175 }
181 } 176 }
182 if (ifmt) { 177 if (!ifmt) {
183 break; /* else bb_show_usage(); */
184 }
185#endif
186 default:
187 bb_show_usage(); 178 bb_show_usage();
188 } 179 }
189 } 180 }
190 181#endif
191 182
192 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) { 183 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
193 date_fmt = &argv[optind][1]; /* Skip over the '+' */ 184 date_fmt = &argv[optind][1]; /* Skip over the '+' */
diff --git a/coreutils/dd.c b/coreutils/dd.c
index 11508614f..cd97b24ee 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -106,9 +106,7 @@ int dd_main(int argc, char **argv)
106 buf = xmalloc(bs); 106 buf = xmalloc(bs);
107 107
108 if (infile != NULL) { 108 if (infile != NULL) {
109 if ((ifd = open(infile, O_RDONLY)) < 0) { 109 ifd = bb_xopen(infile, O_RDONLY);
110 bb_perror_msg_and_die("%s", infile);
111 }
112 } else { 110 } else {
113 ifd = STDIN_FILENO; 111 ifd = STDIN_FILENO;
114 infile = bb_msg_standard_input; 112 infile = bb_msg_standard_input;
diff --git a/coreutils/df.c b/coreutils/df.c
index 708e12cc0..9673633cc 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -55,41 +55,27 @@ extern int df_main(int argc, char **argv)
55 unsigned long df_disp_hr = KILOBYTE; 55 unsigned long df_disp_hr = KILOBYTE;
56#endif 56#endif
57 int status = EXIT_SUCCESS; 57 int status = EXIT_SUCCESS;
58 int opt; 58 unsigned long opt;
59 FILE *mount_table; 59 FILE *mount_table;
60 struct mntent *mount_entry; 60 struct mntent *mount_entry;
61 struct statfs s; 61 struct statfs s;
62 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */ 62 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
63 const char *disp_units_hdr = hdr_1k; 63 const char *disp_units_hdr = hdr_1k;
64 64
65 while ((opt = getopt(argc, argv, "k"
66#ifdef CONFIG_FEATURE_HUMAN_READABLE 65#ifdef CONFIG_FEATURE_HUMAN_READABLE
67 "hm" 66 bb_opt_complementaly = "h-km:k-hm:m-hk";
68#endif 67 opt = bb_getopt_ulflags(argc, argv, "hmk");
69)) > 0) 68 if(opt & 1) {
70 {
71 switch (opt) {
72#ifdef CONFIG_FEATURE_HUMAN_READABLE
73 case 'h':
74 df_disp_hr = 0; 69 df_disp_hr = 0;
75 disp_units_hdr = " Size"; 70 disp_units_hdr = " Size";
76 break; 71 }
77 case 'm': 72 if(opt & 2) {
78 df_disp_hr = MEGABYTE; 73 df_disp_hr = MEGABYTE;
79 disp_units_hdr = "1M-blocks"; 74 disp_units_hdr = "1M-blocks";
80 break;
81#endif
82 case 'k':
83 /* default display is kilobytes */
84#ifdef CONFIG_FEATURE_HUMAN_READABLE
85 df_disp_hr = KILOBYTE;
86 disp_units_hdr = hdr_1k;
87#endif
88 break;
89 default:
90 bb_show_usage();
91 }
92 } 75 }
76#else
77 opt = bb_getopt_ulflags(argc, argv, "k");
78#endif
93 79
94 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n", 80 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
95 "", disp_units_hdr); 81 "", disp_units_hdr);
diff --git a/coreutils/du.c b/coreutils/du.c
index 1c16cfbd4..a9f6c28ba 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -166,8 +166,9 @@ int du_main(int argc, char **argv)
166{ 166{
167 long total; 167 long total;
168 int slink_depth_save; 168 int slink_depth_save;
169 int print_final_total = 0; 169 int print_final_total;
170 int c; 170 char *smax_print_depth;
171 unsigned long opt;
171 172
172#ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K 173#ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
173 if (getenv("POSIXLY_CORRECT")) { /* TODO - a new libbb function? */ 174 if (getenv("POSIXLY_CORRECT")) { /* TODO - a new libbb function? */
@@ -185,57 +186,57 @@ int du_main(int argc, char **argv)
185 * gnu du exits with an error code in this case. We choose to simply 186 * gnu du exits with an error code in this case. We choose to simply
186 * ignore -a. This is consistent with -s being equivalent to -d 0. 187 * ignore -a. This is consistent with -s being equivalent to -d 0.
187 */ 188 */
188
189 while ((c = getopt(argc, argv, "aHkLsx" "d:" "lc"
190#ifdef CONFIG_FEATURE_HUMAN_READABLE
191 "hm"
192#endif
193 )) > 0) {
194 switch (c) {
195 case 'a':
196 print_files = INT_MAX;
197 break;
198 case 'H':
199 slink_depth = 1;
200 break;
201 case 'k':
202#ifdef CONFIG_FEATURE_HUMAN_READABLE 189#ifdef CONFIG_FEATURE_HUMAN_READABLE
190 bb_opt_complementaly = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
191 opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
192 if((opt & (1 << 9))) {
193 /* -h opt */
194 disp_hr = 0;
195 }
196 if((opt & (1 << 10))) {
197 /* -m opt */
198 disp_hr = MEGABYTE;
199 }
200 if((opt & (1 << 2))) {
201 /* -k opt */
203 disp_hr = KILOBYTE; 202 disp_hr = KILOBYTE;
204#elif !defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K 203 }
204#else
205 bb_opt_complementaly = "H-L:L-H:s-d:d-s";
206 opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc", &smax_print_depth);
207#if !defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
208 if((opt & (1 << 2))) {
209 /* -k opt */
205 disp_k = 1; 210 disp_k = 1;
211 }
212#endif
206#endif 213#endif
207 break; 214 if((opt & (1 << 0))) {
208 case 'L': 215 /* -a opt */
216 print_files = INT_MAX;
217 }
218 if((opt & (1 << 1))) {
219 /* -H opt */
220 slink_depth = 1;
221 }
222 if((opt & (1 << 3))) {
223 /* -L opt */
209 slink_depth = INT_MAX; 224 slink_depth = INT_MAX;
210 break; 225 }
211 case 's': 226 if((opt & (1 << 4))) {
227 /* -s opt */
212 max_print_depth = 0; 228 max_print_depth = 0;
213 break;
214 case 'x':
215 one_file_system = 1;
216 break;
217
218 case 'd':
219 max_print_depth = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
220 break;
221 case 'l':
222 count_hardlinks = 1;
223 break;
224 case 'c':
225 print_final_total = 1;
226 break;
227#ifdef CONFIG_FEATURE_HUMAN_READABLE
228 case 'h':
229 disp_hr = 0;
230 break;
231 case 'm':
232 disp_hr = MEGABYTE;
233 break;
234#endif
235 default:
236 bb_show_usage();
237 } 229 }
230 one_file_system = opt & (1 << 5); /* -x opt */
231 if((opt & (1 << 6))) {
232 /* -d opt */
233 max_print_depth = bb_xgetularg10_bnd(smax_print_depth, 0, INT_MAX);
238 } 234 }
235 if((opt & (1 << 7))) {
236 /* -l opt */
237 count_hardlinks = 1;
238 }
239 print_final_total = opt & (1 << 8); /* -c opt */
239 240
240 /* go through remaining args (if any) */ 241 /* go through remaining args (if any) */
241 argv += optind; 242 argv += optind;
@@ -252,7 +253,9 @@ int du_main(int argc, char **argv)
252 total += du(*argv); 253 total += du(*argv);
253 slink_depth = slink_depth_save; 254 slink_depth = slink_depth_save;
254 } while (*++argv); 255 } while (*++argv);
256#ifdef CONFIG_FEATURE_CLEAN_UP
255 reset_ino_dev_hashtable(); 257 reset_ino_dev_hashtable();
258#endif
256 259
257 if (print_final_total) { 260 if (print_final_total) {
258 print(total, "total"); 261 print(total, "total");
diff --git a/coreutils/env.c b/coreutils/env.c
index db13b3aed..eb761e9e9 100644
--- a/coreutils/env.c
+++ b/coreutils/env.c
@@ -44,20 +44,13 @@ extern int env_main(int argc, char** argv)
44{ 44{
45 char **ep, *p; 45 char **ep, *p;
46 char *cleanenv[1] = { NULL }; 46 char *cleanenv[1] = { NULL };
47 int ch; 47 unsigned long opt;
48 48
49 while ((ch = getopt(argc, argv, "iu:")) > 0) { 49 opt = bb_getopt_ulflags(argc, argv, "iu:", &p);
50 switch(ch) { 50 if(opt & 1)
51 case 'i':
52 environ = cleanenv; 51 environ = cleanenv;
53 break; 52 if(opt & 2)
54 case 'u': 53 unsetenv(p);
55 unsetenv(optarg);
56 break;
57 default:
58 bb_show_usage();
59 }
60 }
61 54
62 argv += optind; 55 argv += optind;
63 56
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c
index b018ac181..50364f17f 100644
--- a/coreutils/mkdir.c
+++ b/coreutils/mkdir.c
@@ -31,27 +31,33 @@
31 31
32#include <stdlib.h> 32#include <stdlib.h>
33#include <unistd.h> 33#include <unistd.h>
34#include <getopt.h>
34#include "busybox.h" 35#include "busybox.h"
35 36
37static const struct option mkdir_long_options[] = {
38 { "mode", 1, NULL, 'm' },
39 { "parents", 0, NULL, 'p' },
40 { 0, 0, 0, 0 }
41};
42
36extern int mkdir_main (int argc, char **argv) 43extern int mkdir_main (int argc, char **argv)
37{ 44{
38 mode_t mode = (mode_t)(-1); 45 mode_t mode = (mode_t)(-1);
39 int status = EXIT_SUCCESS; 46 int status = EXIT_SUCCESS;
40 int flags = 0; 47 int flags = 0;
41 int opt; 48 unsigned long opt;
49 char *smode;
42 50
43 while ((opt = getopt (argc, argv, "m:p")) > 0) { 51 bb_applet_long_options = mkdir_long_options;
44 if (opt == 'm') { 52 opt = bb_getopt_ulflags(argc, argv, "m:p", &smode);
53 if(opt & 1) {
45 mode = 0777; 54 mode = 0777;
46 if (!bb_parse_mode (optarg, &mode)) { 55 if (!bb_parse_mode (smode, &mode)) {
47 bb_error_msg_and_die ("invalid mode `%s'", optarg); 56 bb_error_msg_and_die ("invalid mode `%s'", smode);
48 }
49 } else if (opt == 'p') {
50 flags |= FILEUTILS_RECUR;
51 } else {
52 bb_show_usage();
53 } 57 }
54 } 58 }
59 if(opt & 2)
60 flags |= FILEUTILS_RECUR;
55 61
56 if (optind == argc) { 62 if (optind == argc) {
57 bb_show_usage(); 63 bb_show_usage();
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
37static const char *fmt = "cannot overwrite %sdirectory with %sdirectory"; 38static const struct option mv_long_options[] = {
39 { "interactive", 0, NULL, 'i' },
40 { "force", 0, NULL, 'f' },
41 { 0, 0, 0, 0 }
42};
43
44static const char mv_getopt_short_option[] = "fi";
45#define OPT_FILEUTILS_FORCE 1
46#define OPT_FILEUTILS_INTERACTIVE 2
47
48static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
38 49
39extern int mv_main(int argc, char **argv) 50extern 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 }
diff --git a/coreutils/rm.c b/coreutils/rm.c
index 5489350e5..39609e7b8 100644
--- a/coreutils/rm.c
+++ b/coreutils/rm.c
@@ -36,22 +36,16 @@ extern int rm_main(int argc, char **argv)
36{ 36{
37 int status = 0; 37 int status = 0;
38 int flags = 0; 38 int flags = 0;
39 int opt; 39 unsigned long opt;
40 40
41 while ((opt = getopt(argc, argv, "fiRr")) > 0) { 41 bb_opt_complementaly = "f-i:i-f";
42 if ((opt == 'r') || (opt == 'R')) { 42 opt = bb_getopt_ulflags(argc, argv, "fiRr");
43 flags |= FILEUTILS_RECUR; 43 if(opt & 1)
44 } else {
45 flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
46 if (opt == 'i') {
47 flags |= FILEUTILS_INTERACTIVE;
48 } else if (opt == 'f') {
49 flags |= FILEUTILS_FORCE; 44 flags |= FILEUTILS_FORCE;
50 } else { 45 if(opt & 2)
51 bb_show_usage(); 46 flags |= FILEUTILS_INTERACTIVE;
52 } 47 if(opt & 12)
53 } 48 flags |= FILEUTILS_RECUR;
54 }
55 49
56 if (*(argv += optind) != NULL) { 50 if (*(argv += optind) != NULL) {
57 do { 51 do {
diff --git a/coreutils/stty.c b/coreutils/stty.c
index a3a98d9ef..bd3a36911 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -414,22 +414,25 @@ static int set_mode(const struct mode_info *info,
414 int reversed, struct termios *mode); 414 int reversed, struct termios *mode);
415static speed_t string_to_baud(const char *arg); 415static speed_t string_to_baud(const char *arg);
416static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode); 416static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode);
417static void display_all(struct termios *mode, int fd, 417static void display_all(struct termios *mode, int fd);
418 const char *device_name); 418static void display_changed(struct termios *mode, int fd);
419static void display_changed(struct termios *mode, int fd, 419static void display_recoverable(struct termios *mode, int fd);
420 const char *device_name);
421static void display_recoverable(struct termios *mode, int fd,
422 const char *device_name);
423static void display_speed(struct termios *mode, int fancy); 420static void display_speed(struct termios *mode, int fancy);
424static void display_window_size(int fancy, int fd, 421static void display_window_size(int fancy, int fd);
425 const char *device_name);
426static void sane_mode(struct termios *mode); 422static void sane_mode(struct termios *mode);
427static void set_control_char(const struct control_info *info, 423static void set_control_char(const struct control_info *info,
428 const char *arg, struct termios *mode); 424 const char *arg, struct termios *mode);
429static void set_speed(enum speed_setting type, 425static void set_speed(enum speed_setting type,
430 const char *arg, struct termios *mode); 426 const char *arg, struct termios *mode);
431static void set_window_size(int rows, int cols, int fd, 427static void set_window_size(int rows, int cols, int fd);
432 const char *device_name); 428
429static const char *device_name;
430
431static __attribute__ ((noreturn)) void perror_on_device(const char *fmt)
432{
433 bb_perror_msg_and_die(fmt, device_name);
434}
435
433 436
434/* The width of the screen, for output wrapping. */ 437/* The width of the screen, for output wrapping. */
435static int max_col; 438static int max_col;
@@ -477,7 +480,7 @@ extern int main(int argc, char **argv)
477#endif 480#endif
478{ 481{
479 struct termios mode; 482 struct termios mode;
480 void (*output_func)(struct termios *, int, const char *); 483 void (*output_func)(struct termios *, int);
481 int optc; 484 int optc;
482 int require_set_attr; 485 int require_set_attr;
483 int speed_was_set; 486 int speed_was_set;
@@ -487,7 +490,7 @@ extern int main(int argc, char **argv)
487 int noargs = 1; 490 int noargs = 1;
488 char * file_name = NULL; 491 char * file_name = NULL;
489 int fd; 492 int fd;
490 const char *device_name; 493
491 494
492 output_func = display_changed; 495 output_func = display_changed;
493 verbose_output = 0; 496 verbose_output = 0;
@@ -543,13 +546,10 @@ extern int main(int argc, char **argv)
543 int fdflags; 546 int fdflags;
544 547
545 device_name = file_name; 548 device_name = file_name;
546 fd = open(device_name, O_RDONLY | O_NONBLOCK); 549 fd = bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
547 if (fd < 0)
548 bb_perror_msg_and_die("%s", device_name);
549 if ((fdflags = fcntl(fd, F_GETFL)) == -1 550 if ((fdflags = fcntl(fd, F_GETFL)) == -1
550 || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) 551 || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
551 bb_perror_msg_and_die("%s: couldn't reset non-blocking mode", 552 perror_on_device("%s: couldn't reset non-blocking mode");
552 device_name);
553 } else { 553 } else {
554 fd = 0; 554 fd = 0;
555 device_name = bb_msg_standard_input; 555 device_name = bb_msg_standard_input;
@@ -559,12 +559,12 @@ extern int main(int argc, char **argv)
559 spurious difference in an uninitialized portion of the structure. */ 559 spurious difference in an uninitialized portion of the structure. */
560 memset(&mode, 0, sizeof(mode)); 560 memset(&mode, 0, sizeof(mode));
561 if (tcgetattr(fd, &mode)) 561 if (tcgetattr(fd, &mode))
562 bb_perror_msg_and_die("%s", device_name); 562 perror_on_device("%s");
563 563
564 if (verbose_output | recoverable_output | noargs) { 564 if (verbose_output | recoverable_output | noargs) {
565 max_col = screen_columns(); 565 max_col = screen_columns();
566 current_col = 0; 566 current_col = 0;
567 output_func(&mode, fd, device_name); 567 output_func(&mode, fd);
568 return EXIT_SUCCESS; 568 return EXIT_SUCCESS;
569 } 569 }
570 570
@@ -644,18 +644,18 @@ extern int main(int argc, char **argv)
644 bb_error_msg_and_die("missing argument to `%s'", argv[k]); 644 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
645 ++k; 645 ++k;
646 set_window_size((int) bb_xparse_number(argv[k], stty_suffixes), 646 set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
647 -1, fd, device_name); 647 -1, fd);
648 } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) { 648 } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
649 if (k == argc - 1) 649 if (k == argc - 1)
650 bb_error_msg_and_die("missing argument to `%s'", argv[k]); 650 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
651 ++k; 651 ++k;
652 set_window_size(-1, 652 set_window_size(-1,
653 (int) bb_xparse_number(argv[k], stty_suffixes), 653 (int) bb_xparse_number(argv[k], stty_suffixes),
654 fd, device_name); 654 fd);
655 } else if (STREQ(argv[k], "size")) { 655 } else if (STREQ(argv[k], "size")) {
656 max_col = screen_columns(); 656 max_col = screen_columns();
657 current_col = 0; 657 current_col = 0;
658 display_window_size(0, fd, device_name); 658 display_window_size(0, fd);
659 } 659 }
660#endif 660#endif
661#ifdef HAVE_C_LINE 661#ifdef HAVE_C_LINE
@@ -685,7 +685,7 @@ extern int main(int argc, char **argv)
685 struct termios new_mode; 685 struct termios new_mode;
686 686
687 if (tcsetattr(fd, TCSADRAIN, &mode)) 687 if (tcsetattr(fd, TCSADRAIN, &mode))
688 bb_perror_msg_and_die("%s", device_name); 688 perror_on_device("%s");
689 689
690 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if 690 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
691 it performs *any* of the requested operations. This means it 691 it performs *any* of the requested operations. This means it
@@ -698,7 +698,7 @@ extern int main(int argc, char **argv)
698 spurious difference in an uninitialized portion of the structure. */ 698 spurious difference in an uninitialized portion of the structure. */
699 memset(&new_mode, 0, sizeof(new_mode)); 699 memset(&new_mode, 0, sizeof(new_mode));
700 if (tcgetattr(fd, &new_mode)) 700 if (tcgetattr(fd, &new_mode))
701 bb_perror_msg_and_die("%s", device_name); 701 perror_on_device("%s");
702 702
703 /* Normally, one shouldn't use memcmp to compare structures that 703 /* Normally, one shouldn't use memcmp to compare structures that
704 may have `holes' containing uninitialized data, but we have been 704 may have `holes' containing uninitialized data, but we have been
@@ -721,8 +721,7 @@ extern int main(int argc, char **argv)
721 new_mode.c_cflag &= (~CIBAUD); 721 new_mode.c_cflag &= (~CIBAUD);
722 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0) 722 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
723#endif 723#endif
724 bb_error_msg_and_die ("%s: unable to perform all requested operations", 724 perror_on_device ("%s: unable to perform all requested operations");
725 device_name);
726 } 725 }
727 } 726 }
728 727
@@ -948,13 +947,13 @@ static int get_win_size(int fd, struct winsize *win)
948} 947}
949 948
950static void 949static void
951set_window_size(int rows, int cols, int fd, const char *device_name) 950set_window_size(int rows, int cols, int fd)
952{ 951{
953 struct winsize win; 952 struct winsize win;
954 953
955 if (get_win_size(fd, &win)) { 954 if (get_win_size(fd, &win)) {
956 if (errno != EINVAL) 955 if (errno != EINVAL)
957 bb_perror_msg_and_die("%s", device_name); 956 perror_on_device("%s");
958 memset(&win, 0, sizeof(win)); 957 memset(&win, 0, sizeof(win));
959 } 958 }
960 959
@@ -980,23 +979,23 @@ set_window_size(int rows, int cols, int fd, const char *device_name)
980 979
981 if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0) 980 if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
982 || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) { 981 || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
983 bb_perror_msg_and_die("%s", device_name); 982 perror_on_device("%s");
984 return; 983 return;
985 } 984 }
986# endif 985# endif
987 986
988 if (ioctl(fd, TIOCSWINSZ, (char *) &win)) 987 if (ioctl(fd, TIOCSWINSZ, (char *) &win))
989 bb_perror_msg_and_die("%s", device_name); 988 perror_on_device("%s");
990} 989}
991 990
992static void display_window_size(int fancy, int fd, const char *device_name) 991static void display_window_size(int fancy, int fd)
993{ 992{
994 const char *fmt_str = "%s" "\0" "%s: no size information for this device"; 993 const char *fmt_str = "%s" "\0" "%s: no size information for this device";
995 struct winsize win; 994 struct winsize win;
996 995
997 if (get_win_size(fd, &win)) { 996 if (get_win_size(fd, &win)) {
998 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) { 997 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
999 bb_perror_msg_and_die(fmt_str, device_name); 998 perror_on_device(fmt_str);
1000 } 999 }
1001 } else { 1000 } else {
1002 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", 1001 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
@@ -1047,7 +1046,7 @@ static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode)
1047 return NULL; 1046 return NULL;
1048} 1047}
1049 1048
1050static void display_changed(struct termios *mode, int fd, const char *device_name) 1049static void display_changed(struct termios *mode, int fd)
1051{ 1050{
1052 int i; 1051 int i;
1053 int empty_line; 1052 int empty_line;
@@ -1122,7 +1121,7 @@ static void display_changed(struct termios *mode, int fd, const char *device_nam
1122} 1121}
1123 1122
1124static void 1123static void
1125display_all(struct termios *mode, int fd, const char *device_name) 1124display_all(struct termios *mode, int fd)
1126{ 1125{
1127 int i; 1126 int i;
1128 tcflag_t *bitsp; 1127 tcflag_t *bitsp;
@@ -1131,7 +1130,7 @@ display_all(struct termios *mode, int fd, const char *device_name)
1131 1130
1132 display_speed(mode, 1); 1131 display_speed(mode, 1);
1133#ifdef TIOCGWINSZ 1132#ifdef TIOCGWINSZ
1134 display_window_size(1, fd, device_name); 1133 display_window_size(1, fd);
1135#endif 1134#endif
1136#ifdef HAVE_C_LINE 1135#ifdef HAVE_C_LINE
1137 wrapf("line = %d;", mode->c_line); 1136 wrapf("line = %d;", mode->c_line);
@@ -1202,7 +1201,7 @@ static void display_speed(struct termios *mode, int fancy)
1202 current_col = 0; 1201 current_col = 0;
1203} 1202}
1204 1203
1205static void display_recoverable(struct termios *mode, int fd, const char *device_name) 1204static void display_recoverable(struct termios *mode, int fd)
1206{ 1205{
1207 int i; 1206 int i;
1208 1207
diff --git a/coreutils/test.c b/coreutils/test.c
index 0bce66e6f..31ac87f34 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -452,10 +452,8 @@ static int getn(const char *s)
452 if (errno != 0) 452 if (errno != 0)
453 bb_error_msg_and_die("%s: out of range", s); 453 bb_error_msg_and_die("%s: out of range", s);
454 454
455 while (isspace(*p)) 455 /* p = bb_skip_whitespace(p); avoid const warning */
456 p++; 456 if (*(bb_skip_whitespace(p)))
457
458 if (*p)
459 bb_error_msg_and_die("%s: bad number", s); 457 bb_error_msg_and_die("%s: bad number", s);
460 458
461 return (int) r; 459 return (int) r;