Merge branch 'vendor/DHCPCD'
[dragonfly.git] / usr.bin / sort / sort.c
blob8735077e381af3a1e8fd65ba929ab32cad96869e
1 /*-
2 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
3 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $FreeBSD: head/usr.bin/sort/sort.c 281182 2015-04-07 01:17:49Z pfg $
31 #include <sys/stat.h>
32 #include <sys/sysctl.h>
33 #include <sys/types.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <limits.h>
39 #include <locale.h>
40 #include <regex.h>
41 #include <signal.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <wchar.h>
48 #include <wctype.h>
49 #if defined(SORT_RANDOM)
50 #include <openssl/md5.h>
51 #endif
53 #include "coll.h"
54 #include "file.h"
55 #include "sort.h"
57 #ifndef WITHOUT_NLS
58 #include <nl_types.h>
59 nl_catd catalog;
60 #endif
62 #if defined(SORT_RANDOM)
63 #define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz"
64 #else
65 #define OPTIONS "bcCdfghik:Mmno:rsS:t:T:uVz"
66 #endif
68 #if defined(SORT_RANDOM)
69 #define DEFAULT_RANDOM_SORT_SEED_FILE ("/dev/random")
70 #define MAX_DEFAULT_RANDOM_SEED_DATA_SIZE (1024)
72 static bool need_random;
73 static const char *random_source = DEFAULT_RANDOM_SORT_SEED_FILE;
74 static const void *random_seed;
75 static size_t random_seed_size;
77 MD5_CTX md5_ctx;
78 #endif
81 * Default messages to use when NLS is disabled or no catalogue
82 * is found.
84 const char *nlsstr[] = { "",
85 /* 1*/"mutually exclusive flags",
86 /* 2*/"extra argument not allowed with -c",
87 /* 3*/"Unknown feature",
88 /* 4*/"Wrong memory buffer specification",
89 /* 5*/"0 field in key specs",
90 /* 6*/"0 column in key specs",
91 /* 7*/"Wrong file mode",
92 /* 8*/"Cannot open file for reading",
93 /* 9*/"Radix sort cannot be used with these sort options",
94 /*10*/"The chosen sort method cannot be used with stable and/or unique sort",
95 /*11*/"Invalid key position",
96 /*12*/"Usage: %s [-bcCdfigMmnrsuz] [-kPOS1[,POS2] ... ] "
97 "[+POS1 [-POS2]] [-S memsize] [-T tmpdir] [-t separator] "
98 "[-o outfile] [--batch-size size] [--files0-from file] "
99 "[--heapsort] [--mergesort] [--radixsort] [--qsort] "
100 "[--mmap] "
101 #if defined(SORT_THREADS)
102 "[--parallel thread_no] "
103 #endif
104 "[--human-numeric-sort] "
105 #if defined(SORT_RANDOM)
106 "[--version-sort] [--random-sort [--random-source file]] "
107 #else
108 "[--version-sort] "
109 #endif
110 "[--compress-program program] [file ...]\n" };
112 struct sort_opts sort_opts_vals;
114 bool debug_sort;
115 bool need_hint;
117 #if defined(SORT_THREADS)
118 unsigned int ncpu = 1;
119 size_t nthreads = 1;
120 #endif
122 static bool gnusort_numeric_compatibility;
124 static struct sort_mods default_sort_mods_object;
125 struct sort_mods * const default_sort_mods = &default_sort_mods_object;
127 static bool print_symbols_on_debug;
130 * Arguments from file (when file0-from option is used:
132 static size_t argc_from_file0 = (size_t)-1;
133 static char **argv_from_file0;
136 * Placeholder symbols for options which have no single-character equivalent
138 enum
140 SORT_OPT = CHAR_MAX + 1,
141 HELP_OPT,
142 FF_OPT,
143 BS_OPT,
144 VERSION_OPT,
145 DEBUG_OPT,
146 #if defined(SORT_THREADS)
147 PARALLEL_OPT,
148 #endif
149 #if defined(SORT_RANDOM)
150 RANDOMSOURCE_OPT,
151 #endif
152 COMPRESSPROGRAM_OPT,
153 QSORT_OPT,
154 MERGESORT_OPT,
155 HEAPSORT_OPT,
156 RADIXSORT_OPT,
157 MMAP_OPT
160 #define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
161 static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };
163 static struct option long_options[] = {
164 { "batch-size", required_argument, NULL, BS_OPT },
165 { "buffer-size", required_argument, NULL, 'S' },
166 { "check", optional_argument, NULL, 'c' },
167 { "check=silent|quiet", optional_argument, NULL, 'C' },
168 { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT },
169 { "debug", no_argument, NULL, DEBUG_OPT },
170 { "dictionary-order", no_argument, NULL, 'd' },
171 { "field-separator", required_argument, NULL, 't' },
172 { "files0-from", required_argument, NULL, FF_OPT },
173 { "general-numeric-sort", no_argument, NULL, 'g' },
174 { "heapsort", no_argument, NULL, HEAPSORT_OPT },
175 { "help",no_argument, NULL, HELP_OPT },
176 { "human-numeric-sort", no_argument, NULL, 'h' },
177 { "ignore-leading-blanks", no_argument, NULL, 'b' },
178 { "ignore-case", no_argument, NULL, 'f' },
179 { "ignore-nonprinting", no_argument, NULL, 'i' },
180 { "key", required_argument, NULL, 'k' },
181 { "merge", no_argument, NULL, 'm' },
182 { "mergesort", no_argument, NULL, MERGESORT_OPT },
183 { "mmap", no_argument, NULL, MMAP_OPT },
184 { "month-sort", no_argument, NULL, 'M' },
185 { "numeric-sort", no_argument, NULL, 'n' },
186 { "output", required_argument, NULL, 'o' },
187 #if defined(SORT_THREADS)
188 { "parallel", required_argument, NULL, PARALLEL_OPT },
189 #endif
190 { "qsort", no_argument, NULL, QSORT_OPT },
191 { "radixsort", no_argument, NULL, RADIXSORT_OPT },
192 #if defined(SORT_RANDOM)
193 { "random-sort", no_argument, NULL, 'R' },
194 { "random-source", required_argument, NULL, RANDOMSOURCE_OPT },
195 #endif
196 { "reverse", no_argument, NULL, 'r' },
197 { "sort", required_argument, NULL, SORT_OPT },
198 { "stable", no_argument, NULL, 's' },
199 { "temporary-directory",required_argument, NULL, 'T' },
200 { "unique", no_argument, NULL, 'u' },
201 { "version", no_argument, NULL, VERSION_OPT },
202 { "version-sort",no_argument, NULL, 'V' },
203 { "zero-terminated", no_argument, NULL, 'z' },
204 { NULL, no_argument, NULL, 0 }
207 static void fix_obsolete_keys(int *argc, char **argv);
210 * Check where sort modifier is present
212 static bool
213 sort_modifier_empty(struct sort_mods *sm)
216 if (sm == NULL)
217 return (true);
218 return (!(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag ||
219 #ifdef SORT_RANDOM
220 sm->Rflag ||
221 #endif
222 sm->rflag || sm->hflag || sm->dflag || sm->fflag));
226 * Print out usage text.
228 static void
229 usage(bool opt_err)
231 FILE *out;
233 out = opt_err ? stderr : stdout;
235 fprintf(out, getstr(12), getprogname());
236 if (opt_err)
237 exit(2);
238 exit(0);
242 * Read input file names from a file (file0-from option).
244 static void
245 read_fns_from_file0(const char *fn)
247 FILE *f;
248 char *line = NULL;
249 size_t linesize = 0;
250 ssize_t linelen;
252 if (fn == NULL)
253 return;
255 f = fopen(fn, "r");
256 if (f == NULL)
257 err(2, "%s", fn);
259 while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
260 if (*line != '\0') {
261 if (argc_from_file0 == (size_t) - 1)
262 argc_from_file0 = 0;
263 ++argc_from_file0;
264 argv_from_file0 = sort_realloc(argv_from_file0,
265 argc_from_file0 * sizeof(char *));
266 if (argv_from_file0 == NULL)
267 err(2, NULL);
268 argv_from_file0[argc_from_file0 - 1] = line;
269 } else {
270 free(line);
272 line = NULL;
273 linesize = 0;
275 if (ferror(f))
276 err(2, "%s: getdelim", fn);
278 closefile(f, fn);
282 * Check how much RAM is available for the sort.
284 static void
285 set_hw_params(void)
287 long pages, psize;
289 pages = psize = 0;
291 #if defined(SORT_THREADS)
292 ncpu = 1;
293 #endif
295 pages = sysconf(_SC_PHYS_PAGES);
296 if (pages < 1) {
297 perror("sysconf pages");
298 pages = 1;
300 psize = sysconf(_SC_PAGESIZE);
301 if (psize < 1) {
302 perror("sysconf psize");
303 psize = 4096;
305 #if defined(SORT_THREADS)
306 ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
307 if (ncpu < 1)
308 ncpu = 1;
309 else if(ncpu > 32)
310 ncpu = 32;
312 nthreads = ncpu;
313 #endif
315 free_memory = (unsigned long long) pages * (unsigned long long) psize;
316 available_free_memory = free_memory / 2;
318 if (available_free_memory < 1024)
319 available_free_memory = 1024;
323 * Convert "plain" symbol to wide symbol, with default value.
325 static void
326 conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def)
329 if (wc && c) {
330 int res;
332 res = mbtowc(wc, c, MB_CUR_MAX);
333 if (res < 1)
334 *wc = def;
339 * Set current locale symbols.
341 static void
342 set_locale(void)
344 struct lconv *lc;
345 const char *locale;
347 setlocale(LC_ALL, "");
349 lc = localeconv();
351 if (lc) {
352 /* obtain LC_NUMERIC info */
353 /* Convert to wide char form */
354 conv_mbtowc(&symbol_decimal_point, lc->decimal_point,
355 symbol_decimal_point);
356 conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep,
357 symbol_thousands_sep);
358 conv_mbtowc(&symbol_positive_sign, lc->positive_sign,
359 symbol_positive_sign);
360 conv_mbtowc(&symbol_negative_sign, lc->negative_sign,
361 symbol_negative_sign);
364 if (getenv("GNUSORT_NUMERIC_COMPATIBILITY"))
365 gnusort_numeric_compatibility = true;
367 locale = setlocale(LC_COLLATE, NULL);
369 if (locale) {
370 char *tmpl;
371 const char *cclocale;
373 tmpl = sort_strdup(locale);
374 cclocale = setlocale(LC_COLLATE, "C");
375 if (cclocale && !strcmp(cclocale, tmpl))
376 byte_sort = true;
377 else {
378 const char *pclocale;
380 pclocale = setlocale(LC_COLLATE, "POSIX");
381 if (pclocale && !strcmp(pclocale, tmpl))
382 byte_sort = true;
384 setlocale(LC_COLLATE, tmpl);
385 sort_free(tmpl);
390 * Set directory temporary files.
392 static void
393 set_tmpdir(void)
395 char *td;
397 td = getenv("TMPDIR");
398 if (td != NULL)
399 tmpdir = sort_strdup(td);
403 * Parse -S option.
405 static unsigned long long
406 parse_memory_buffer_value(const char *value)
409 if (value == NULL)
410 return (available_free_memory);
411 else {
412 char *endptr;
413 unsigned long long membuf;
415 endptr = NULL;
416 errno = 0;
417 membuf = strtoll(value, &endptr, 10);
419 if (errno != 0) {
420 warn("%s",getstr(4));
421 membuf = available_free_memory;
422 } else {
423 switch (*endptr){
424 case 'Y':
425 membuf *= 1024;
426 /* FALLTHROUGH */
427 case 'Z':
428 membuf *= 1024;
429 /* FALLTHROUGH */
430 case 'E':
431 membuf *= 1024;
432 /* FALLTHROUGH */
433 case 'P':
434 membuf *= 1024;
435 /* FALLTHROUGH */
436 case 'T':
437 membuf *= 1024;
438 /* FALLTHROUGH */
439 case 'G':
440 membuf *= 1024;
441 /* FALLTHROUGH */
442 case 'M':
443 membuf *= 1024;
444 /* FALLTHROUGH */
445 case '\0':
446 case 'K':
447 membuf *= 1024;
448 /* FALLTHROUGH */
449 case 'b':
450 break;
451 case '%':
452 membuf = (available_free_memory * membuf) /
453 100;
454 break;
455 default:
456 warnc(EINVAL, "%s", optarg);
457 membuf = available_free_memory;
460 return (membuf);
465 * Signal handler that clears the temporary files.
467 static void
468 sig_handler(int sig __unused, siginfo_t *siginfo __unused,
469 void *context __unused)
472 clear_tmp_files();
473 exit(-1);
477 * Set signal handler on panic signals.
479 static void
480 set_signal_handler(void)
482 struct sigaction sa;
484 memset(&sa, 0, sizeof(sa));
485 sa.sa_sigaction = &sig_handler;
486 sa.sa_flags = SA_SIGINFO;
488 if (sigaction(SIGTERM, &sa, NULL) < 0) {
489 perror("sigaction");
490 return;
492 if (sigaction(SIGHUP, &sa, NULL) < 0) {
493 perror("sigaction");
494 return;
496 if (sigaction(SIGINT, &sa, NULL) < 0) {
497 perror("sigaction");
498 return;
500 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
501 perror("sigaction");
502 return;
504 if (sigaction(SIGABRT, &sa, NULL) < 0) {
505 perror("sigaction");
506 return;
508 if (sigaction(SIGBUS, &sa, NULL) < 0) {
509 perror("sigaction");
510 return;
512 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
513 perror("sigaction");
514 return;
516 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
517 perror("sigaction");
518 return;
520 if (sigaction(SIGUSR2, &sa, NULL) < 0) {
521 perror("sigaction");
522 return;
527 * Print "unknown" message and exit with status 2.
529 static void
530 unknown(const char *what)
533 errx(2, "%s: %s", getstr(3), what);
537 * Check whether contradictory input options are used.
539 static void
540 check_mutually_exclusive_flags(char c, bool *mef_flags)
542 int fo_index, mec;
543 bool found_others, found_this;
545 found_others = found_this = false;
546 fo_index = 0;
548 for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {
549 mec = mutually_exclusive_flags[i];
551 if (mec != c) {
552 if (mef_flags[i]) {
553 if (found_this)
554 errx(1, "%c:%c: %s", c, mec, getstr(1));
555 found_others = true;
556 fo_index = i;
558 } else {
559 if (found_others)
560 errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1));
561 mef_flags[i] = true;
562 found_this = true;
568 * Initialise sort opts data.
570 static void
571 set_sort_opts(void)
574 memset(&default_sort_mods_object, 0,
575 sizeof(default_sort_mods_object));
576 memset(&sort_opts_vals, 0, sizeof(sort_opts_vals));
577 default_sort_mods_object.func =
578 get_sort_func(&default_sort_mods_object);
582 * Set a sort modifier on a sort modifiers object.
584 static bool
585 set_sort_modifier(struct sort_mods *sm, int c)
588 if (sm) {
589 switch (c){
590 case 'b':
591 sm->bflag = true;
592 break;
593 case 'd':
594 sm->dflag = true;
595 break;
596 case 'f':
597 sm->fflag = true;
598 break;
599 case 'g':
600 sm->gflag = true;
601 need_hint = true;
602 break;
603 case 'i':
604 sm->iflag = true;
605 break;
606 #ifdef SORT_RANDOM
607 case 'R':
608 sm->Rflag = true;
609 need_random = true;
610 break;
611 #endif
612 case 'M':
613 initialise_months();
614 sm->Mflag = true;
615 need_hint = true;
616 break;
617 case 'n':
618 sm->nflag = true;
619 need_hint = true;
620 print_symbols_on_debug = true;
621 break;
622 case 'r':
623 sm->rflag = true;
624 break;
625 case 'V':
626 sm->Vflag = true;
627 break;
628 case 'h':
629 sm->hflag = true;
630 need_hint = true;
631 print_symbols_on_debug = true;
632 break;
633 default:
634 return false;
636 sort_opts_vals.complex_sort = true;
637 sm->func = get_sort_func(sm);
639 return (true);
643 * Parse POS in -k option.
645 static int
646 parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second)
648 regmatch_t pmatch[4];
649 regex_t re;
650 char *c, *f;
651 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$";
652 size_t len, nmatch;
653 int ret;
655 ret = -1;
656 nmatch = 4;
657 c = f = NULL;
659 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
660 return (-1);
662 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
663 goto end;
665 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
666 goto end;
668 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
669 goto end;
671 len = pmatch[1].rm_eo - pmatch[1].rm_so;
672 f = sort_malloc((len + 1) * sizeof(char));
674 strncpy(f, s + pmatch[1].rm_so, len);
675 f[len] = '\0';
677 if (second) {
678 errno = 0;
679 ks->f2 = (size_t) strtoul(f, NULL, 10);
680 if (errno != 0)
681 err(2, "-k");
682 if (ks->f2 == 0) {
683 warn("%s",getstr(5));
684 goto end;
686 } else {
687 errno = 0;
688 ks->f1 = (size_t) strtoul(f, NULL, 10);
689 if (errno != 0)
690 err(2, "-k");
691 if (ks->f1 == 0) {
692 warn("%s",getstr(5));
693 goto end;
697 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
698 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
699 c = sort_malloc((len + 1) * sizeof(char));
701 strncpy(c, s + pmatch[2].rm_so + 1, len);
702 c[len] = '\0';
704 if (second) {
705 errno = 0;
706 ks->c2 = (size_t) strtoul(c, NULL, 10);
707 if (errno != 0)
708 err(2, "-k");
709 } else {
710 errno = 0;
711 ks->c1 = (size_t) strtoul(c, NULL, 10);
712 if (errno != 0)
713 err(2, "-k");
714 if (ks->c1 == 0) {
715 warn("%s",getstr(6));
716 goto end;
719 } else {
720 if (second)
721 ks->c2 = 0;
722 else
723 ks->c1 = 1;
726 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
727 regoff_t i = 0;
729 for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) {
730 check_mutually_exclusive_flags(s[i], mef_flags);
731 if (s[i] == 'b') {
732 if (second)
733 ks->pos2b = true;
734 else
735 ks->pos1b = true;
736 } else if (!set_sort_modifier(&(ks->sm), s[i]))
737 goto end;
741 ret = 0;
743 end:
745 if (c)
746 sort_free(c);
747 if (f)
748 sort_free(f);
749 regfree(&re);
751 return (ret);
755 * Parse -k option value.
757 static int
758 parse_k(const char *s, struct key_specs *ks)
760 int ret = -1;
761 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
762 { false, false, false, false, false, false };
764 if (s && *s) {
765 char *sptr;
767 sptr = strchr(s, ',');
768 if (sptr) {
769 size_t size1;
770 char *pos1, *pos2;
772 size1 = sptr - s;
774 if (size1 < 1)
775 return (-1);
776 pos1 = sort_malloc((size1 + 1) * sizeof(char));
778 strncpy(pos1, s, size1);
779 pos1[size1] = '\0';
781 ret = parse_pos(pos1, ks, mef_flags, false);
783 sort_free(pos1);
784 if (ret < 0)
785 return (ret);
787 pos2 = sort_strdup(sptr + 1);
788 ret = parse_pos(pos2, ks, mef_flags, true);
789 sort_free(pos2);
790 } else
791 ret = parse_pos(s, ks, mef_flags, false);
794 return (ret);
798 * Parse POS in +POS -POS option.
800 static int
801 parse_pos_obs(const char *s, int *nf, int *nc, char* sopts)
803 regex_t re;
804 regmatch_t pmatch[4];
805 char *c, *f;
806 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$";
807 int ret;
808 size_t len, nmatch;
810 ret = -1;
811 nmatch = 4;
812 c = f = NULL;
813 *nc = *nf = 0;
815 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
816 return (-1);
818 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
819 goto end;
821 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
822 goto end;
824 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
825 goto end;
827 len = pmatch[1].rm_eo - pmatch[1].rm_so;
828 f = sort_malloc((len + 1) * sizeof(char));
830 strncpy(f, s + pmatch[1].rm_so, len);
831 f[len] = '\0';
833 errno = 0;
834 *nf = (size_t) strtoul(f, NULL, 10);
835 if (errno != 0)
836 errx(2, "%s", getstr(11));
838 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
839 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
840 c = sort_malloc((len + 1) * sizeof(char));
842 strncpy(c, s + pmatch[2].rm_so + 1, len);
843 c[len] = '\0';
845 errno = 0;
846 *nc = (size_t) strtoul(c, NULL, 10);
847 if (errno != 0)
848 errx(2, "%s", getstr(11));
851 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
853 len = pmatch[3].rm_eo - pmatch[3].rm_so;
855 strncpy(sopts, s + pmatch[3].rm_so, len);
856 sopts[len] = '\0';
859 ret = 0;
861 end:
862 if (c)
863 sort_free(c);
864 if (f)
865 sort_free(f);
866 regfree(&re);
868 return (ret);
872 * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax
874 static void
875 fix_obsolete_keys(int *argc, char **argv)
877 char *snew = NULL;
879 for (int i = 1; i < *argc; i++) {
880 char *arg1;
882 arg1 = argv[i];
884 if (strlen(arg1) > 1 && arg1[0] == '+') {
885 int c1, f1;
886 char sopts1[128];
888 sopts1[0] = 0;
889 c1 = f1 = 0;
891 if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1) < 0)
892 continue;
893 else {
894 f1 += 1;
895 c1 += 1;
896 if (i + 1 < *argc) {
897 char *arg2 = argv[i + 1];
899 if (strlen(arg2) > 1 &&
900 arg2[0] == '-') {
901 int c2, f2;
902 char sopts2[128];
904 sopts2[0] = 0;
905 c2 = f2 = 0;
907 if (parse_pos_obs(arg2 + 1,
908 &f2, &c2, sopts2) >= 0) {
909 if (c2 > 0)
910 f2 += 1;
911 if (asprintf(&snew,
912 "-k%d.%d%s,%d.%d%s",
913 f1, c1, sopts1,
914 f2, c2, sopts2)== -1)
915 return;
916 argv[i] = snew;
917 for (int j = i + 1; j + 1 < *argc; j++)
918 argv[j] = argv[j + 1];
919 *argc -= 1;
920 continue;
924 asprintf(&snew, "-k%d.%d%s", f1, c1, sopts1);
925 argv[i] = snew;
932 * Set random seed
934 #if defined(SORT_RANDOM)
935 static char *
936 random_md5end(MD5_CTX *ctx)
938 unsigned char digest[MD5_DIGEST_LENGTH];
939 static const char hex[]="0123456789abcdef";
940 char *buf;
941 int i;
943 buf = malloc(MD5_DIGEST_LENGTH * 2 + 1);
944 if (!buf)
945 return NULL;
946 MD5_Final(digest, ctx);
947 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
948 buf[2*i] = hex[digest[i] >> 4];
949 buf[2*i+1] = hex[digest[i] & 0x0f];
951 buf[MD5_DIGEST_LENGTH * 2] = '\0';
952 return buf;
955 static char *
956 random_fromfile(const char *filename)
958 MD5_CTX ctx;
959 FILE* fp;
960 unsigned char buffer[4096];
961 struct stat st;
962 off_t size;
963 int bytes;
965 fp = openfile(filename, "r");
966 if (fp == NULL)
967 return NULL;
968 if (fstat(fileno(fp), &st) < 0) {
969 bytes = -1;
970 goto err;
973 MD5_Init(&ctx);
974 size = st.st_size;
975 bytes = 0;
976 while (size > 0 && (bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
977 MD5_Update(&ctx, buffer, bytes);
978 size -= bytes;
981 err:
982 closefile(fp, NULL);
983 if (bytes < 0)
984 return NULL;
986 return (random_md5end(&ctx));
989 static void
990 set_random_seed(void)
992 if (need_random) {
994 if (strcmp(random_source, DEFAULT_RANDOM_SORT_SEED_FILE) == 0) {
995 FILE* fseed;
996 MD5_CTX ctx;
997 char rsd[MAX_DEFAULT_RANDOM_SEED_DATA_SIZE];
998 size_t sz = 0;
1000 fseed = openfile(random_source, "r");
1001 while (!feof(fseed)) {
1002 int cr;
1004 cr = fgetc(fseed);
1005 if (cr == EOF)
1006 break;
1008 rsd[sz++] = (char) cr;
1010 if (sz >= MAX_DEFAULT_RANDOM_SEED_DATA_SIZE)
1011 break;
1014 closefile(fseed, random_source);
1016 MD5_Init(&ctx);
1017 MD5_Update(&ctx, rsd, sz);
1019 random_seed = random_md5end(&ctx);
1020 random_seed_size = strlen(random_seed);
1022 } else {
1023 MD5_CTX ctx;
1024 char *b;
1026 MD5_Init(&ctx);
1027 b = random_fromfile(random_source);
1028 if (b == NULL)
1029 err(2, NULL);
1031 random_seed = b;
1032 random_seed_size = strlen(b);
1035 MD5_Init(&md5_ctx);
1036 if(random_seed_size>0) {
1037 MD5_Update(&md5_ctx, random_seed, random_seed_size);
1041 #endif
1044 * Main function.
1047 main(int argc, char **argv)
1049 char *outfile, *real_outfile;
1050 int c, result;
1051 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
1052 { false, false, false, false, false, false };
1054 result = 0;
1055 outfile = sort_strdup("-");
1056 real_outfile = NULL;
1058 struct sort_mods *sm = &default_sort_mods_object;
1060 init_tmp_files();
1062 set_signal_handler();
1064 set_hw_params();
1065 set_locale();
1066 set_tmpdir();
1067 set_sort_opts();
1069 fix_obsolete_keys(&argc, argv);
1071 while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
1072 != -1)) {
1074 check_mutually_exclusive_flags(c, mef_flags);
1076 if (!set_sort_modifier(sm, c)) {
1078 switch (c) {
1079 case 'c':
1080 sort_opts_vals.cflag = true;
1081 if (optarg) {
1082 if (!strcmp(optarg, "diagnose-first"))
1084 else if (!strcmp(optarg, "silent") ||
1085 !strcmp(optarg, "quiet"))
1086 sort_opts_vals.csilentflag = true;
1087 else if (*optarg)
1088 unknown(optarg);
1090 break;
1091 case 'C':
1092 sort_opts_vals.cflag = true;
1093 sort_opts_vals.csilentflag = true;
1094 break;
1095 case 'k':
1097 sort_opts_vals.complex_sort = true;
1098 sort_opts_vals.kflag = true;
1100 keys_num++;
1101 keys = sort_realloc(keys, keys_num *
1102 sizeof(struct key_specs));
1103 memset(&(keys[keys_num - 1]), 0,
1104 sizeof(struct key_specs));
1106 if (parse_k(optarg, &(keys[keys_num - 1]))
1107 < 0) {
1108 errc(2, EINVAL, "-k %s", optarg);
1111 break;
1113 case 'm':
1114 sort_opts_vals.mflag = true;
1115 break;
1116 case 'o':
1117 outfile = sort_realloc(outfile, (strlen(optarg) + 1));
1118 strcpy(outfile, optarg);
1119 break;
1120 case 's':
1121 sort_opts_vals.sflag = true;
1122 break;
1123 case 'S':
1124 available_free_memory =
1125 parse_memory_buffer_value(optarg);
1126 break;
1127 case 'T':
1128 tmpdir = sort_strdup(optarg);
1129 break;
1130 case 't':
1131 while (strlen(optarg) > 1) {
1132 if (optarg[0] != '\\') {
1133 errc(2, EINVAL, "%s", optarg);
1135 optarg += 1;
1136 if (*optarg == '0') {
1137 *optarg = 0;
1138 break;
1141 sort_opts_vals.tflag = true;
1142 sort_opts_vals.field_sep = btowc(optarg[0]);
1143 if (sort_opts_vals.field_sep == WEOF) {
1144 errno = EINVAL;
1145 err(2, NULL);
1147 if (!gnusort_numeric_compatibility) {
1148 if (symbol_decimal_point == sort_opts_vals.field_sep)
1149 symbol_decimal_point = WEOF;
1150 if (symbol_thousands_sep == sort_opts_vals.field_sep)
1151 symbol_thousands_sep = WEOF;
1152 if (symbol_negative_sign == sort_opts_vals.field_sep)
1153 symbol_negative_sign = WEOF;
1154 if (symbol_positive_sign == sort_opts_vals.field_sep)
1155 symbol_positive_sign = WEOF;
1157 break;
1158 case 'u':
1159 sort_opts_vals.uflag = true;
1160 /* stable sort for the correct unique val */
1161 sort_opts_vals.sflag = true;
1162 break;
1163 case 'z':
1164 sort_opts_vals.zflag = true;
1165 break;
1166 case SORT_OPT:
1167 if (optarg) {
1168 if (!strcmp(optarg, "general-numeric"))
1169 set_sort_modifier(sm, 'g');
1170 else if (!strcmp(optarg, "human-numeric"))
1171 set_sort_modifier(sm, 'h');
1172 else if (!strcmp(optarg, "numeric"))
1173 set_sort_modifier(sm, 'n');
1174 else if (!strcmp(optarg, "month"))
1175 set_sort_modifier(sm, 'M');
1176 #if defined(SORT_RANDOM)
1177 else if (!strcmp(optarg, "random"))
1178 set_sort_modifier(sm, 'R');
1179 #endif
1180 else
1181 unknown(optarg);
1183 break;
1184 #if defined(SORT_THREADS)
1185 case PARALLEL_OPT:
1186 nthreads = (size_t)(atoi(optarg));
1187 if (nthreads < 1)
1188 nthreads = 1;
1189 if (nthreads > 1024)
1190 nthreads = 1024;
1191 break;
1192 #endif
1193 case QSORT_OPT:
1194 sort_opts_vals.sort_method = SORT_QSORT;
1195 break;
1196 case MERGESORT_OPT:
1197 sort_opts_vals.sort_method = SORT_MERGESORT;
1198 break;
1199 case MMAP_OPT:
1200 use_mmap = true;
1201 break;
1202 case HEAPSORT_OPT:
1203 sort_opts_vals.sort_method = SORT_HEAPSORT;
1204 break;
1205 case RADIXSORT_OPT:
1206 sort_opts_vals.sort_method = SORT_RADIXSORT;
1207 break;
1208 #if defined(SORT_RANDOM)
1209 case RANDOMSOURCE_OPT:
1210 random_source = strdup(optarg);
1211 break;
1212 #endif
1213 case COMPRESSPROGRAM_OPT:
1214 compress_program = strdup(optarg);
1215 break;
1216 case FF_OPT:
1217 read_fns_from_file0(optarg);
1218 break;
1219 case BS_OPT:
1221 errno = 0;
1222 long mof = strtol(optarg, NULL, 10);
1223 if (errno != 0)
1224 err(2, "--batch-size");
1225 if (mof >= 2)
1226 max_open_files = (size_t) mof + 1;
1228 break;
1229 case VERSION_OPT:
1230 printf("%s\n", VERSION);
1231 exit(EXIT_SUCCESS);
1232 /* NOTREACHED */
1233 break;
1234 case DEBUG_OPT:
1235 debug_sort = true;
1236 break;
1237 case HELP_OPT:
1238 usage(false);
1239 /* NOTREACHED */
1240 break;
1241 default:
1242 usage(true);
1243 /* NOTREACHED */
1248 argc -= optind;
1249 argv += optind;
1251 #ifndef WITHOUT_NLS
1252 catalog = catopen("sort", NL_CAT_LOCALE);
1253 #endif
1255 if (sort_opts_vals.cflag && sort_opts_vals.mflag)
1256 errx(1, "%c:%c: %s", 'm', 'c', getstr(1));
1258 #ifndef WITHOUT_NLS
1259 catclose(catalog);
1260 #endif
1262 if (keys_num == 0) {
1263 keys_num = 1;
1264 keys = sort_realloc(keys, sizeof(struct key_specs));
1265 memset(&(keys[0]), 0, sizeof(struct key_specs));
1266 keys[0].c1 = 1;
1267 keys[0].pos1b = default_sort_mods->bflag;
1268 keys[0].pos2b = default_sort_mods->bflag;
1269 memcpy(&(keys[0].sm), default_sort_mods,
1270 sizeof(struct sort_mods));
1273 for (size_t i = 0; i < keys_num; i++) {
1274 struct key_specs *ks;
1276 ks = &(keys[i]);
1278 if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) &&
1279 !(ks->pos2b)) {
1280 ks->pos1b = sm->bflag;
1281 ks->pos2b = sm->bflag;
1282 memcpy(&(ks->sm), sm, sizeof(struct sort_mods));
1285 ks->sm.func = get_sort_func(&(ks->sm));
1288 if (argv_from_file0) {
1289 argc = argc_from_file0;
1290 argv = argv_from_file0;
1293 if (debug_sort) {
1294 printf("Memory to be used for sorting: %llu\n",available_free_memory);
1295 #if defined(SORT_THREADS)
1296 printf("Number of CPUs: %d\n",(int)ncpu);
1297 nthreads = 1;
1298 #endif
1299 printf("Using collate rules of %s locale\n",
1300 setlocale(LC_COLLATE, NULL));
1301 if (byte_sort)
1302 printf("Byte sort is used\n");
1303 if (print_symbols_on_debug) {
1304 printf("Decimal Point: <%lc>\n", symbol_decimal_point);
1305 if (symbol_thousands_sep)
1306 printf("Thousands separator: <%lc>\n",
1307 symbol_thousands_sep);
1308 printf("Positive sign: <%lc>\n", symbol_positive_sign);
1309 printf("Negative sign: <%lc>\n", symbol_negative_sign);
1313 #if defined(SORT_RANDOM)
1314 set_random_seed();
1315 #endif
1317 /* Case when the outfile equals one of the input files: */
1318 if (strcmp(outfile, "-")) {
1320 for(int i = 0; i < argc; ++i) {
1321 if (strcmp(argv[i], outfile) == 0) {
1322 real_outfile = sort_strdup(outfile);
1323 for(;;) {
1324 char* tmp = sort_malloc(strlen(outfile) +
1325 strlen(".tmp") + 1);
1327 strcpy(tmp, outfile);
1328 strcpy(tmp + strlen(tmp), ".tmp");
1329 sort_free(outfile);
1330 outfile = tmp;
1331 if (access(outfile, F_OK) < 0)
1332 break;
1334 tmp_file_atexit(outfile);
1339 #if defined(SORT_THREADS)
1340 if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0))
1341 nthreads = 1;
1342 #endif
1344 if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) {
1345 struct file_list fl;
1346 struct sort_list list;
1348 sort_list_init(&list);
1349 file_list_init(&fl, true);
1351 if (argc < 1)
1352 procfile("-", &list, &fl);
1353 else {
1354 while (argc > 0) {
1355 procfile(*argv, &list, &fl);
1356 --argc;
1357 ++argv;
1361 if (fl.count < 1)
1362 sort_list_to_file(&list, outfile);
1363 else {
1364 if (list.count > 0) {
1365 char *flast = new_tmp_file_name();
1367 sort_list_to_file(&list, flast);
1368 file_list_add(&fl, flast, false);
1370 merge_files(&fl, outfile);
1373 file_list_clean(&fl);
1376 * We are about to exit the program, so we can ignore
1377 * the clean-up for speed
1379 * sort_list_clean(&list);
1382 } else if (sort_opts_vals.cflag) {
1383 result = (argc == 0) ? (check("-")) : (check(*argv));
1384 } else if (sort_opts_vals.mflag) {
1385 struct file_list fl;
1387 file_list_init(&fl, false);
1388 file_list_populate(&fl, argc, argv, true);
1389 merge_files(&fl, outfile);
1390 file_list_clean(&fl);
1393 if (real_outfile) {
1394 unlink(real_outfile);
1395 if (rename(outfile, real_outfile) < 0)
1396 err(2, NULL);
1397 sort_free(real_outfile);
1400 sort_free(outfile);
1402 return (result);