diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-01 18:18:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-01 18:18:04 +0000 |
commit | b2abef3e54de606ced5082d82e381dbafef1bf72 (patch) | |
tree | 9082c2b4d0091f108432b2a41b7ee33152c9240f | |
parent | e27f15615f93065265209e26ff07cf9b4ae8658c (diff) | |
download | busybox-w32-b2abef3e54de606ced5082d82e381dbafef1bf72.tar.gz busybox-w32-b2abef3e54de606ced5082d82e381dbafef1bf72.tar.bz2 busybox-w32-b2abef3e54de606ced5082d82e381dbafef1bf72.zip |
stty: fix width of a field for ppc32
sort: fix -u to match coreutils 6.3
msh: compile fix (my fault)
-rw-r--r-- | coreutils/sort.c | 216 | ||||
-rw-r--r-- | coreutils/stty.c | 23 | ||||
-rw-r--r-- | shell/msh.c | 7 |
3 files changed, 148 insertions, 98 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index f2152c0a0..ab59355a9 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
@@ -14,38 +14,44 @@ | |||
14 | 14 | ||
15 | #include "busybox.h" | 15 | #include "busybox.h" |
16 | 16 | ||
17 | static int global_flags; | ||
18 | |||
19 | /* | 17 | /* |
20 | sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...] | 18 | sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...] |
21 | sort -c [-bdfinru][-t char][-k keydef][file] | 19 | sort -c [-bdfinru][-t char][-k keydef][file] |
22 | */ | 20 | */ |
23 | 21 | ||
24 | /* These are sort types */ | 22 | /* These are sort types */ |
25 | #define FLAG_n 1 /* Numeric sort */ | 23 | static const char OPT_STR[] = "ngMucszbrdfimS:T:o:k:t:"; |
26 | #define FLAG_g 2 /* Sort using strtod() */ | 24 | enum { |
27 | #define FLAG_M 4 /* Sort date */ | 25 | FLAG_n = 1, /* Numeric sort */ |
26 | FLAG_g = 2, /* Sort using strtod() */ | ||
27 | FLAG_M = 4, /* Sort date */ | ||
28 | /* ucsz apply to root level only, not keys. b at root level implies bb */ | 28 | /* ucsz apply to root level only, not keys. b at root level implies bb */ |
29 | #define FLAG_u 8 /* Unique */ | 29 | FLAG_u = 8, /* Unique */ |
30 | #define FLAG_c 16 /* Check: no output, exit(!ordered) */ | 30 | FLAG_c = 0x10, /* Check: no output, exit(!ordered) */ |
31 | #define FLAG_s 32 /* Stable sort, no ascii fallback at end */ | 31 | FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */ |
32 | #define FLAG_z 64 /* Input is null terminated, not \n */ | 32 | FLAG_z = 0x40, /* Input is null terminated, not \n */ |
33 | /* These can be applied to search keys, the previous four can't */ | 33 | /* These can be applied to search keys, the previous four can't */ |
34 | #define FLAG_b 128 /* Ignore leading blanks */ | 34 | FLAG_b = 0x80, /* Ignore leading blanks */ |
35 | #define FLAG_r 256 /* Reverse */ | 35 | FLAG_r = 0x100, /* Reverse */ |
36 | #define FLAG_d 512 /* Ignore !(isalnum()|isspace()) */ | 36 | FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */ |
37 | #define FLAG_f 1024 /* Force uppercase */ | 37 | FLAG_f = 0x400, /* Force uppercase */ |
38 | #define FLAG_i 2048 /* Ignore !isprint() */ | 38 | FLAG_i = 0x800, /* Ignore !isprint() */ |
39 | #define FLAG_bb 32768 /* Ignore trailing blanks */ | 39 | FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */ |
40 | 40 | FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */ | |
41 | FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */ | ||
42 | FLAG_o = 0x8000, | ||
43 | FLAG_k = 0x10000, | ||
44 | FLAG_t = 0x20000, | ||
45 | FLAG_bb = 0x80000000, /* Ignore trailing blanks */ | ||
46 | }; | ||
41 | 47 | ||
42 | #if ENABLE_FEATURE_SORT_BIG | 48 | #if ENABLE_FEATURE_SORT_BIG |
43 | static char key_separator; | 49 | static char key_separator; |
44 | 50 | ||
45 | static struct sort_key { | 51 | static struct sort_key { |
46 | struct sort_key *next_key; /* linked list */ | 52 | struct sort_key *next_key; /* linked list */ |
47 | unsigned short range[4]; /* start word, start char, end word, end char */ | 53 | unsigned range[4]; /* start word, start char, end word, end char */ |
48 | int flags; | 54 | unsigned flags; |
49 | } *key_list; | 55 | } *key_list; |
50 | 56 | ||
51 | static char *get_key(char *str, struct sort_key *key, int flags) | 57 | static char *get_key(char *str, struct sort_key *key, int flags) |
@@ -59,7 +65,7 @@ static char *get_key(char *str, struct sort_key *key, int flags) | |||
59 | return str; | 65 | return str; |
60 | } | 66 | } |
61 | 67 | ||
62 | /* Find start of key on first pass, end on second pass*/ | 68 | /* Find start of key on first pass, end on second pass */ |
63 | len = strlen(str); | 69 | len = strlen(str); |
64 | for (j = 0; j < 2; j++) { | 70 | for (j = 0; j < 2; j++) { |
65 | if (!key->range[2*j]) | 71 | if (!key->range[2*j]) |
@@ -68,19 +74,25 @@ static char *get_key(char *str, struct sort_key *key, int flags) | |||
68 | else { | 74 | else { |
69 | end = 0; | 75 | end = 0; |
70 | for (i = 1; i < key->range[2*j] + j; i++) { | 76 | for (i = 1; i < key->range[2*j] + j; i++) { |
71 | /* Skip leading blanks or first separator */ | 77 | if (key_separator) { |
72 | if (str[end]) { | 78 | /* Skip first separator */ |
73 | if (!key_separator) | 79 | while (str[end] == key_separator) |
74 | while (isspace(str[end])) end++; | 80 | end++; |
75 | } | 81 | /* Skip body of key */ |
76 | /* Skip body of key */ | 82 | while (str[end]) { |
77 | for (; str[end]; end++) { | ||
78 | if (key_separator) { | ||
79 | if (str[end] == key_separator) | 83 | if (str[end] == key_separator) |
80 | break; | 84 | break; |
81 | } else { | 85 | end++; |
86 | } | ||
87 | } else { | ||
88 | /* Skip leading blanks */ | ||
89 | while (isspace(str[end])) | ||
90 | end++; | ||
91 | /* Skip body of key */ | ||
92 | while (str[end]) { | ||
82 | if (isspace(str[end])) | 93 | if (isspace(str[end])) |
83 | break; | 94 | break; |
95 | end++; | ||
84 | } | 96 | } |
85 | } | 97 | } |
86 | } | 98 | } |
@@ -140,7 +152,7 @@ static struct sort_key *add_key(void) | |||
140 | } | 152 | } |
141 | 153 | ||
142 | #define GET_LINE(fp) \ | 154 | #define GET_LINE(fp) \ |
143 | ((global_flags & FLAG_z) \ | 155 | ((option_mask32 & FLAG_z) \ |
144 | ? bb_get_chunk_from_file(fp, NULL) \ | 156 | ? bb_get_chunk_from_file(fp, NULL) \ |
145 | : xmalloc_getline(fp)) | 157 | : xmalloc_getline(fp)) |
146 | #else | 158 | #else |
@@ -150,14 +162,14 @@ static struct sort_key *add_key(void) | |||
150 | /* Iterate through keys list and perform comparisons */ | 162 | /* Iterate through keys list and perform comparisons */ |
151 | static int compare_keys(const void *xarg, const void *yarg) | 163 | static int compare_keys(const void *xarg, const void *yarg) |
152 | { | 164 | { |
153 | int flags = global_flags, retval = 0; | 165 | int flags = option_mask32, retval = 0; |
154 | char *x, *y; | 166 | char *x, *y; |
155 | 167 | ||
156 | #if ENABLE_FEATURE_SORT_BIG | 168 | #if ENABLE_FEATURE_SORT_BIG |
157 | struct sort_key *key; | 169 | struct sort_key *key; |
158 | 170 | ||
159 | for (key = key_list; !retval && key; key = key->next_key) { | 171 | for (key = key_list; !retval && key; key = key->next_key) { |
160 | flags = key->flags ? key->flags : global_flags; | 172 | flags = key->flags ? key->flags : option_mask32; |
161 | /* Chop out and modify key chunks, handling -dfib */ | 173 | /* Chop out and modify key chunks, handling -dfib */ |
162 | x = get_key(*(char **)xarg, key, flags); | 174 | x = get_key(*(char **)xarg, key, flags); |
163 | y = get_key(*(char **)yarg, key, flags); | 175 | y = get_key(*(char **)yarg, key, flags); |
@@ -175,7 +187,11 @@ static int compare_keys(const void *xarg, const void *yarg) | |||
175 | break; | 187 | break; |
176 | /* Ascii sort */ | 188 | /* Ascii sort */ |
177 | case 0: | 189 | case 0: |
190 | #if ENABLE_LOCALE_SUPPORT | ||
191 | retval = strcoll(x, y); | ||
192 | #else | ||
178 | retval = strcmp(x, y); | 193 | retval = strcmp(x, y); |
194 | #endif | ||
179 | break; | 195 | break; |
180 | #if ENABLE_FEATURE_SORT_BIG | 196 | #if ENABLE_FEATURE_SORT_BIG |
181 | case FLAG_g: { | 197 | case FLAG_g: { |
@@ -239,74 +255,97 @@ static int compare_keys(const void *xarg, const void *yarg) | |||
239 | break; | 255 | break; |
240 | } /* switch */ | 256 | } /* switch */ |
241 | #endif | 257 | #endif |
242 | } | 258 | } /* for */ |
259 | |||
243 | /* Perform fallback sort if necessary */ | 260 | /* Perform fallback sort if necessary */ |
244 | if (!retval && !(global_flags & FLAG_s)) | 261 | if (!retval && !(option_mask32 & FLAG_s)) |
245 | retval = strcmp(*(char **)xarg, *(char **)yarg); | 262 | retval = strcmp(*(char **)xarg, *(char **)yarg); |
246 | 263 | ||
247 | if (flags & FLAG_r) return -retval; | 264 | if (flags & FLAG_r) return -retval; |
248 | return retval; | 265 | return retval; |
249 | } | 266 | } |
250 | 267 | ||
268 | static unsigned str2u(char **str) | ||
269 | { | ||
270 | unsigned long lu; | ||
271 | if (!isdigit((*str)[0])) | ||
272 | bb_error_msg_and_die("bad field specification"); | ||
273 | lu = strtoul(*str, str, 10); | ||
274 | if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu) | ||
275 | bb_error_msg_and_die("bad field specification"); | ||
276 | return lu; | ||
277 | } | ||
278 | |||
251 | int sort_main(int argc, char **argv) | 279 | int sort_main(int argc, char **argv) |
252 | { | 280 | { |
253 | FILE *fp, *outfile = NULL; | 281 | FILE *fp, *outfile = stdout; |
254 | int linecount = 0, i, flag; | 282 | char *line, **lines = NULL; |
255 | char *line, **lines = NULL, *optlist = "ngMucszbrdfimS:T:o:k:t:"; | 283 | char *str_ignored, *str_o, *str_k, *str_t; |
256 | int c; | 284 | int i, flag; |
285 | int linecount = 0; | ||
257 | 286 | ||
258 | xfunc_error_retval = 2; | 287 | xfunc_error_retval = 2; |
288 | |||
259 | /* Parse command line options */ | 289 | /* Parse command line options */ |
260 | while ((c = getopt(argc, argv, optlist)) > 0) { | 290 | /* -o and -t can be given at most once */ |
261 | line = strchr(optlist, c); | 291 | opt_complementary = "?:o--o:t--t"; |
262 | if (!line) bb_show_usage(); | 292 | getopt32(argc, argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &str_k, &str_t); |
263 | switch (*line) { | ||
264 | #if ENABLE_FEATURE_SORT_BIG | 293 | #if ENABLE_FEATURE_SORT_BIG |
265 | case 'o': | 294 | if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w"); |
266 | if (outfile) bb_error_msg_and_die("too many -o"); | 295 | if (option_mask32 & FLAG_t) { |
267 | outfile = xfopen(optarg, "w"); | 296 | if (!str_t[0] || str_t[1]) |
268 | break; | 297 | bb_error_msg_and_die("bad -t parameter"); |
269 | case 't': | 298 | key_separator = str_t[0]; |
270 | if (key_separator || optarg[1]) | 299 | } |
271 | bb_error_msg_and_die("too many -t"); | 300 | /* parse sort key */ |
272 | key_separator = *optarg; | 301 | if (option_mask32 & FLAG_k) { |
273 | break; | 302 | enum { |
274 | /* parse sort key */ | 303 | FLAG_allowed_for_k = |
275 | case 'k': { | 304 | FLAG_n | /* Numeric sort */ |
276 | struct sort_key *key = add_key(); | 305 | FLAG_g | /* Sort using strtod() */ |
277 | char *temp, *temp2; | 306 | FLAG_M | /* Sort date */ |
307 | FLAG_b | /* Ignore leading blanks */ | ||
308 | FLAG_r | /* Reverse */ | ||
309 | FLAG_d | /* Ignore !(isalnum()|isspace()) */ | ||
310 | FLAG_f | /* Force uppercase */ | ||
311 | FLAG_i | /* Ignore !isprint() */ | ||
312 | 0 | ||
313 | }; | ||
314 | struct sort_key *key = add_key(); | ||
315 | const char *temp2; | ||
278 | 316 | ||
279 | temp = optarg; | 317 | i = 0; /* i==0 before comma, 1 after (-k3,6) */ |
280 | for (i = 0; *temp;) { | 318 | while (*str_k) { |
281 | /* Start of range */ | 319 | /* Start of range */ |
282 | key->range[2*i] = (unsigned short)strtol(temp, &temp, 10); | 320 | /* Cannot use bb_strtou - suffix can be a letter */ |
283 | if (*temp == '.') | 321 | key->range[2*i] = str2u(&str_k); |
284 | key->range[(2*i)+1] = (unsigned short)strtol(temp+1, &temp, 10); | 322 | if (*str_k == '.') { |
285 | for (; *temp; temp++) { | 323 | str_k++; |
286 | if (*temp == ',' && !i++) { | 324 | key->range[2*i+1] = str2u(&str_k); |
287 | temp++; | 325 | } |
288 | break; | 326 | while (*str_k) { |
289 | } /* no else needed: fall through to syntax error | 327 | if (*str_k == ',' && !i++) { |
290 | because comma isn't in optlist */ | 328 | str_k++; |
291 | temp2 = strchr(optlist, *temp); | 329 | break; |
292 | flag = (1 << (temp2 - optlist)); | 330 | } /* no else needed: fall through to syntax error |
293 | if (!temp2 || (flag > FLAG_M && flag < FLAG_b)) | 331 | because comma isn't in OPT_STR */ |
294 | bb_error_msg_and_die("unknown key option"); | 332 | temp2 = strchr(OPT_STR, *str_k); |
295 | /* b after ',' means strip _trailing_ space */ | 333 | if (!temp2) |
296 | if (i && flag == FLAG_b) flag = FLAG_bb; | 334 | bb_error_msg_and_die("unknown key option"); |
297 | key->flags |= flag; | 335 | flag = 1 << (temp2 - OPT_STR); |
298 | } | 336 | if (flag & ~FLAG_allowed_for_k) |
337 | bb_error_msg_and_die("unknown sort type"); | ||
338 | /* b after ',' means strip _trailing_ space */ | ||
339 | if (i && flag == FLAG_b) flag = FLAG_bb; | ||
340 | key->flags |= flag; | ||
341 | str_k++; | ||
299 | } | 342 | } |
300 | break; | ||
301 | } | ||
302 | #endif | ||
303 | default: | ||
304 | global_flags |= (1 << (line - optlist)); | ||
305 | /* global b strips leading and trailing spaces */ | ||
306 | if (global_flags & FLAG_b) global_flags |= FLAG_bb; | ||
307 | break; | ||
308 | } | 343 | } |
309 | } | 344 | } |
345 | #endif | ||
346 | /* global b strips leading and trailing spaces */ | ||
347 | if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb; | ||
348 | |||
310 | /* Open input files and read data */ | 349 | /* Open input files and read data */ |
311 | for (i = argv[optind] ? optind : optind-1; argv[i]; i++) { | 350 | for (i = argv[optind] ? optind : optind-1; argv[i]; i++) { |
312 | fp = stdin; | 351 | fp = stdin; |
@@ -326,8 +365,8 @@ int sort_main(int argc, char **argv) | |||
326 | if (!key_list) | 365 | if (!key_list) |
327 | add_key()->range[0] = 1; | 366 | add_key()->range[0] = 1; |
328 | /* handle -c */ | 367 | /* handle -c */ |
329 | if (global_flags & FLAG_c) { | 368 | if (option_mask32 & FLAG_c) { |
330 | int j = (global_flags & FLAG_u) ? -1 : 0; | 369 | int j = (option_mask32 & FLAG_u) ? -1 : 0; |
331 | for (i = 1; i < linecount; i++) | 370 | for (i = 1; i < linecount; i++) |
332 | if (compare_keys(&lines[i-1], &lines[i]) > j) { | 371 | if (compare_keys(&lines[i-1], &lines[i]) > j) { |
333 | fprintf(stderr, "Check line %d\n", i); | 372 | fprintf(stderr, "Check line %d\n", i); |
@@ -339,8 +378,12 @@ int sort_main(int argc, char **argv) | |||
339 | /* Perform the actual sort */ | 378 | /* Perform the actual sort */ |
340 | qsort(lines, linecount, sizeof(char *), compare_keys); | 379 | qsort(lines, linecount, sizeof(char *), compare_keys); |
341 | /* handle -u */ | 380 | /* handle -u */ |
342 | if (global_flags & FLAG_u) { | 381 | if (option_mask32 & FLAG_u) { |
343 | for (flag = 0, i = 1; i < linecount; i++) { | 382 | flag = 0; |
383 | /* coreutils 6.3 drop lines for which only key is the same */ | ||
384 | /* -- disabling last-resort compare... */ | ||
385 | option_mask32 |= FLAG_s; | ||
386 | for (i = 1; i < linecount; i++) { | ||
344 | if (!compare_keys(&lines[flag], &lines[i])) | 387 | if (!compare_keys(&lines[flag], &lines[i])) |
345 | free(lines[i]); | 388 | free(lines[i]); |
346 | else | 389 | else |
@@ -349,7 +392,6 @@ int sort_main(int argc, char **argv) | |||
349 | if (linecount) linecount = flag+1; | 392 | if (linecount) linecount = flag+1; |
350 | } | 393 | } |
351 | /* Print it */ | 394 | /* Print it */ |
352 | if (!outfile) outfile = stdout; | ||
353 | for (i = 0; i < linecount; i++) | 395 | for (i = 0; i < linecount; i++) |
354 | fprintf(outfile, "%s\n", lines[i]); | 396 | fprintf(outfile, "%s\n", lines[i]); |
355 | 397 | ||
diff --git a/coreutils/stty.c b/coreutils/stty.c index 22784a260..b48941429 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c | |||
@@ -158,13 +158,18 @@ static const char stty_dec [] = "dec"; | |||
158 | 158 | ||
159 | /* Each mode */ | 159 | /* Each mode */ |
160 | struct mode_info { | 160 | struct mode_info { |
161 | const char *name; /* Name given on command line */ | 161 | const char *name; /* Name given on command line */ |
162 | char type; /* Which structure element to change */ | 162 | char type; /* Which structure element to change */ |
163 | char flags; /* Setting and display options */ | 163 | char flags; /* Setting and display options */ |
164 | unsigned short mask; /* Other bits to turn off for this mode */ | 164 | /* were using short here, but ppc32 was unhappy: */ |
165 | unsigned long bits; /* Bits to set for this mode */ | 165 | tcflag_t mask; /* Other bits to turn off for this mode */ |
166 | tcflag_t bits; /* Bits to set for this mode */ | ||
166 | }; | 167 | }; |
167 | 168 | ||
169 | /* We can optimize it further by using name[8] instead of char *name */ | ||
170 | /* but beware of "if (info->name == evenp)" checks! */ | ||
171 | /* Need to replace them with "if (info == &mode_info[EVENP_INDX])" */ | ||
172 | |||
168 | #define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B } | 173 | #define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B } |
169 | 174 | ||
170 | static const struct mode_info mode_info[] = { | 175 | static const struct mode_info mode_info[] = { |
@@ -319,9 +324,9 @@ enum { | |||
319 | 324 | ||
320 | /* Control character settings */ | 325 | /* Control character settings */ |
321 | struct control_info { | 326 | struct control_info { |
322 | const char *name; /* Name given on command line */ | 327 | const char *name; /* Name given on command line */ |
323 | unsigned char saneval; /* Value to set for 'stty sane' */ | 328 | unsigned char saneval; /* Value to set for 'stty sane' */ |
324 | unsigned char offset; /* Offset in c_cc */ | 329 | unsigned char offset; /* Offset in c_cc */ |
325 | }; | 330 | }; |
326 | 331 | ||
327 | /* Control characters */ | 332 | /* Control characters */ |
@@ -968,9 +973,9 @@ static void set_mode(const struct mode_info *info, int reversed, | |||
968 | 973 | ||
969 | if (bitsp) { | 974 | if (bitsp) { |
970 | if (reversed) | 975 | if (reversed) |
971 | *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits; | 976 | *bitsp = *bitsp & ~info->mask & ~info->bits; |
972 | else | 977 | else |
973 | *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits; | 978 | *bitsp = (*bitsp & ~info->mask) | info->bits; |
974 | return; | 979 | return; |
975 | } | 980 | } |
976 | 981 | ||
diff --git a/shell/msh.c b/shell/msh.c index e43cb1875..c88308f8f 100644 --- a/shell/msh.c +++ b/shell/msh.c | |||
@@ -4860,9 +4860,12 @@ static int qstrchar(struct ioarg *ap) | |||
4860 | { | 4860 | { |
4861 | int c; | 4861 | int c; |
4862 | 4862 | ||
4863 | if (ap->aword == NULL) || (c = *ap->aword++) == 0) | 4863 | if (ap->aword == NULL) |
4864 | return 0; | 4864 | return 0; |
4865 | return c | QUOTE; | 4865 | c = *ap->aword++; |
4866 | if (c) | ||
4867 | c |= QUOTE; | ||
4868 | return c; | ||
4866 | } | 4869 | } |
4867 | 4870 | ||
4868 | /* | 4871 | /* |