diff options
author | Ron Yorston <rmy@pobox.com> | 2018-04-09 08:50:34 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-04-09 08:50:34 +0100 |
commit | 921c1ab66bad54d4ad8591bb74e41ac985248496 (patch) | |
tree | 552a04c691e78e78570e4ec2c83fbc0e59953924 /coreutils | |
parent | 5b6f06f5eb8628955262508d153627fe6f2d1c8b (diff) | |
parent | a1870f4807a75663a085c9f5e92870fa7554f0ad (diff) | |
download | busybox-w32-921c1ab66bad54d4ad8591bb74e41ac985248496.tar.gz busybox-w32-921c1ab66bad54d4ad8591bb74e41ac985248496.tar.bz2 busybox-w32-921c1ab66bad54d4ad8591bb74e41ac985248496.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/mv.c | 2 | ||||
-rw-r--r-- | coreutils/nice.c | 1 | ||||
-rw-r--r-- | coreutils/sort.c | 90 | ||||
-rw-r--r-- | coreutils/test.c | 1 |
4 files changed, 83 insertions, 11 deletions
diff --git a/coreutils/mv.c b/coreutils/mv.c index aeafd1e40..6e11197a1 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
@@ -101,7 +101,7 @@ int mv_main(int argc, char **argv) | |||
101 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { | 101 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { |
102 | goto RET_1; /* Ouch! fprintf failed! */ | 102 | goto RET_1; /* Ouch! fprintf failed! */ |
103 | } | 103 | } |
104 | if (!bb_ask_confirmation()) { | 104 | if (!bb_ask_y_confirmation()) { |
105 | goto RET_0; | 105 | goto RET_0; |
106 | } | 106 | } |
107 | } | 107 | } |
diff --git a/coreutils/nice.c b/coreutils/nice.c index aa8b06cce..da1696c42 100644 --- a/coreutils/nice.c +++ b/coreutils/nice.c | |||
@@ -22,7 +22,6 @@ | |||
22 | //usage: "Change scheduling priority, run PROG\n" | 22 | //usage: "Change scheduling priority, run PROG\n" |
23 | //usage: "\n -n ADJUST Adjust priority by ADJUST" | 23 | //usage: "\n -n ADJUST Adjust priority by ADJUST" |
24 | 24 | ||
25 | #include <sys/resource.h> | ||
26 | #include "libbb.h" | 25 | #include "libbb.h" |
27 | 26 | ||
28 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 27 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
diff --git a/coreutils/sort.c b/coreutils/sort.c index b39297a26..4d741e76d 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
@@ -18,16 +18,24 @@ | |||
18 | //config: sort is used to sort lines of text in specified files. | 18 | //config: sort is used to sort lines of text in specified files. |
19 | //config: | 19 | //config: |
20 | //config:config FEATURE_SORT_BIG | 20 | //config:config FEATURE_SORT_BIG |
21 | //config: bool "Full SuSv3 compliant sort (support -ktcsbdfiozgM)" | 21 | //config: bool "Full SuSv3 compliant sort (support -ktcbdfiogM)" |
22 | //config: default y | 22 | //config: default y |
23 | //config: depends on SORT | 23 | //config: depends on SORT |
24 | //config: help | 24 | //config: help |
25 | //config: Without this, sort only supports -r, -u, and an integer version | 25 | //config: Without this, sort only supports -rusz, and an integer version |
26 | //config: of -n. Selecting this adds sort keys, floating point support, and | 26 | //config: of -n. Selecting this adds sort keys, floating point support, and |
27 | //config: more. This adds a little over 3k to a nonstatic build on x86. | 27 | //config: more. This adds a little over 3k to a nonstatic build on x86. |
28 | //config: | 28 | //config: |
29 | //config: The SuSv3 sort standard is available at: | 29 | //config: The SuSv3 sort standard is available at: |
30 | //config: http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html | 30 | //config: http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html |
31 | //config: | ||
32 | //config:config FEATURE_SORT_OPTIMIZE_MEMORY | ||
33 | //config: bool "Use less memory (but might be slower)" | ||
34 | //config: default n # defaults to N since we are size-paranoid tribe | ||
35 | //config: depends on SORT | ||
36 | //config: help | ||
37 | //config: Attempt to use less memory (by storing only one copy | ||
38 | //config: of duplicated lines, and such). Useful if you work on huge files. | ||
31 | 39 | ||
32 | //applet:IF_SORT(APPLET_NOEXEC(sort, sort, BB_DIR_USR_BIN, BB_SUID_DROP, sort)) | 40 | //applet:IF_SORT(APPLET_NOEXEC(sort, sort, BB_DIR_USR_BIN, BB_SUID_DROP, sort)) |
33 | 41 | ||
@@ -46,26 +54,22 @@ | |||
46 | //usage: "\n -f Ignore case" | 54 | //usage: "\n -f Ignore case" |
47 | //usage: "\n -i Ignore unprintable characters" | 55 | //usage: "\n -i Ignore unprintable characters" |
48 | //usage: "\n -d Dictionary order (blank or alphanumeric only)" | 56 | //usage: "\n -d Dictionary order (blank or alphanumeric only)" |
49 | //usage: "\n -g General numerical sort" | ||
50 | //usage: "\n -M Sort month" | ||
51 | //usage: ) | 57 | //usage: ) |
52 | //-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G) | 58 | //-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G) |
53 | //usage: "\n -n Sort numbers" | 59 | //usage: "\n -n Sort numbers" |
54 | //usage: IF_FEATURE_SORT_BIG( | 60 | //usage: IF_FEATURE_SORT_BIG( |
61 | //usage: "\n -g General numerical sort" | ||
62 | //usage: "\n -M Sort month" | ||
55 | //usage: "\n -t CHAR Field separator" | 63 | //usage: "\n -t CHAR Field separator" |
56 | //usage: "\n -k N[,M] Sort by Nth field" | 64 | //usage: "\n -k N[,M] Sort by Nth field" |
57 | //usage: ) | 65 | //usage: ) |
58 | //usage: "\n -r Reverse sort order" | 66 | //usage: "\n -r Reverse sort order" |
59 | //usage: IF_FEATURE_SORT_BIG( | ||
60 | //usage: "\n -s Stable (don't sort ties alphabetically)" | 67 | //usage: "\n -s Stable (don't sort ties alphabetically)" |
61 | //usage: ) | ||
62 | //usage: "\n -u Suppress duplicate lines" | 68 | //usage: "\n -u Suppress duplicate lines" |
63 | //usage: IF_FEATURE_SORT_BIG( | ||
64 | //usage: "\n -z Lines are terminated by NUL, not newline" | 69 | //usage: "\n -z Lines are terminated by NUL, not newline" |
65 | ///////: "\n -m Ignored for GNU compatibility" | 70 | ///////: "\n -m Ignored for GNU compatibility" |
66 | ///////: "\n -S BUFSZ Ignored for GNU compatibility" | 71 | ///////: "\n -S BUFSZ Ignored for GNU compatibility" |
67 | ///////: "\n -T TMPDIR Ignored for GNU compatibility" | 72 | ///////: "\n -T TMPDIR Ignored for GNU compatibility" |
68 | //usage: ) | ||
69 | //usage: | 73 | //usage: |
70 | //usage:#define sort_example_usage | 74 | //usage:#define sort_example_usage |
71 | //usage: "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" | 75 | //usage: "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" |
@@ -404,6 +408,15 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
404 | int i; | 408 | int i; |
405 | int linecount; | 409 | int linecount; |
406 | unsigned opts; | 410 | unsigned opts; |
411 | #if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY | ||
412 | bool can_drop_dups; | ||
413 | size_t prev_len = 0; | ||
414 | char *prev_line = (char*) ""; | ||
415 | /* Postpone optimizing if the input is small, < 16k lines: | ||
416 | * even just free()ing duplicate lines takes time. | ||
417 | */ | ||
418 | size_t count_to_optimize_dups = 0x3fff; | ||
419 | #endif | ||
407 | 420 | ||
408 | xfunc_error_retval = 2; | 421 | xfunc_error_retval = 2; |
409 | 422 | ||
@@ -412,6 +425,29 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
412 | sort_opt_str, | 425 | sort_opt_str, |
413 | &str_ignored, &str_ignored, &str_o, &lst_k, &str_t | 426 | &str_ignored, &str_ignored, &str_o, &lst_k, &str_t |
414 | ); | 427 | ); |
428 | #if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY | ||
429 | /* Can drop dups only if -u but no "complicating" options, | ||
430 | * IOW: if we do a full line compares. Safe options: | ||
431 | * -o FILE Output to FILE | ||
432 | * -z Lines are terminated by NUL, not newline | ||
433 | * -r Reverse sort order | ||
434 | * -s Stable (don't sort ties alphabetically) | ||
435 | * Not sure about these: | ||
436 | * -b Ignore leading blanks | ||
437 | * -f Ignore case | ||
438 | * -i Ignore unprintable characters | ||
439 | * -d Dictionary order (blank or alphanumeric only) | ||
440 | * -n Sort numbers | ||
441 | * -g General numerical sort | ||
442 | * -M Sort month | ||
443 | */ | ||
444 | can_drop_dups = ((opts & ~(FLAG_o|FLAG_z|FLAG_r|FLAG_s)) == FLAG_u); | ||
445 | /* Stable sort needs every line to be uniquely allocated, | ||
446 | * disable optimization to reuse strings: | ||
447 | */ | ||
448 | if (opts & FLAG_s) | ||
449 | count_to_optimize_dups = (size_t)-1L; | ||
450 | #endif | ||
415 | /* global b strips leading and trailing spaces */ | 451 | /* global b strips leading and trailing spaces */ |
416 | if (opts & FLAG_b) | 452 | if (opts & FLAG_b) |
417 | option_mask32 |= FLAG_bb; | 453 | option_mask32 |= FLAG_bb; |
@@ -489,6 +525,44 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
489 | char *line = GET_LINE(fp); | 525 | char *line = GET_LINE(fp); |
490 | if (!line) | 526 | if (!line) |
491 | break; | 527 | break; |
528 | |||
529 | #if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY | ||
530 | if (count_to_optimize_dups != 0) | ||
531 | count_to_optimize_dups--; | ||
532 | if (count_to_optimize_dups == 0) { | ||
533 | size_t len; | ||
534 | char *new_line; | ||
535 | |||
536 | /* On kernel/linux/arch/ *.[ch] files, | ||
537 | * this reduces memory usage by 6%. | ||
538 | * yes | head -99999999 | sort | ||
539 | * goes down from 1900Mb to 380 Mb. | ||
540 | */ | ||
541 | len = strlen(line); | ||
542 | if (len <= prev_len) { | ||
543 | new_line = prev_line + (prev_len - len); | ||
544 | if (strcmp(line, new_line) == 0) { | ||
545 | /* it's a tail of the prev line */ | ||
546 | if (can_drop_dups && prev_len == len) { | ||
547 | /* it's identical to prev line */ | ||
548 | free(line); | ||
549 | continue; | ||
550 | } | ||
551 | free(line); | ||
552 | line = new_line; | ||
553 | /* continue using longer prev_line | ||
554 | * for future tail tests. | ||
555 | */ | ||
556 | goto skip; | ||
557 | } | ||
558 | } | ||
559 | prev_len = len; | ||
560 | prev_line = line; | ||
561 | skip: ; | ||
562 | } | ||
563 | #else | ||
564 | //TODO: lighter version which only drops total dups if can_drop_dups == true | ||
565 | #endif | ||
492 | lines = xrealloc_vector(lines, 6, linecount); | 566 | lines = xrealloc_vector(lines, 6, linecount); |
493 | lines[linecount++] = line; | 567 | lines[linecount++] = line; |
494 | } | 568 | } |
diff --git a/coreutils/test.c b/coreutils/test.c index a8286525a..824ce3b5a 100644 --- a/coreutils/test.c +++ b/coreutils/test.c | |||
@@ -76,7 +76,6 @@ | |||
76 | //usage: "1\n" | 76 | //usage: "1\n" |
77 | 77 | ||
78 | #include "libbb.h" | 78 | #include "libbb.h" |
79 | #include <setjmp.h> | ||
80 | 79 | ||
81 | /* This is a NOFORK applet. Be very careful! */ | 80 | /* This is a NOFORK applet. Be very careful! */ |
82 | 81 | ||