1 /* vi: set sw=4 ts=4: */
3 * SuS3 compliant sort implementation for busybox
5 * Copyright (C) 2004 by Rob Landley <rob@landley.net>
7 * MAINTAINER: Rob Landley <rob@landley.net>
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 * See SuS3 sort standard at:
12 * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
17 /* This is a NOEXEC applet. Be very careful! */
21 sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
22 sort -c [-bdfinru][-t char][-k keydef][file]
25 /* These are sort types */
26 static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
28 FLAG_n = 1, /* Numeric sort */
29 FLAG_g = 2, /* Sort using strtod() */
30 FLAG_M = 4, /* Sort date */
31 /* ucsz apply to root level only, not keys. b at root level implies bb */
32 FLAG_u = 8, /* Unique */
33 FLAG_c = 0x10, /* Check: no output, exit(!ordered) */
34 FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */
35 FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */
36 /* These can be applied to search keys, the previous four can't */
37 FLAG_b = 0x80, /* Ignore leading blanks */
38 FLAG_r = 0x100, /* Reverse */
39 FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */
40 FLAG_f = 0x400, /* Force uppercase */
41 FLAG_i = 0x800, /* Ignore !isprint() */
42 FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */
43 FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */
44 FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */
48 FLAG_bb = 0x80000000, /* Ignore trailing blanks */
51 #if ENABLE_FEATURE_SORT_BIG
52 static char key_separator;
54 static struct sort_key {
55 struct sort_key *next_key; /* linked list */
56 unsigned range[4]; /* start word, start char, end word, end char */
60 static char *get_key(char *str, struct sort_key *key, int flags)
62 int start = 0, end = 0, len, j;
65 /* Special case whole string, so we don't have to make a copy */
66 if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
67 && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
72 /* Find start of key on first pass, end on second pass */
74 for (j = 0; j < 2; j++) {
77 /* Loop through fields */
80 for (i = 1; i < key->range[2*j] + j; i++) {
82 /* Skip body of key and separator */
84 if (str[end++] == key_separator)
88 /* Skip leading blanks */
89 while (isspace(str[end]))
91 /* Skip body of key */
93 if (isspace(str[end]))
102 /* Strip leading whitespace if necessary */
103 //XXX: skip_whitespace()
105 while (isspace(str[start])) start++;
106 /* Strip trailing whitespace if necessary */
108 while (end > start && isspace(str[end-1])) end--;
109 /* Handle offsets on start and end */
111 end += key->range[3] - 1;
112 if (end > len) end = len;
115 start += key->range[1] - 1;
116 if (start > len) start = len;
119 if (end < start) end = start;
120 str = xstrndup(str+start, end-start);
122 if (flags & FLAG_d) {
123 for (start = end = 0; str[end]; end++)
124 if (isspace(str[end]) || isalnum(str[end]))
125 str[start++] = str[end];
129 if (flags & FLAG_i) {
130 for (start = end = 0; str[end]; end++)
131 if (isprint_asciionly(str[end]))
132 str[start++] = str[end];
137 for (i = 0; str[i]; i++)
138 str[i] = toupper(str[i]);
143 static struct sort_key *add_key(void)
145 struct sort_key **pkey = &key_list;
147 pkey = &((*pkey)->next_key);
148 return *pkey = xzalloc(sizeof(struct sort_key));
151 #define GET_LINE(fp) \
152 ((option_mask32 & FLAG_z) \
153 ? bb_get_chunk_from_file(fp, NULL) \
154 : xmalloc_fgetline(fp))
156 #define GET_LINE(fp) xmalloc_fgetline(fp)
159 /* Iterate through keys list and perform comparisons */
160 static int compare_keys(const void *xarg, const void *yarg)
162 int flags = option_mask32, retval = 0;
165 #if ENABLE_FEATURE_SORT_BIG
166 struct sort_key *key;
168 for (key = key_list; !retval && key; key = key->next_key) {
169 flags = key->flags ? key->flags : option_mask32;
170 /* Chop out and modify key chunks, handling -dfib */
171 x = get_key(*(char **)xarg, key, flags);
172 y = get_key(*(char **)yarg, key, flags);
174 /* This curly bracket serves no purpose but to match the nesting
175 level of the for () loop we're not using */
180 /* Perform actual comparison */
183 bb_error_msg_and_die("unknown sort type");
187 #if ENABLE_LOCALE_SUPPORT
188 retval = strcoll(x, y);
190 retval = strcmp(x, y);
193 #if ENABLE_FEATURE_SORT_BIG
196 double dx = strtod(x, &xx);
197 double dy = strtod(y, &yy);
198 /* not numbers < NaN < -infinity < numbers < +infinity) */
200 retval = (y == yy ? 0 : -1);
203 /* Check for isnan */
205 retval = (dy != dy) ? 0 : -1;
208 /* Check for infinity. Could underflow, but it avoids libm. */
209 else if (1.0 / dx == 0.0) {
211 retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
213 retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
214 } else if (1.0 / dy == 0.0)
215 retval = (dy < 0) ? 1 : -1;
217 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
225 xx = strptime(x, "%b", &thyme);
227 yy = strptime(y, "%b", &thyme);
229 retval = (!yy) ? 0 : -1;
233 retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
236 /* Full floating point version of -n */
240 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
244 /* Free key copies. */
245 if (x != *(char **)xarg) free(x);
246 if (y != *(char **)yarg) free(y);
247 /* if (retval) break; - done by for () anyway */
249 /* Integer version of -n for tiny systems */
251 retval = atoi(x) - atoi(y);
257 /* Perform fallback sort if necessary */
258 if (!retval && !(option_mask32 & FLAG_s))
259 retval = strcmp(*(char **)xarg, *(char **)yarg);
261 if (flags & FLAG_r) return -retval;
265 #if ENABLE_FEATURE_SORT_BIG
266 static unsigned str2u(char **str)
269 if (!isdigit((*str)[0]))
270 bb_error_msg_and_die("bad field specification");
271 lu = strtoul(*str, str, 10);
272 if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu)
273 bb_error_msg_and_die("bad field specification");
278 int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
279 int sort_main(int argc UNUSED_PARAM, char **argv)
282 char *str_ignored, *str_o, *str_t;
283 llist_t *lst_k = NULL;
288 xfunc_error_retval = 2;
290 /* Parse command line options */
291 /* -o and -t can be given at most once */
292 opt_complementary = "o--o:t--t:" /* -t, -o: at most one of each */
293 "k::"; /* -k takes list */
294 opts = getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
295 /* global b strips leading and trailing spaces */
297 option_mask32 |= FLAG_bb;
298 #if ENABLE_FEATURE_SORT_BIG
300 if (!str_t[0] || str_t[1])
301 bb_error_msg_and_die("bad -t parameter");
302 key_separator = str_t[0];
304 /* note: below this point we use option_mask32, not opts,
305 * since that reduces register pressure and makes code smaller */
311 FLAG_n | /* Numeric sort */
312 FLAG_g | /* Sort using strtod() */
313 FLAG_M | /* Sort date */
314 FLAG_b | /* Ignore leading blanks */
315 FLAG_r | /* Reverse */
316 FLAG_d | /* Ignore !(isalnum()|isspace()) */
317 FLAG_f | /* Force uppercase */
318 FLAG_i | /* Ignore !isprint() */
321 struct sort_key *key = add_key();
322 char *str_k = llist_pop(&lst_k);
324 i = 0; /* i==0 before comma, 1 after (-k3,6) */
327 /* Cannot use bb_strtou - suffix can be a letter */
328 key->range[2*i] = str2u(&str_k);
331 key->range[2*i+1] = str2u(&str_k);
336 if (*str_k == ',' && !i++) {
339 } /* no else needed: fall through to syntax error
340 because comma isn't in OPT_STR */
341 temp2 = strchr(OPT_STR, *str_k);
343 bb_error_msg_and_die("unknown key option");
344 flag = 1 << (temp2 - OPT_STR);
345 if (flag & ~FLAG_allowed_for_k)
346 bb_error_msg_and_die("unknown sort type");
347 /* b after ',' means strip _trailing_ space */
348 if (i && flag == FLAG_b)
357 /* Open input files and read data */
360 *--argv = (char*)"-";
364 /* coreutils 6.9 compat: abort on first open error,
365 * do not continue to next file: */
366 FILE *fp = xfopen_stdin(*argv);
371 lines = xrealloc_vector(lines, 6, linecount);
372 lines[linecount++] = line;
374 fclose_if_not_stdin(fp);
377 #if ENABLE_FEATURE_SORT_BIG
378 /* if no key, perform alphabetic sort */
380 add_key()->range[0] = 1;
382 if (option_mask32 & FLAG_c) {
383 int j = (option_mask32 & FLAG_u) ? -1 : 0;
384 for (i = 1; i < linecount; i++) {
385 if (compare_keys(&lines[i-1], &lines[i]) > j) {
386 fprintf(stderr, "Check line %u\n", i);
393 /* Perform the actual sort */
394 qsort(lines, linecount, sizeof(lines[0]), compare_keys);
396 if (option_mask32 & FLAG_u) {
398 /* coreutils 6.3 drop lines for which only key is the same */
399 /* -- disabling last-resort compare... */
400 option_mask32 |= FLAG_s;
401 for (i = 1; i < linecount; i++) {
402 if (compare_keys(&lines[flag], &lines[i]) == 0)
405 lines[++flag] = lines[i];
412 #if ENABLE_FEATURE_SORT_BIG
413 /* Open output file _after_ we read all input ones */
414 if (option_mask32 & FLAG_o)
415 xmove_fd(xopen3(str_o, O_WRONLY, 0666), STDOUT_FILENO);
417 flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
418 for (i = 0; i < linecount; i++)
419 printf("%s%c", lines[i], flag);
421 fflush_stdout_and_exit(EXIT_SUCCESS);