1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * SuS3 compliant sort implementation for busybox
|
---|
4 | *
|
---|
5 | * Copyright (C) 2004 by Rob Landley <rob@landley.net>
|
---|
6 | *
|
---|
7 | * MAINTAINER: Rob Landley <rob@landley.net>
|
---|
8 | *
|
---|
9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
10 | *
|
---|
11 | * See SuS3 sort standard at:
|
---|
12 | * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
|
---|
13 | */
|
---|
14 |
|
---|
15 | //usage:#define sort_trivial_usage
|
---|
16 | //usage: "[-nru"
|
---|
17 | //usage: IF_FEATURE_SORT_BIG("gMcszbdfimSTokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
|
---|
18 | //usage: "] [FILE]..."
|
---|
19 | //usage:#define sort_full_usage "\n\n"
|
---|
20 | //usage: "Sort lines of text\n"
|
---|
21 | //usage: IF_FEATURE_SORT_BIG(
|
---|
22 | //usage: "\n -b Ignore leading blanks"
|
---|
23 | //usage: "\n -c Check whether input is sorted"
|
---|
24 | //usage: "\n -d Dictionary order (blank or alphanumeric only)"
|
---|
25 | //usage: "\n -f Ignore case"
|
---|
26 | //usage: "\n -g General numerical sort"
|
---|
27 | //usage: "\n -i Ignore unprintable characters"
|
---|
28 | //usage: "\n -k Sort key"
|
---|
29 | //usage: "\n -M Sort month"
|
---|
30 | //usage: )
|
---|
31 | //usage: "\n -n Sort numbers"
|
---|
32 | //usage: IF_FEATURE_SORT_BIG(
|
---|
33 | //usage: "\n -o Output to file"
|
---|
34 | //usage: "\n -k Sort by key"
|
---|
35 | //usage: "\n -t CHAR Key separator"
|
---|
36 | //usage: )
|
---|
37 | //usage: "\n -r Reverse sort order"
|
---|
38 | //usage: IF_FEATURE_SORT_BIG(
|
---|
39 | //usage: "\n -s Stable (don't sort ties alphabetically)"
|
---|
40 | //usage: )
|
---|
41 | //usage: "\n -u Suppress duplicate lines"
|
---|
42 | //usage: IF_FEATURE_SORT_BIG(
|
---|
43 | //usage: "\n -z Lines are terminated by NUL, not newline"
|
---|
44 | //usage: "\n -mST Ignored for GNU compatibility")
|
---|
45 | //usage:
|
---|
46 | //usage:#define sort_example_usage
|
---|
47 | //usage: "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n"
|
---|
48 | //usage: "a\n"
|
---|
49 | //usage: "b\n"
|
---|
50 | //usage: "c\n"
|
---|
51 | //usage: "d\n"
|
---|
52 | //usage: "e\n"
|
---|
53 | //usage: "f\n"
|
---|
54 | //usage: IF_FEATURE_SORT_BIG(
|
---|
55 | //usage: "$ echo -e \"c 3\\nb 2\\nd 2\" | $SORT -k 2,2n -k 1,1r\n"
|
---|
56 | //usage: "d 2\n"
|
---|
57 | //usage: "b 2\n"
|
---|
58 | //usage: "c 3\n"
|
---|
59 | //usage: )
|
---|
60 | //usage: ""
|
---|
61 |
|
---|
62 | #include "libbb.h"
|
---|
63 |
|
---|
64 | /* This is a NOEXEC applet. Be very careful! */
|
---|
65 |
|
---|
66 |
|
---|
67 | /*
|
---|
68 | sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
|
---|
69 | sort -c [-bdfinru][-t char][-k keydef][file]
|
---|
70 | */
|
---|
71 |
|
---|
72 | /* These are sort types */
|
---|
73 | static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
|
---|
74 | enum {
|
---|
75 | FLAG_n = 1, /* Numeric sort */
|
---|
76 | FLAG_g = 2, /* Sort using strtod() */
|
---|
77 | FLAG_M = 4, /* Sort date */
|
---|
78 | /* ucsz apply to root level only, not keys. b at root level implies bb */
|
---|
79 | FLAG_u = 8, /* Unique */
|
---|
80 | FLAG_c = 0x10, /* Check: no output, exit(!ordered) */
|
---|
81 | FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */
|
---|
82 | FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */
|
---|
83 | /* These can be applied to search keys, the previous four can't */
|
---|
84 | FLAG_b = 0x80, /* Ignore leading blanks */
|
---|
85 | FLAG_r = 0x100, /* Reverse */
|
---|
86 | FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */
|
---|
87 | FLAG_f = 0x400, /* Force uppercase */
|
---|
88 | FLAG_i = 0x800, /* Ignore !isprint() */
|
---|
89 | FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */
|
---|
90 | FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */
|
---|
91 | FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */
|
---|
92 | FLAG_o = 0x8000,
|
---|
93 | FLAG_k = 0x10000,
|
---|
94 | FLAG_t = 0x20000,
|
---|
95 | FLAG_bb = 0x80000000, /* Ignore trailing blanks */
|
---|
96 | };
|
---|
97 |
|
---|
98 | #if ENABLE_FEATURE_SORT_BIG
|
---|
99 | static char key_separator;
|
---|
100 |
|
---|
101 | static struct sort_key {
|
---|
102 | struct sort_key *next_key; /* linked list */
|
---|
103 | unsigned range[4]; /* start word, start char, end word, end char */
|
---|
104 | unsigned flags;
|
---|
105 | } *key_list;
|
---|
106 |
|
---|
107 | static char *get_key(char *str, struct sort_key *key, int flags)
|
---|
108 | {
|
---|
109 | int start = 0, end = 0, len, j;
|
---|
110 | unsigned i;
|
---|
111 |
|
---|
112 | /* Special case whole string, so we don't have to make a copy */
|
---|
113 | if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
|
---|
114 | && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
|
---|
115 | ) {
|
---|
116 | return str;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* Find start of key on first pass, end on second pass */
|
---|
120 | len = strlen(str);
|
---|
121 | for (j = 0; j < 2; j++) {
|
---|
122 | if (!key->range[2*j])
|
---|
123 | end = len;
|
---|
124 | /* Loop through fields */
|
---|
125 | else {
|
---|
126 | end = 0;
|
---|
127 | for (i = 1; i < key->range[2*j] + j; i++) {
|
---|
128 | if (key_separator) {
|
---|
129 | /* Skip body of key and separator */
|
---|
130 | while (str[end]) {
|
---|
131 | if (str[end++] == key_separator)
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | } else {
|
---|
135 | /* Skip leading blanks */
|
---|
136 | while (isspace(str[end]))
|
---|
137 | end++;
|
---|
138 | /* Skip body of key */
|
---|
139 | while (str[end]) {
|
---|
140 | if (isspace(str[end]))
|
---|
141 | break;
|
---|
142 | end++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (!j) start = end;
|
---|
148 | }
|
---|
149 | /* Strip leading whitespace if necessary */
|
---|
150 | //XXX: skip_whitespace()
|
---|
151 | if (flags & FLAG_b)
|
---|
152 | while (isspace(str[start])) start++;
|
---|
153 | /* Strip trailing whitespace if necessary */
|
---|
154 | if (flags & FLAG_bb)
|
---|
155 | while (end > start && isspace(str[end-1])) end--;
|
---|
156 | /* Handle offsets on start and end */
|
---|
157 | if (key->range[3]) {
|
---|
158 | end += key->range[3] - 1;
|
---|
159 | if (end > len) end = len;
|
---|
160 | }
|
---|
161 | if (key->range[1]) {
|
---|
162 | start += key->range[1] - 1;
|
---|
163 | if (start > len) start = len;
|
---|
164 | }
|
---|
165 | /* Make the copy */
|
---|
166 | if (end < start) end = start;
|
---|
167 | str = xstrndup(str+start, end-start);
|
---|
168 | /* Handle -d */
|
---|
169 | if (flags & FLAG_d) {
|
---|
170 | for (start = end = 0; str[end]; end++)
|
---|
171 | if (isspace(str[end]) || isalnum(str[end]))
|
---|
172 | str[start++] = str[end];
|
---|
173 | str[start] = '\0';
|
---|
174 | }
|
---|
175 | /* Handle -i */
|
---|
176 | if (flags & FLAG_i) {
|
---|
177 | for (start = end = 0; str[end]; end++)
|
---|
178 | if (isprint_asciionly(str[end]))
|
---|
179 | str[start++] = str[end];
|
---|
180 | str[start] = '\0';
|
---|
181 | }
|
---|
182 | /* Handle -f */
|
---|
183 | if (flags & FLAG_f)
|
---|
184 | for (i = 0; str[i]; i++)
|
---|
185 | str[i] = toupper(str[i]);
|
---|
186 |
|
---|
187 | return str;
|
---|
188 | }
|
---|
189 |
|
---|
190 | static struct sort_key *add_key(void)
|
---|
191 | {
|
---|
192 | struct sort_key **pkey = &key_list;
|
---|
193 | while (*pkey)
|
---|
194 | pkey = &((*pkey)->next_key);
|
---|
195 | return *pkey = xzalloc(sizeof(struct sort_key));
|
---|
196 | }
|
---|
197 |
|
---|
198 | #define GET_LINE(fp) \
|
---|
199 | ((option_mask32 & FLAG_z) \
|
---|
200 | ? bb_get_chunk_from_file(fp, NULL) \
|
---|
201 | : xmalloc_fgetline(fp))
|
---|
202 | #else
|
---|
203 | #define GET_LINE(fp) xmalloc_fgetline(fp)
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | /* Iterate through keys list and perform comparisons */
|
---|
207 | static int compare_keys(const void *xarg, const void *yarg)
|
---|
208 | {
|
---|
209 | int flags = option_mask32, retval = 0;
|
---|
210 | char *x, *y;
|
---|
211 |
|
---|
212 | #if ENABLE_FEATURE_SORT_BIG
|
---|
213 | struct sort_key *key;
|
---|
214 |
|
---|
215 | for (key = key_list; !retval && key; key = key->next_key) {
|
---|
216 | flags = key->flags ? key->flags : option_mask32;
|
---|
217 | /* Chop out and modify key chunks, handling -dfib */
|
---|
218 | x = get_key(*(char **)xarg, key, flags);
|
---|
219 | y = get_key(*(char **)yarg, key, flags);
|
---|
220 | #else
|
---|
221 | /* This curly bracket serves no purpose but to match the nesting
|
---|
222 | * level of the for () loop we're not using */
|
---|
223 | {
|
---|
224 | x = *(char **)xarg;
|
---|
225 | y = *(char **)yarg;
|
---|
226 | #endif
|
---|
227 | /* Perform actual comparison */
|
---|
228 | switch (flags & 7) {
|
---|
229 | default:
|
---|
230 | bb_error_msg_and_die("unknown sort type");
|
---|
231 | break;
|
---|
232 | /* Ascii sort */
|
---|
233 | case 0:
|
---|
234 | #if ENABLE_LOCALE_SUPPORT
|
---|
235 | retval = strcoll(x, y);
|
---|
236 | #else
|
---|
237 | retval = strcmp(x, y);
|
---|
238 | #endif
|
---|
239 | break;
|
---|
240 | #if ENABLE_FEATURE_SORT_BIG
|
---|
241 | case FLAG_g: {
|
---|
242 | char *xx, *yy;
|
---|
243 | double dx = strtod(x, &xx);
|
---|
244 | double dy = strtod(y, &yy);
|
---|
245 | /* not numbers < NaN < -infinity < numbers < +infinity) */
|
---|
246 | if (x == xx)
|
---|
247 | retval = (y == yy ? 0 : -1);
|
---|
248 | else if (y == yy)
|
---|
249 | retval = 1;
|
---|
250 | /* Check for isnan */
|
---|
251 | else if (dx != dx)
|
---|
252 | retval = (dy != dy) ? 0 : -1;
|
---|
253 | else if (dy != dy)
|
---|
254 | retval = 1;
|
---|
255 | /* Check for infinity. Could underflow, but it avoids libm. */
|
---|
256 | else if (1.0 / dx == 0.0) {
|
---|
257 | if (dx < 0)
|
---|
258 | retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
|
---|
259 | else
|
---|
260 | retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
|
---|
261 | } else if (1.0 / dy == 0.0)
|
---|
262 | retval = (dy < 0) ? 1 : -1;
|
---|
263 | else
|
---|
264 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | case FLAG_M: {
|
---|
268 | struct tm thyme;
|
---|
269 | int dx;
|
---|
270 | char *xx, *yy;
|
---|
271 |
|
---|
272 | xx = strptime(x, "%b", &thyme);
|
---|
273 | dx = thyme.tm_mon;
|
---|
274 | yy = strptime(y, "%b", &thyme);
|
---|
275 | if (!xx)
|
---|
276 | retval = (!yy) ? 0 : -1;
|
---|
277 | else if (!yy)
|
---|
278 | retval = 1;
|
---|
279 | else
|
---|
280 | retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | /* Full floating point version of -n */
|
---|
284 | case FLAG_n: {
|
---|
285 | double dx = atof(x);
|
---|
286 | double dy = atof(y);
|
---|
287 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | } /* switch */
|
---|
291 | /* Free key copies. */
|
---|
292 | if (x != *(char **)xarg) free(x);
|
---|
293 | if (y != *(char **)yarg) free(y);
|
---|
294 | /* if (retval) break; - done by for () anyway */
|
---|
295 | #else
|
---|
296 | /* Integer version of -n for tiny systems */
|
---|
297 | case FLAG_n:
|
---|
298 | retval = atoi(x) - atoi(y);
|
---|
299 | break;
|
---|
300 | } /* switch */
|
---|
301 | #endif
|
---|
302 | } /* for */
|
---|
303 |
|
---|
304 | /* Perform fallback sort if necessary */
|
---|
305 | if (!retval && !(option_mask32 & FLAG_s))
|
---|
306 | retval = strcmp(*(char **)xarg, *(char **)yarg);
|
---|
307 |
|
---|
308 | if (flags & FLAG_r) return -retval;
|
---|
309 | return retval;
|
---|
310 | }
|
---|
311 |
|
---|
312 | #if ENABLE_FEATURE_SORT_BIG
|
---|
313 | static unsigned str2u(char **str)
|
---|
314 | {
|
---|
315 | unsigned long lu;
|
---|
316 | if (!isdigit((*str)[0]))
|
---|
317 | bb_error_msg_and_die("bad field specification");
|
---|
318 | lu = strtoul(*str, str, 10);
|
---|
319 | if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu)
|
---|
320 | bb_error_msg_and_die("bad field specification");
|
---|
321 | return lu;
|
---|
322 | }
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
326 | int sort_main(int argc UNUSED_PARAM, char **argv)
|
---|
327 | {
|
---|
328 | char *line, **lines;
|
---|
329 | char *str_ignored, *str_o, *str_t;
|
---|
330 | llist_t *lst_k = NULL;
|
---|
331 | int i, flag;
|
---|
332 | int linecount;
|
---|
333 | unsigned opts;
|
---|
334 |
|
---|
335 | xfunc_error_retval = 2;
|
---|
336 |
|
---|
337 | /* Parse command line options */
|
---|
338 | /* -o and -t can be given at most once */
|
---|
339 | opt_complementary = "o--o:t--t:" /* -t, -o: at most one of each */
|
---|
340 | "k::"; /* -k takes list */
|
---|
341 | opts = getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
|
---|
342 | /* global b strips leading and trailing spaces */
|
---|
343 | if (opts & FLAG_b)
|
---|
344 | option_mask32 |= FLAG_bb;
|
---|
345 | #if ENABLE_FEATURE_SORT_BIG
|
---|
346 | if (opts & FLAG_t) {
|
---|
347 | if (!str_t[0] || str_t[1])
|
---|
348 | bb_error_msg_and_die("bad -t parameter");
|
---|
349 | key_separator = str_t[0];
|
---|
350 | }
|
---|
351 | /* note: below this point we use option_mask32, not opts,
|
---|
352 | * since that reduces register pressure and makes code smaller */
|
---|
353 |
|
---|
354 | /* parse sort key */
|
---|
355 | while (lst_k) {
|
---|
356 | enum {
|
---|
357 | FLAG_allowed_for_k =
|
---|
358 | FLAG_n | /* Numeric sort */
|
---|
359 | FLAG_g | /* Sort using strtod() */
|
---|
360 | FLAG_M | /* Sort date */
|
---|
361 | FLAG_b | /* Ignore leading blanks */
|
---|
362 | FLAG_r | /* Reverse */
|
---|
363 | FLAG_d | /* Ignore !(isalnum()|isspace()) */
|
---|
364 | FLAG_f | /* Force uppercase */
|
---|
365 | FLAG_i | /* Ignore !isprint() */
|
---|
366 | 0
|
---|
367 | };
|
---|
368 | struct sort_key *key = add_key();
|
---|
369 | char *str_k = llist_pop(&lst_k);
|
---|
370 |
|
---|
371 | i = 0; /* i==0 before comma, 1 after (-k3,6) */
|
---|
372 | while (*str_k) {
|
---|
373 | /* Start of range */
|
---|
374 | /* Cannot use bb_strtou - suffix can be a letter */
|
---|
375 | key->range[2*i] = str2u(&str_k);
|
---|
376 | if (*str_k == '.') {
|
---|
377 | str_k++;
|
---|
378 | key->range[2*i+1] = str2u(&str_k);
|
---|
379 | }
|
---|
380 | while (*str_k) {
|
---|
381 | const char *temp2;
|
---|
382 |
|
---|
383 | if (*str_k == ',' && !i++) {
|
---|
384 | str_k++;
|
---|
385 | break;
|
---|
386 | } /* no else needed: fall through to syntax error
|
---|
387 | because comma isn't in OPT_STR */
|
---|
388 | temp2 = strchr(OPT_STR, *str_k);
|
---|
389 | if (!temp2)
|
---|
390 | bb_error_msg_and_die("unknown key option");
|
---|
391 | flag = 1 << (temp2 - OPT_STR);
|
---|
392 | if (flag & ~FLAG_allowed_for_k)
|
---|
393 | bb_error_msg_and_die("unknown sort type");
|
---|
394 | /* b after ',' means strip _trailing_ space */
|
---|
395 | if (i && flag == FLAG_b)
|
---|
396 | flag = FLAG_bb;
|
---|
397 | key->flags |= flag;
|
---|
398 | str_k++;
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 | #endif
|
---|
403 |
|
---|
404 | /* Open input files and read data */
|
---|
405 | argv += optind;
|
---|
406 | if (!*argv)
|
---|
407 | *--argv = (char*)"-";
|
---|
408 | linecount = 0;
|
---|
409 | lines = NULL;
|
---|
410 | do {
|
---|
411 | /* coreutils 6.9 compat: abort on first open error,
|
---|
412 | * do not continue to next file: */
|
---|
413 | FILE *fp = xfopen_stdin(*argv);
|
---|
414 | for (;;) {
|
---|
415 | line = GET_LINE(fp);
|
---|
416 | if (!line)
|
---|
417 | break;
|
---|
418 | lines = xrealloc_vector(lines, 6, linecount);
|
---|
419 | lines[linecount++] = line;
|
---|
420 | }
|
---|
421 | fclose_if_not_stdin(fp);
|
---|
422 | } while (*++argv);
|
---|
423 |
|
---|
424 | #if ENABLE_FEATURE_SORT_BIG
|
---|
425 | /* if no key, perform alphabetic sort */
|
---|
426 | if (!key_list)
|
---|
427 | add_key()->range[0] = 1;
|
---|
428 | /* handle -c */
|
---|
429 | if (option_mask32 & FLAG_c) {
|
---|
430 | int j = (option_mask32 & FLAG_u) ? -1 : 0;
|
---|
431 | for (i = 1; i < linecount; i++) {
|
---|
432 | if (compare_keys(&lines[i-1], &lines[i]) > j) {
|
---|
433 | fprintf(stderr, "Check line %u\n", i);
|
---|
434 | return EXIT_FAILURE;
|
---|
435 | }
|
---|
436 | }
|
---|
437 | return EXIT_SUCCESS;
|
---|
438 | }
|
---|
439 | #endif
|
---|
440 | /* Perform the actual sort */
|
---|
441 | qsort(lines, linecount, sizeof(lines[0]), compare_keys);
|
---|
442 | /* handle -u */
|
---|
443 | if (option_mask32 & FLAG_u) {
|
---|
444 | flag = 0;
|
---|
445 | /* coreutils 6.3 drop lines for which only key is the same */
|
---|
446 | /* -- disabling last-resort compare... */
|
---|
447 | option_mask32 |= FLAG_s;
|
---|
448 | for (i = 1; i < linecount; i++) {
|
---|
449 | if (compare_keys(&lines[flag], &lines[i]) == 0)
|
---|
450 | free(lines[i]);
|
---|
451 | else
|
---|
452 | lines[++flag] = lines[i];
|
---|
453 | }
|
---|
454 | if (linecount)
|
---|
455 | linecount = flag+1;
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* Print it */
|
---|
459 | #if ENABLE_FEATURE_SORT_BIG
|
---|
460 | /* Open output file _after_ we read all input ones */
|
---|
461 | if (option_mask32 & FLAG_o)
|
---|
462 | xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
|
---|
463 | #endif
|
---|
464 | flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
|
---|
465 | for (i = 0; i < linecount; i++)
|
---|
466 | printf("%s%c", lines[i], flag);
|
---|
467 |
|
---|
468 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
469 | }
|
---|