[821] | 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>
|
---|
[1765] | 8 | *
|
---|
[2725] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 10 | *
|
---|
| 11 | * See SuS3 sort standard at:
|
---|
| 12 | * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[1765] | 15 | #include "libbb.h"
|
---|
[821] | 16 |
|
---|
[1765] | 17 | /* This is a NOEXEC applet. Be very careful! */
|
---|
[821] | 18 |
|
---|
[1765] | 19 |
|
---|
[821] | 20 | /*
|
---|
| 21 | sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
|
---|
| 22 | sort -c [-bdfinru][-t char][-k keydef][file]
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | /* These are sort types */
|
---|
[1765] | 26 | static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
|
---|
| 27 | enum {
|
---|
| 28 | FLAG_n = 1, /* Numeric sort */
|
---|
| 29 | FLAG_g = 2, /* Sort using strtod() */
|
---|
| 30 | FLAG_M = 4, /* Sort date */
|
---|
[821] | 31 | /* ucsz apply to root level only, not keys. b at root level implies bb */
|
---|
[1765] | 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 */
|
---|
[2725] | 35 | FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */
|
---|
[821] | 36 | /* These can be applied to search keys, the previous four can't */
|
---|
[1765] | 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 */
|
---|
| 45 | FLAG_o = 0x8000,
|
---|
| 46 | FLAG_k = 0x10000,
|
---|
| 47 | FLAG_t = 0x20000,
|
---|
| 48 | FLAG_bb = 0x80000000, /* Ignore trailing blanks */
|
---|
| 49 | };
|
---|
[821] | 50 |
|
---|
[1765] | 51 | #if ENABLE_FEATURE_SORT_BIG
|
---|
[821] | 52 | static char key_separator;
|
---|
| 53 |
|
---|
[1765] | 54 | static struct sort_key {
|
---|
[2725] | 55 | struct sort_key *next_key; /* linked list */
|
---|
| 56 | unsigned range[4]; /* start word, start char, end word, end char */
|
---|
[1765] | 57 | unsigned flags;
|
---|
[821] | 58 | } *key_list;
|
---|
| 59 |
|
---|
| 60 | static char *get_key(char *str, struct sort_key *key, int flags)
|
---|
| 61 | {
|
---|
[2725] | 62 | int start = 0, end = 0, len, j;
|
---|
| 63 | unsigned i;
|
---|
[821] | 64 |
|
---|
| 65 | /* Special case whole string, so we don't have to make a copy */
|
---|
[1765] | 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))
|
---|
| 68 | ) {
|
---|
| 69 | return str;
|
---|
| 70 | }
|
---|
[821] | 71 |
|
---|
[1765] | 72 | /* Find start of key on first pass, end on second pass */
|
---|
| 73 | len = strlen(str);
|
---|
| 74 | for (j = 0; j < 2; j++) {
|
---|
| 75 | if (!key->range[2*j])
|
---|
| 76 | end = len;
|
---|
[821] | 77 | /* Loop through fields */
|
---|
| 78 | else {
|
---|
[1765] | 79 | end = 0;
|
---|
| 80 | for (i = 1; i < key->range[2*j] + j; i++) {
|
---|
| 81 | if (key_separator) {
|
---|
| 82 | /* Skip body of key and separator */
|
---|
| 83 | while (str[end]) {
|
---|
| 84 | if (str[end++] == key_separator)
|
---|
| 85 | break;
|
---|
| 86 | }
|
---|
| 87 | } else {
|
---|
| 88 | /* Skip leading blanks */
|
---|
| 89 | while (isspace(str[end]))
|
---|
| 90 | end++;
|
---|
| 91 | /* Skip body of key */
|
---|
| 92 | while (str[end]) {
|
---|
| 93 | if (isspace(str[end]))
|
---|
| 94 | break;
|
---|
| 95 | end++;
|
---|
| 96 | }
|
---|
[821] | 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[1765] | 100 | if (!j) start = end;
|
---|
[821] | 101 | }
|
---|
| 102 | /* Strip leading whitespace if necessary */
|
---|
[1765] | 103 | //XXX: skip_whitespace()
|
---|
| 104 | if (flags & FLAG_b)
|
---|
| 105 | while (isspace(str[start])) start++;
|
---|
[821] | 106 | /* Strip trailing whitespace if necessary */
|
---|
[1765] | 107 | if (flags & FLAG_bb)
|
---|
| 108 | while (end > start && isspace(str[end-1])) end--;
|
---|
[821] | 109 | /* Handle offsets on start and end */
|
---|
[1765] | 110 | if (key->range[3]) {
|
---|
| 111 | end += key->range[3] - 1;
|
---|
| 112 | if (end > len) end = len;
|
---|
[821] | 113 | }
|
---|
[1765] | 114 | if (key->range[1]) {
|
---|
| 115 | start += key->range[1] - 1;
|
---|
| 116 | if (start > len) start = len;
|
---|
[821] | 117 | }
|
---|
| 118 | /* Make the copy */
|
---|
[1765] | 119 | if (end < start) end = start;
|
---|
| 120 | str = xstrndup(str+start, end-start);
|
---|
[821] | 121 | /* Handle -d */
|
---|
[1765] | 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];
|
---|
| 126 | str[start] = '\0';
|
---|
[821] | 127 | }
|
---|
| 128 | /* Handle -i */
|
---|
[1765] | 129 | if (flags & FLAG_i) {
|
---|
| 130 | for (start = end = 0; str[end]; end++)
|
---|
[2725] | 131 | if (isprint_asciionly(str[end]))
|
---|
[1765] | 132 | str[start++] = str[end];
|
---|
| 133 | str[start] = '\0';
|
---|
[821] | 134 | }
|
---|
| 135 | /* Handle -f */
|
---|
[1765] | 136 | if (flags & FLAG_f)
|
---|
| 137 | for (i = 0; str[i]; i++)
|
---|
| 138 | str[i] = toupper(str[i]);
|
---|
[821] | 139 |
|
---|
| 140 | return str;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | static struct sort_key *add_key(void)
|
---|
| 144 | {
|
---|
[1765] | 145 | struct sort_key **pkey = &key_list;
|
---|
| 146 | while (*pkey)
|
---|
| 147 | pkey = &((*pkey)->next_key);
|
---|
| 148 | return *pkey = xzalloc(sizeof(struct sort_key));
|
---|
[821] | 149 | }
|
---|
| 150 |
|
---|
[1765] | 151 | #define GET_LINE(fp) \
|
---|
| 152 | ((option_mask32 & FLAG_z) \
|
---|
| 153 | ? bb_get_chunk_from_file(fp, NULL) \
|
---|
[2725] | 154 | : xmalloc_fgetline(fp))
|
---|
[821] | 155 | #else
|
---|
[2725] | 156 | #define GET_LINE(fp) xmalloc_fgetline(fp)
|
---|
[821] | 157 | #endif
|
---|
| 158 |
|
---|
| 159 | /* Iterate through keys list and perform comparisons */
|
---|
| 160 | static int compare_keys(const void *xarg, const void *yarg)
|
---|
| 161 | {
|
---|
[1765] | 162 | int flags = option_mask32, retval = 0;
|
---|
| 163 | char *x, *y;
|
---|
[821] | 164 |
|
---|
[1765] | 165 | #if ENABLE_FEATURE_SORT_BIG
|
---|
[821] | 166 | struct sort_key *key;
|
---|
| 167 |
|
---|
[1765] | 168 | for (key = key_list; !retval && key; key = key->next_key) {
|
---|
| 169 | flags = key->flags ? key->flags : option_mask32;
|
---|
[821] | 170 | /* Chop out and modify key chunks, handling -dfib */
|
---|
[1765] | 171 | x = get_key(*(char **)xarg, key, flags);
|
---|
| 172 | y = get_key(*(char **)yarg, key, flags);
|
---|
[821] | 173 | #else
|
---|
| 174 | /* This curly bracket serves no purpose but to match the nesting
|
---|
[1765] | 175 | level of the for () loop we're not using */
|
---|
[821] | 176 | {
|
---|
[1765] | 177 | x = *(char **)xarg;
|
---|
| 178 | y = *(char **)yarg;
|
---|
[821] | 179 | #endif
|
---|
| 180 | /* Perform actual comparison */
|
---|
[1765] | 181 | switch (flags & 7) {
|
---|
| 182 | default:
|
---|
| 183 | bb_error_msg_and_die("unknown sort type");
|
---|
| 184 | break;
|
---|
| 185 | /* Ascii sort */
|
---|
| 186 | case 0:
|
---|
| 187 | #if ENABLE_LOCALE_SUPPORT
|
---|
| 188 | retval = strcoll(x, y);
|
---|
| 189 | #else
|
---|
| 190 | retval = strcmp(x, y);
|
---|
| 191 | #endif
|
---|
| 192 | break;
|
---|
| 193 | #if ENABLE_FEATURE_SORT_BIG
|
---|
| 194 | case FLAG_g: {
|
---|
| 195 | char *xx, *yy;
|
---|
| 196 | double dx = strtod(x, &xx);
|
---|
| 197 | double dy = strtod(y, &yy);
|
---|
| 198 | /* not numbers < NaN < -infinity < numbers < +infinity) */
|
---|
| 199 | if (x == xx)
|
---|
| 200 | retval = (y == yy ? 0 : -1);
|
---|
| 201 | else if (y == yy)
|
---|
| 202 | retval = 1;
|
---|
| 203 | /* Check for isnan */
|
---|
| 204 | else if (dx != dx)
|
---|
| 205 | retval = (dy != dy) ? 0 : -1;
|
---|
| 206 | else if (dy != dy)
|
---|
| 207 | retval = 1;
|
---|
| 208 | /* Check for infinity. Could underflow, but it avoids libm. */
|
---|
| 209 | else if (1.0 / dx == 0.0) {
|
---|
| 210 | if (dx < 0)
|
---|
| 211 | retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
|
---|
| 212 | else
|
---|
| 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;
|
---|
| 216 | else
|
---|
| 217 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | case FLAG_M: {
|
---|
| 221 | struct tm thyme;
|
---|
| 222 | int dx;
|
---|
| 223 | char *xx, *yy;
|
---|
[821] | 224 |
|
---|
[1765] | 225 | xx = strptime(x, "%b", &thyme);
|
---|
| 226 | dx = thyme.tm_mon;
|
---|
| 227 | yy = strptime(y, "%b", &thyme);
|
---|
| 228 | if (!xx)
|
---|
| 229 | retval = (!yy) ? 0 : -1;
|
---|
| 230 | else if (!yy)
|
---|
| 231 | retval = 1;
|
---|
| 232 | else
|
---|
| 233 | retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
|
---|
| 234 | break;
|
---|
[821] | 235 | }
|
---|
[1765] | 236 | /* Full floating point version of -n */
|
---|
| 237 | case FLAG_n: {
|
---|
| 238 | double dx = atof(x);
|
---|
| 239 | double dy = atof(y);
|
---|
| 240 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
---|
| 241 | break;
|
---|
| 242 | }
|
---|
| 243 | } /* switch */
|
---|
[821] | 244 | /* Free key copies. */
|
---|
[1765] | 245 | if (x != *(char **)xarg) free(x);
|
---|
| 246 | if (y != *(char **)yarg) free(y);
|
---|
| 247 | /* if (retval) break; - done by for () anyway */
|
---|
[821] | 248 | #else
|
---|
[1765] | 249 | /* Integer version of -n for tiny systems */
|
---|
| 250 | case FLAG_n:
|
---|
| 251 | retval = atoi(x) - atoi(y);
|
---|
| 252 | break;
|
---|
| 253 | } /* switch */
|
---|
[821] | 254 | #endif
|
---|
[1765] | 255 | } /* for */
|
---|
| 256 |
|
---|
[821] | 257 | /* Perform fallback sort if necessary */
|
---|
[1765] | 258 | if (!retval && !(option_mask32 & FLAG_s))
|
---|
| 259 | retval = strcmp(*(char **)xarg, *(char **)yarg);
|
---|
| 260 |
|
---|
| 261 | if (flags & FLAG_r) return -retval;
|
---|
| 262 | return retval;
|
---|
[821] | 263 | }
|
---|
| 264 |
|
---|
[1765] | 265 | #if ENABLE_FEATURE_SORT_BIG
|
---|
| 266 | static unsigned str2u(char **str)
|
---|
| 267 | {
|
---|
| 268 | unsigned long lu;
|
---|
| 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");
|
---|
| 274 | return lu;
|
---|
| 275 | }
|
---|
| 276 | #endif
|
---|
| 277 |
|
---|
[2725] | 278 | int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 279 | int sort_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 280 | {
|
---|
[2725] | 281 | char *line, **lines;
|
---|
[1765] | 282 | char *str_ignored, *str_o, *str_t;
|
---|
| 283 | llist_t *lst_k = NULL;
|
---|
| 284 | int i, flag;
|
---|
[2725] | 285 | int linecount;
|
---|
| 286 | unsigned opts;
|
---|
[821] | 287 |
|
---|
[1765] | 288 | xfunc_error_retval = 2;
|
---|
| 289 |
|
---|
[821] | 290 | /* Parse command line options */
|
---|
[1765] | 291 | /* -o and -t can be given at most once */
|
---|
[2725] | 292 | opt_complementary = "o--o:t--t:" /* -t, -o: at most one of each */
|
---|
[1765] | 293 | "k::"; /* -k takes list */
|
---|
[2725] | 294 | opts = getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
|
---|
| 295 | /* global b strips leading and trailing spaces */
|
---|
| 296 | if (opts & FLAG_b)
|
---|
| 297 | option_mask32 |= FLAG_bb;
|
---|
[1765] | 298 | #if ENABLE_FEATURE_SORT_BIG
|
---|
[2725] | 299 | if (opts & FLAG_t) {
|
---|
[1765] | 300 | if (!str_t[0] || str_t[1])
|
---|
| 301 | bb_error_msg_and_die("bad -t parameter");
|
---|
| 302 | key_separator = str_t[0];
|
---|
| 303 | }
|
---|
[2725] | 304 | /* note: below this point we use option_mask32, not opts,
|
---|
| 305 | * since that reduces register pressure and makes code smaller */
|
---|
| 306 |
|
---|
[1765] | 307 | /* parse sort key */
|
---|
| 308 | while (lst_k) {
|
---|
| 309 | enum {
|
---|
| 310 | FLAG_allowed_for_k =
|
---|
| 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() */
|
---|
| 319 | 0
|
---|
| 320 | };
|
---|
| 321 | struct sort_key *key = add_key();
|
---|
[2725] | 322 | char *str_k = llist_pop(&lst_k);
|
---|
[821] | 323 |
|
---|
[1765] | 324 | i = 0; /* i==0 before comma, 1 after (-k3,6) */
|
---|
| 325 | while (*str_k) {
|
---|
| 326 | /* Start of range */
|
---|
| 327 | /* Cannot use bb_strtou - suffix can be a letter */
|
---|
| 328 | key->range[2*i] = str2u(&str_k);
|
---|
| 329 | if (*str_k == '.') {
|
---|
| 330 | str_k++;
|
---|
| 331 | key->range[2*i+1] = str2u(&str_k);
|
---|
[821] | 332 | }
|
---|
[1765] | 333 | while (*str_k) {
|
---|
[2725] | 334 | const char *temp2;
|
---|
| 335 |
|
---|
[1765] | 336 | if (*str_k == ',' && !i++) {
|
---|
| 337 | str_k++;
|
---|
| 338 | break;
|
---|
| 339 | } /* no else needed: fall through to syntax error
|
---|
[2725] | 340 | because comma isn't in OPT_STR */
|
---|
[1765] | 341 | temp2 = strchr(OPT_STR, *str_k);
|
---|
| 342 | if (!temp2)
|
---|
| 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 */
|
---|
[2725] | 348 | if (i && flag == FLAG_b)
|
---|
| 349 | flag = FLAG_bb;
|
---|
[1765] | 350 | key->flags |= flag;
|
---|
| 351 | str_k++;
|
---|
| 352 | }
|
---|
[821] | 353 | }
|
---|
| 354 | }
|
---|
[1765] | 355 | #endif
|
---|
| 356 |
|
---|
[821] | 357 | /* Open input files and read data */
|
---|
[2725] | 358 | argv += optind;
|
---|
| 359 | if (!*argv)
|
---|
| 360 | *--argv = (char*)"-";
|
---|
| 361 | linecount = 0;
|
---|
| 362 | lines = NULL;
|
---|
| 363 | do {
|
---|
| 364 | /* coreutils 6.9 compat: abort on first open error,
|
---|
| 365 | * do not continue to next file: */
|
---|
| 366 | FILE *fp = xfopen_stdin(*argv);
|
---|
[1765] | 367 | for (;;) {
|
---|
| 368 | line = GET_LINE(fp);
|
---|
[2725] | 369 | if (!line)
|
---|
| 370 | break;
|
---|
| 371 | lines = xrealloc_vector(lines, 6, linecount);
|
---|
[1765] | 372 | lines[linecount++] = line;
|
---|
[821] | 373 | }
|
---|
[2725] | 374 | fclose_if_not_stdin(fp);
|
---|
| 375 | } while (*++argv);
|
---|
| 376 |
|
---|
[1765] | 377 | #if ENABLE_FEATURE_SORT_BIG
|
---|
[821] | 378 | /* if no key, perform alphabetic sort */
|
---|
[1765] | 379 | if (!key_list)
|
---|
| 380 | add_key()->range[0] = 1;
|
---|
[821] | 381 | /* handle -c */
|
---|
[1765] | 382 | if (option_mask32 & FLAG_c) {
|
---|
| 383 | int j = (option_mask32 & FLAG_u) ? -1 : 0;
|
---|
[2725] | 384 | for (i = 1; i < linecount; i++) {
|
---|
[1765] | 385 | if (compare_keys(&lines[i-1], &lines[i]) > j) {
|
---|
[2725] | 386 | fprintf(stderr, "Check line %u\n", i);
|
---|
| 387 | return EXIT_FAILURE;
|
---|
[821] | 388 | }
|
---|
[2725] | 389 | }
|
---|
| 390 | return EXIT_SUCCESS;
|
---|
[821] | 391 | }
|
---|
| 392 | #endif
|
---|
| 393 | /* Perform the actual sort */
|
---|
[2725] | 394 | qsort(lines, linecount, sizeof(lines[0]), compare_keys);
|
---|
[821] | 395 | /* handle -u */
|
---|
[1765] | 396 | if (option_mask32 & FLAG_u) {
|
---|
| 397 | flag = 0;
|
---|
| 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++) {
|
---|
[2725] | 402 | if (compare_keys(&lines[flag], &lines[i]) == 0)
|
---|
[1765] | 403 | free(lines[i]);
|
---|
| 404 | else
|
---|
| 405 | lines[++flag] = lines[i];
|
---|
[821] | 406 | }
|
---|
[2725] | 407 | if (linecount)
|
---|
| 408 | linecount = flag+1;
|
---|
[821] | 409 | }
|
---|
[2725] | 410 |
|
---|
[821] | 411 | /* Print it */
|
---|
[2725] | 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(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
|
---|
| 416 | #endif
|
---|
| 417 | flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
|
---|
[1765] | 418 | for (i = 0; i < linecount; i++)
|
---|
[2725] | 419 | printf("%s%c", lines[i], flag);
|
---|
[1765] | 420 |
|
---|
| 421 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
[821] | 422 | }
|
---|