1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91#include "libbb.h"
92
93
94enum {
95 FLAG_n = 1 << 0,
96 FLAG_g = 1 << 1,
97 FLAG_M = 1 << 2,
98 FLAG_V = 1 << 3,
99
100 FLAG_u = 1 << 4,
101 FLAG_c = 1 << 5,
102 FLAG_s = 1 << 6,
103 FLAG_z = 1 << 7,
104
105 FLAG_b = 1 << 8,
106 FLAG_r = 1 << 9,
107 FLAG_d = 1 << 10,
108 FLAG_f = 1 << 11,
109 FLAG_i = 1 << 12,
110 FLAG_m = 1 << 13,
111 FLAG_S = 1 << 14,
112 FLAG_T = 1 << 15,
113 FLAG_o = 1 << 16,
114 FLAG_k = 1 << 17,
115 FLAG_t = 1 << 18,
116 FLAG_bb = 0x80000000,
117 FLAG_no_tie_break = 0x40000000,
118};
119
120static const char sort_opt_str[] ALIGN1 = "^"
121 "ngMVucszbrdfimS:T:o:k:*t:"
122 "\0" "o--o:t--t";
123
124
125
126
127#define OPT_STR (sort_opt_str + 1)
128
129#if ENABLE_FEATURE_SORT_BIG
130static char key_separator;
131
132static struct sort_key {
133 struct sort_key *next_key;
134 unsigned range[4];
135 unsigned flags;
136} *key_list;
137
138
139
140
141
142static char *get_key(char *str, struct sort_key *key, int flags)
143{
144 int start = start;
145 int end;
146 int len, j;
147 unsigned i;
148
149
150 if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
151 && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
152 ) {
153 return str;
154 }
155
156
157 len = strlen(str);
158 for (j = 0; j < 2; j++) {
159 if (!key->range[2*j])
160 end = len;
161
162 else {
163 unsigned char ch = 0;
164
165 end = 0;
166 for (i = 1; i < key->range[2*j] + j; i++) {
167 if (key_separator) {
168
169 while ((ch = str[end]) != '\0') {
170 end++;
171 if (ch == key_separator)
172 break;
173 }
174 } else {
175
176 while (isspace(str[end]))
177 end++;
178
179 while (str[end] != '\0') {
180 if (isspace(str[end]))
181 break;
182 end++;
183 }
184 }
185 }
186
187 if (j && ch) {
188
189
190
191
192 end--;
193 }
194 }
195 if (!j) start = end;
196 }
197
198 if (flags & FLAG_b)
199
200 while (isspace(str[start])) start++;
201
202 if (flags & FLAG_bb)
203 while (end > start && isspace(str[end-1])) end--;
204
205 if (key->range[3]) {
206 end = key->range[3];
207 if (end > len) end = len;
208 }
209
210 if (key->range[1]) {
211 start += key->range[1] - 1;
212 if (start > len) start = len;
213 }
214
215 if (end < start)
216 end = start;
217 str = xstrndup(str+start, end-start);
218
219 if (flags & FLAG_d) {
220 for (start = end = 0; str[end]; end++)
221 if (isspace(str[end]) || isalnum(str[end]))
222 str[start++] = str[end];
223 str[start] = '\0';
224 }
225
226 if (flags & FLAG_i) {
227 for (start = end = 0; str[end]; end++)
228 if (isprint_asciionly(str[end]))
229 str[start++] = str[end];
230 str[start] = '\0';
231 }
232
233 if (flags & FLAG_f)
234 for (i = 0; str[i]; i++)
235 str[i] = toupper(str[i]);
236
237 return str;
238}
239
240static struct sort_key *add_key(void)
241{
242 struct sort_key **pkey = &key_list;
243 while (*pkey)
244 pkey = &((*pkey)->next_key);
245 return *pkey = xzalloc(sizeof(struct sort_key));
246}
247
248#define GET_LINE(fp) \
249 ((option_mask32 & FLAG_z) \
250 ? bb_get_chunk_from_file(fp, NULL) \
251 : xmalloc_fgetline(fp))
252#else
253#define GET_LINE(fp) xmalloc_fgetline(fp)
254#endif
255
256
257static int compare_keys(const void *xarg, const void *yarg)
258{
259 int flags = option_mask32, retval = 0;
260 char *x, *y;
261
262#if ENABLE_FEATURE_SORT_BIG
263 struct sort_key *key;
264
265 for (key = key_list; !retval && key; key = key->next_key) {
266 flags = key->flags ? key->flags : option_mask32;
267
268 x = get_key(*(char **)xarg, key, flags);
269 y = get_key(*(char **)yarg, key, flags);
270#else
271
272
273 {
274 x = *(char **)xarg;
275 y = *(char **)yarg;
276#endif
277
278 switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) {
279 default:
280 bb_simple_error_msg_and_die("unknown sort type");
281 break;
282#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
283 case FLAG_V:
284 retval = strverscmp(x, y);
285 break;
286#endif
287
288 case 0:
289#if ENABLE_LOCALE_SUPPORT
290 retval = strcoll(x, y);
291#else
292 retval = strcmp(x, y);
293#endif
294 break;
295#if ENABLE_FEATURE_SORT_BIG
296 case FLAG_g: {
297 char *xx, *yy;
298
299 double dx = strtod(x, &xx);
300 double dy = strtod(y, &yy);
301
302 if (x == xx)
303 retval = (y == yy ? 0 : -1);
304 else if (y == yy)
305 retval = 1;
306
307 else if (dx != dx)
308 retval = (dy != dy) ? 0 : -1;
309 else if (dy != dy)
310 retval = 1;
311
312 else if (1.0 / dx == 0.0) {
313 if (dx < 0)
314 retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
315 else
316 retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
317 } else if (1.0 / dy == 0.0)
318 retval = (dy < 0) ? 1 : -1;
319 else
320 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
321 break;
322 }
323 case FLAG_M: {
324 struct tm thyme;
325 int dx;
326 char *xx, *yy;
327
328 xx = strptime(x, "%b", &thyme);
329 dx = thyme.tm_mon;
330 yy = strptime(y, "%b", &thyme);
331 if (!xx)
332 retval = (!yy) ? 0 : -1;
333 else if (!yy)
334 retval = 1;
335 else
336 retval = dx - thyme.tm_mon;
337 break;
338 }
339
340 case FLAG_n: {
341 double dx = atof(x);
342 double dy = atof(y);
343 retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
344 break;
345 }
346 }
347
348 if (x != *(char **)xarg) free(x);
349 if (y != *(char **)yarg) free(y);
350
351#else
352
353 case FLAG_n:
354 retval = atoi(x) - atoi(y);
355 break;
356 }
357#endif
358 }
359
360 if (retval == 0) {
361
362
363 if (option_mask32 & FLAG_s) {
364
365
366
367 uint32_t *p32;
368 uint32_t x32, y32;
369 char *line;
370 unsigned len;
371
372 line = *(char**)xarg;
373 len = (strlen(line) + 4) & (~3u);
374 p32 = (void*)(line + len);
375 x32 = *p32;
376 line = *(char**)yarg;
377 len = (strlen(line) + 4) & (~3u);
378 p32 = (void*)(line + len);
379 y32 = *p32;
380
381
382 retval = (x32 > y32) * 2 - 1;
383 } else
384 if (!(option_mask32 & FLAG_no_tie_break)) {
385
386 flags = option_mask32;
387 retval = strcmp(*(char **)xarg, *(char **)yarg);
388 }
389 }
390
391 if (flags & FLAG_r)
392 return -retval;
393
394 return retval;
395}
396
397#if ENABLE_FEATURE_SORT_BIG
398static unsigned str2u(char **str)
399{
400 unsigned long lu;
401 if (!isdigit((*str)[0]))
402 bb_simple_error_msg_and_die("bad field specification");
403 lu = strtoul(*str, str, 10);
404 if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu)
405 bb_simple_error_msg_and_die("bad field specification");
406 return lu;
407}
408#endif
409
410int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
411int sort_main(int argc UNUSED_PARAM, char **argv)
412{
413 char **lines;
414 char *str_ignored, *str_o, *str_t;
415 llist_t *lst_k = NULL;
416 int i;
417 int linecount;
418 unsigned opts;
419#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
420 bool can_drop_dups;
421 size_t prev_len = 0;
422 char *prev_line = (char*) "";
423
424
425
426 size_t count_to_optimize_dups = 0x3fff;
427#endif
428
429 xfunc_error_retval = 2;
430
431
432 opts = getopt32(argv,
433 sort_opt_str,
434 &str_ignored, &str_ignored, &str_o, &lst_k, &str_t
435 );
436#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 can_drop_dups = ((opts & ~(FLAG_o|FLAG_z|FLAG_r|FLAG_s)) == FLAG_u);
453
454
455
456 if (opts & FLAG_s)
457 count_to_optimize_dups = (size_t)-1L;
458#endif
459
460 if (opts & FLAG_b)
461 option_mask32 |= FLAG_bb;
462#if ENABLE_FEATURE_SORT_BIG
463 if (opts & FLAG_t) {
464 if (!str_t[0] || str_t[1])
465 bb_simple_error_msg_and_die("bad -t parameter");
466 key_separator = str_t[0];
467 }
468
469
470
471
472 while (lst_k) {
473 enum {
474 FLAG_allowed_for_k =
475 FLAG_n |
476 FLAG_g |
477 FLAG_M |
478 FLAG_b |
479 FLAG_r |
480 FLAG_d |
481 FLAG_f |
482 FLAG_i |
483 0
484 };
485 struct sort_key *key = add_key();
486 char *str_k = llist_pop(&lst_k);
487
488 i = 0;
489 while (*str_k) {
490
491
492 key->range[2*i] = str2u(&str_k);
493 if (*str_k == '.') {
494 str_k++;
495 key->range[2*i+1] = str2u(&str_k);
496 }
497 while (*str_k) {
498 int flag;
499 const char *idx;
500
501 if (*str_k == ',' && !i++) {
502 str_k++;
503 break;
504 }
505
506 idx = strchr(OPT_STR, *str_k);
507 if (!idx)
508 bb_simple_error_msg_and_die("unknown key option");
509 flag = 1 << (idx - OPT_STR);
510 if (flag & ~FLAG_allowed_for_k)
511 bb_simple_error_msg_and_die("unknown sort type");
512
513 if (i && flag == FLAG_b)
514 flag = FLAG_bb;
515 key->flags |= flag;
516 str_k++;
517 }
518 }
519 }
520#endif
521
522
523 argv += optind;
524 if (!*argv)
525 *--argv = (char*)"-";
526 linecount = 0;
527 lines = NULL;
528 do {
529
530
531 FILE *fp = xfopen_stdin(*argv);
532 for (;;) {
533 char *line = GET_LINE(fp);
534 if (!line)
535 break;
536
537#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
538 if (count_to_optimize_dups != 0)
539 count_to_optimize_dups--;
540 if (count_to_optimize_dups == 0) {
541 size_t len;
542 char *new_line;
543
544
545
546
547
548
549 len = strlen(line);
550 if (len <= prev_len) {
551 new_line = prev_line + (prev_len - len);
552 if (strcmp(line, new_line) == 0) {
553
554 if (can_drop_dups && prev_len == len) {
555
556 free(line);
557 continue;
558 }
559 free(line);
560 line = new_line;
561
562
563
564 goto skip;
565 }
566 }
567 prev_len = len;
568 prev_line = line;
569 skip: ;
570 }
571#else
572
573#endif
574 lines = xrealloc_vector(lines, 6, linecount);
575 lines[linecount++] = line;
576 }
577 fclose_if_not_stdin(fp);
578 } while (*++argv);
579
580#if ENABLE_FEATURE_SORT_BIG
581
582 if (!key_list)
583 add_key()->range[0] = 1;
584
585 if (option_mask32 & FLAG_c) {
586 int j = (option_mask32 & FLAG_u) ? -1 : 0;
587 for (i = 1; i < linecount; i++) {
588 if (compare_keys(&lines[i-1], &lines[i]) > j) {
589 fprintf(stderr, "Check line %u\n", i);
590 return EXIT_FAILURE;
591 }
592 }
593 return EXIT_SUCCESS;
594 }
595#endif
596
597
598 if (option_mask32 & FLAG_s) {
599 for (i = 0; i < linecount; i++) {
600 uint32_t *p32;
601 char *line;
602 unsigned len;
603
604 line = lines[i];
605 len = (strlen(line) + 4) & (~3u);
606 lines[i] = line = xrealloc(line, len + 4);
607 p32 = (void*)(line + len);
608 *p32 = i;
609 }
610
611
612 }
613
614
615 qsort(lines, linecount, sizeof(lines[0]), compare_keys);
616
617
618 if (option_mask32 & FLAG_u) {
619 int j = 0;
620
621
622
623
624 option_mask32 |= FLAG_no_tie_break;
625 for (i = 1; i < linecount; i++) {
626 if (compare_keys(&lines[j], &lines[i]) == 0)
627 free(lines[i]);
628 else
629 lines[++j] = lines[i];
630 }
631 if (linecount)
632 linecount = j+1;
633 }
634
635
636#if ENABLE_FEATURE_SORT_BIG
637
638 if (option_mask32 & FLAG_o)
639 xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
640#endif
641 {
642 int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
643 for (i = 0; i < linecount; i++)
644 printf("%s%c", lines[i], ch);
645 }
646
647 fflush_stdout_and_exit(EXIT_SUCCESS);
648}
649