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