aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-07-08 01:37:54 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-07-08 01:37:54 +0200
commitcbe8c65e590949cad25dccba90101553b77d88c9 (patch)
tree6139aa34a9a6d1efb013f60243cd5d88e7ac1ce6
parent008e73b5728b19c7ba6e746e279c4bc8a1935f0d (diff)
downloadbusybox-w32-cbe8c65e590949cad25dccba90101553b77d88c9.tar.gz
busybox-w32-cbe8c65e590949cad25dccba90101553b77d88c9.tar.bz2
busybox-w32-cbe8c65e590949cad25dccba90101553b77d88c9.zip
sort: add -V "sort version"
function old new delta packed_usage 32855 32858 +3 get_key 500 503 +3 sort_opt_str 36 37 +1 sort_main 1037 1036 -1 compare_keys 795 783 -12 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/2 up/down: 7/-13) Total: -6 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/sort.c47
1 files changed, 27 insertions, 20 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index 4d741e76d..05e5c9071 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -60,6 +60,7 @@
60//usage: IF_FEATURE_SORT_BIG( 60//usage: IF_FEATURE_SORT_BIG(
61//usage: "\n -g General numerical sort" 61//usage: "\n -g General numerical sort"
62//usage: "\n -M Sort month" 62//usage: "\n -M Sort month"
63//usage: "\n -V Sort version"
63//usage: "\n -t CHAR Field separator" 64//usage: "\n -t CHAR Field separator"
64//usage: "\n -k N[,M] Sort by Nth field" 65//usage: "\n -k N[,M] Sort by Nth field"
65//usage: ) 66//usage: )
@@ -91,32 +92,33 @@
91 92
92/* These are sort types */ 93/* These are sort types */
93enum { 94enum {
94 FLAG_n = 1, /* Numeric sort */ 95 FLAG_n = 1 << 0, /* Numeric sort */
95 FLAG_g = 2, /* Sort using strtod() */ 96 FLAG_g = 1 << 1, /* Sort using strtod() */
96 FLAG_M = 4, /* Sort date */ 97 FLAG_M = 1 << 2, /* Sort date */
98 FLAG_V = 1 << 3, /* Sort version */
97/* ucsz apply to root level only, not keys. b at root level implies bb */ 99/* ucsz apply to root level only, not keys. b at root level implies bb */
98 FLAG_u = 8, /* Unique */ 100 FLAG_u = 1 << 4, /* Unique */
99 FLAG_c = 0x10, /* Check: no output, exit(!ordered) */ 101 FLAG_c = 1 << 5, /* Check: no output, exit(!ordered) */
100 FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */ 102 FLAG_s = 1 << 6, /* Stable sort, no ascii fallback at end */
101 FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */ 103 FLAG_z = 1 << 7, /* Input and output is NUL terminated, not \n */
102/* These can be applied to search keys, the previous four can't */ 104/* These can be applied to search keys, the previous four can't */
103 FLAG_b = 0x80, /* Ignore leading blanks */ 105 FLAG_b = 1 << 8, /* Ignore leading blanks */
104 FLAG_r = 0x100, /* Reverse */ 106 FLAG_r = 1 << 9, /* Reverse */
105 FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */ 107 FLAG_d = 1 << 10, /* Ignore !(isalnum()|isspace()) */
106 FLAG_f = 0x400, /* Force uppercase */ 108 FLAG_f = 1 << 11, /* Force uppercase */
107 FLAG_i = 0x800, /* Ignore !isprint() */ 109 FLAG_i = 1 << 12, /* Ignore !isprint() */
108 FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */ 110 FLAG_m = 1 << 13, /* ignored: merge already sorted files; do not sort */
109 FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */ 111 FLAG_S = 1 << 14, /* ignored: -S, --buffer-size=SIZE */
110 FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */ 112 FLAG_T = 1 << 15, /* ignored: -T, --temporary-directory=DIR */
111 FLAG_o = 0x8000, 113 FLAG_o = 1 << 16,
112 FLAG_k = 0x10000, 114 FLAG_k = 1 << 17,
113 FLAG_t = 0x20000, 115 FLAG_t = 1 << 18,
114 FLAG_bb = 0x80000000, /* Ignore trailing blanks */ 116 FLAG_bb = 0x80000000, /* Ignore trailing blanks */
115 FLAG_no_tie_break = 0x40000000, 117 FLAG_no_tie_break = 0x40000000,
116}; 118};
117 119
118static const char sort_opt_str[] ALIGN1 = "^" 120static const char sort_opt_str[] ALIGN1 = "^"
119 "ngMucszbrdfimS:T:o:k:*t:" 121 "ngMVucszbrdfimS:T:o:k:*t:"
120 "\0" "o--o:t--t"/*-t, -o: at most one of each*/; 122 "\0" "o--o:t--t"/*-t, -o: at most one of each*/;
121/* 123/*
122 * OPT_STR must not be string literal, needs to have stable address: 124 * OPT_STR must not be string literal, needs to have stable address:
@@ -273,10 +275,15 @@ static int compare_keys(const void *xarg, const void *yarg)
273 y = *(char **)yarg; 275 y = *(char **)yarg;
274#endif 276#endif
275 /* Perform actual comparison */ 277 /* Perform actual comparison */
276 switch (flags & (FLAG_n | FLAG_M | FLAG_g)) { 278 switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) {
277 default: 279 default:
278 bb_error_msg_and_die("unknown sort type"); 280 bb_error_msg_and_die("unknown sort type");
279 break; 281 break;
282#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
283 case FLAG_V:
284 retval = strverscmp(x, y);
285 break;
286#endif
280 /* Ascii sort */ 287 /* Ascii sort */
281 case 0: 288 case 0:
282#if ENABLE_LOCALE_SUPPORT 289#if ENABLE_LOCALE_SUPPORT