2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/sysctl.h>
35 #include <sys/types.h>
58 #define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz"
60 static bool need_random;
65 * Default messages to use
67 const char *nlsstr[] = { "",
68 /* 1*/"mutually exclusive flags",
69 /* 2*/"extra argument not allowed with -c",
70 /* 3*/"Unknown feature",
71 /* 4*/"Wrong memory buffer specification",
72 /* 5*/"0 field in key specs",
73 /* 6*/"0 column in key specs",
74 /* 7*/"Wrong file mode",
75 /* 8*/"Cannot open file for reading",
76 /* 9*/"Radix sort cannot be used with these sort options",
77 /*10*/"The chosen sort method cannot be used with stable and/or unique sort",
78 /*11*/"Invalid key position",
79 /*12*/"Usage: %s [-bcCdfigMmnrsuz] [-kPOS1[,POS2] ... ] "
80 "[+POS1 [-POS2]] [-S memsize] [-T tmpdir] [-t separator] "
81 "[-o outfile] [--batch-size size] [--files0-from file] "
82 "[--heapsort] [--mergesort] [--radixsort] [--qsort] "
84 #if defined(SORT_THREADS)
85 "[--parallel thread_no] "
87 "[--human-numeric-sort] "
88 "[--version-sort] [--random-sort [--random-source file]] "
89 "[--compress-program program] [file ...]\n" };
91 struct sort_opts sort_opts_vals;
98 #if defined(SORT_THREADS)
99 unsigned int ncpu = 1;
103 static bool gnusort_numeric_compatibility;
105 static struct sort_mods default_sort_mods_object;
106 struct sort_mods * const default_sort_mods = &default_sort_mods_object;
108 static bool print_symbols_on_debug;
111 * Arguments from file (when file0-from option is used:
113 static size_t argc_from_file0 = (size_t)-1;
114 static char **argv_from_file0;
117 * Placeholder symbols for options which have no single-character equivalent
121 SORT_OPT = CHAR_MAX + 1,
127 #if defined(SORT_THREADS)
139 #define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
140 static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };
142 static struct option long_options[] = {
143 { "batch-size", required_argument, NULL, BS_OPT },
144 { "buffer-size", required_argument, NULL, 'S' },
145 { "check", optional_argument, NULL, 'c' },
146 { "check=silent|quiet", optional_argument, NULL, 'C' },
147 { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT },
148 { "debug", no_argument, NULL, DEBUG_OPT },
149 { "dictionary-order", no_argument, NULL, 'd' },
150 { "field-separator", required_argument, NULL, 't' },
151 { "files0-from", required_argument, NULL, FF_OPT },
152 { "general-numeric-sort", no_argument, NULL, 'g' },
153 { "heapsort", no_argument, NULL, HEAPSORT_OPT },
154 { "help",no_argument, NULL, HELP_OPT },
155 { "human-numeric-sort", no_argument, NULL, 'h' },
156 { "ignore-leading-blanks", no_argument, NULL, 'b' },
157 { "ignore-case", no_argument, NULL, 'f' },
158 { "ignore-nonprinting", no_argument, NULL, 'i' },
159 { "key", required_argument, NULL, 'k' },
160 { "merge", no_argument, NULL, 'm' },
161 { "mergesort", no_argument, NULL, MERGESORT_OPT },
162 { "mmap", no_argument, NULL, MMAP_OPT },
163 { "month-sort", no_argument, NULL, 'M' },
164 { "numeric-sort", no_argument, NULL, 'n' },
165 { "output", required_argument, NULL, 'o' },
166 #if defined(SORT_THREADS)
167 { "parallel", required_argument, NULL, PARALLEL_OPT },
169 { "qsort", no_argument, NULL, QSORT_OPT },
170 { "radixsort", no_argument, NULL, RADIXSORT_OPT },
171 { "random-sort", no_argument, NULL, 'R' },
172 { "random-source", required_argument, NULL, RANDOMSOURCE_OPT },
173 { "reverse", no_argument, NULL, 'r' },
174 { "sort", required_argument, NULL, SORT_OPT },
175 { "stable", no_argument, NULL, 's' },
176 { "temporary-directory",required_argument, NULL, 'T' },
177 { "unique", no_argument, NULL, 'u' },
178 { "version", no_argument, NULL, VERSION_OPT },
179 { "version-sort",no_argument, NULL, 'V' },
180 { "zero-terminated", no_argument, NULL, 'z' },
181 { NULL, no_argument, NULL, 0 }
184 void fix_obsolete_keys(int *argc, char **argv);
187 * Check where sort modifier is present
190 sort_modifier_empty(struct sort_mods *sm)
195 return (!(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag ||
196 sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag));
200 * Print out usage text.
207 out = opt_err ? stderr : stdout;
209 fprintf(out, getstr(12), getprogname());
216 * Read input file names from a file (file0-from option).
219 read_fns_from_file0(const char *fn)
233 while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
235 if (argc_from_file0 == (size_t) - 1)
238 argv_from_file0 = sort_realloc(argv_from_file0,
239 argc_from_file0 * sizeof(char *));
240 if (argv_from_file0 == NULL)
242 argv_from_file0[argc_from_file0 - 1] = line;
250 err(2, "%s: getdelim", fn);
256 * Check how much RAM is available for the sort.
263 #if defined(SORT_THREADS)
267 pages = sysconf(_SC_PHYS_PAGES);
269 perror("sysconf pages");
272 psize = sysconf(_SC_PAGESIZE);
274 perror("sysconf psize");
277 #if defined(SORT_THREADS)
278 ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
287 free_memory = (unsigned long long) pages * (unsigned long long) psize;
288 available_free_memory = free_memory / 2;
290 if (available_free_memory < 1024)
291 available_free_memory = 1024;
295 * Convert "plain" symbol to wide symbol, with default value.
298 conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def)
304 res = mbtowc(wc, c, mb_cur_max);
311 * Set current locale symbols.
319 setlocale(LC_ALL, "");
321 mb_cur_max = MB_CUR_MAX;
326 /* obtain LC_NUMERIC info */
327 /* Convert to wide char form */
328 conv_mbtowc(&symbol_decimal_point, lc->decimal_point,
329 symbol_decimal_point);
330 conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep,
331 symbol_thousands_sep);
332 conv_mbtowc(&symbol_positive_sign, lc->positive_sign,
333 symbol_positive_sign);
334 conv_mbtowc(&symbol_negative_sign, lc->negative_sign,
335 symbol_negative_sign);
338 if (getenv("GNUSORT_NUMERIC_COMPATIBILITY"))
339 gnusort_numeric_compatibility = true;
341 locale = setlocale(LC_COLLATE, NULL);
345 const char *cclocale;
347 tmpl = sort_strdup(locale);
348 cclocale = setlocale(LC_COLLATE, "C");
349 if (cclocale && !strcmp(cclocale, tmpl))
352 const char *pclocale;
354 pclocale = setlocale(LC_COLLATE, "POSIX");
355 if (pclocale && !strcmp(pclocale, tmpl))
358 setlocale(LC_COLLATE, tmpl);
364 * Set directory temporary files.
371 td = getenv("TMPDIR");
373 tmpdir = sort_strdup(td);
379 static unsigned long long
380 parse_memory_buffer_value(const char *value)
384 return (available_free_memory);
387 unsigned long long membuf;
391 membuf = strtoll(value, &endptr, 10);
394 warn("%s",getstr(4));
395 membuf = available_free_memory;
426 membuf = (available_free_memory * membuf) /
430 warnc(EINVAL, "%s", optarg);
431 membuf = available_free_memory;
439 * Signal handler that clears the temporary files.
442 sig_handler(int sig __unused, siginfo_t *siginfo __unused,
443 void *context __unused)
451 * Set signal handler on panic signals.
454 set_signal_handler(void)
458 memset(&sa, 0, sizeof(sa));
459 sa.sa_sigaction = &sig_handler;
460 sa.sa_flags = SA_SIGINFO;
462 if (sigaction(SIGTERM, &sa, NULL) < 0) {
466 if (sigaction(SIGHUP, &sa, NULL) < 0) {
470 if (sigaction(SIGINT, &sa, NULL) < 0) {
474 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
478 if (sigaction(SIGABRT, &sa, NULL) < 0) {
482 if (sigaction(SIGBUS, &sa, NULL) < 0) {
486 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
490 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
494 if (sigaction(SIGUSR2, &sa, NULL) < 0) {
501 * Print "unknown" message and exit with status 2.
504 unknown(const char *what)
507 errx(2, "%s: %s", getstr(3), what);
511 * Check whether contradictory input options are used.
514 check_mutually_exclusive_flags(char c, bool *mef_flags)
517 bool found_others, found_this;
519 found_others = found_this = false;
522 for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {
523 mec = mutually_exclusive_flags[i];
528 errx(1, "%c:%c: %s", c, mec, getstr(1));
534 errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1));
542 * Initialise sort opts data.
548 memset(&default_sort_mods_object, 0,
549 sizeof(default_sort_mods_object));
550 memset(&sort_opts_vals, 0, sizeof(sort_opts_vals));
551 default_sort_mods_object.func =
552 get_sort_func(&default_sort_mods_object);
556 * Set a sort modifier on a sort modifiers object.
559 set_sort_modifier(struct sort_mods *sm, int c)
595 print_symbols_on_debug = true;
606 print_symbols_on_debug = true;
612 sort_opts_vals.complex_sort = true;
613 sm->func = get_sort_func(sm);
618 * Parse POS in -k option.
621 parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second)
623 regmatch_t pmatch[4];
626 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$";
634 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
637 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
640 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
643 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
646 len = pmatch[1].rm_eo - pmatch[1].rm_so;
647 f = sort_malloc((len + 1) * sizeof(char));
649 strncpy(f, s + pmatch[1].rm_so, len);
654 ks->f2 = (size_t) strtoul(f, NULL, 10);
658 warn("%s",getstr(5));
663 ks->f1 = (size_t) strtoul(f, NULL, 10);
667 warn("%s",getstr(5));
672 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
673 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
674 c = sort_malloc((len + 1) * sizeof(char));
676 strncpy(c, s + pmatch[2].rm_so + 1, len);
681 ks->c2 = (size_t) strtoul(c, NULL, 10);
686 ks->c1 = (size_t) strtoul(c, NULL, 10);
690 warn("%s",getstr(6));
701 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
704 for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) {
705 check_mutually_exclusive_flags(s[i], mef_flags);
711 } else if (!set_sort_modifier(&(ks->sm), s[i]))
730 * Parse -k option value.
733 parse_k(const char *s, struct key_specs *ks)
736 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
737 { false, false, false, false, false, false };
742 sptr = strchr(s, ',');
751 pos1 = sort_malloc((size1 + 1) * sizeof(char));
753 strncpy(pos1, s, size1);
756 ret = parse_pos(pos1, ks, mef_flags, false);
762 pos2 = sort_strdup(sptr + 1);
763 ret = parse_pos(pos2, ks, mef_flags, true);
766 ret = parse_pos(s, ks, mef_flags, false);
773 * Parse POS in +POS -POS option.
776 parse_pos_obs(const char *s, int *nf, int *nc, char* sopts)
779 regmatch_t pmatch[4];
781 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$";
790 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
793 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
796 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
799 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
802 len = pmatch[1].rm_eo - pmatch[1].rm_so;
803 f = sort_malloc((len + 1) * sizeof(char));
805 strncpy(f, s + pmatch[1].rm_so, len);
809 *nf = (size_t) strtoul(f, NULL, 10);
811 errx(2, "%s", getstr(11));
813 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
814 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
815 c = sort_malloc((len + 1) * sizeof(char));
817 strncpy(c, s + pmatch[2].rm_so + 1, len);
821 *nc = (size_t) strtoul(c, NULL, 10);
823 errx(2, "%s", getstr(11));
826 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
828 len = pmatch[3].rm_eo - pmatch[3].rm_so;
830 strncpy(sopts, s + pmatch[3].rm_so, len);
847 * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax
850 fix_obsolete_keys(int *argc, char **argv)
854 for (int i = 1; i < *argc; i++) {
859 if (strcmp(arg1, "--") == 0) {
860 /* Following arguments are treated as filenames. */
864 if (strlen(arg1) > 1 && arg1[0] == '+') {
871 if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1) < 0)
877 char *arg2 = argv[i + 1];
879 if (strlen(arg2) > 1 &&
887 if (parse_pos_obs(arg2 + 1,
888 &f2, &c2, sopts2) >= 0) {
891 sprintf(sopt, "-k%d.%d%s,%d.%d%s",
892 f1, c1, sopts1, f2, c2, sopts2);
893 argv[i] = sort_strdup(sopt);
894 for (int j = i + 1; j + 1 < *argc; j++)
895 argv[j] = argv[j + 1];
901 sprintf(sopt, "-k%d.%d%s", f1, c1, sopts1);
902 argv[i] = sort_strdup(sopt);
912 get_random_seed(const char *random_source)
915 struct stat fsb, rsb;
920 rd = sizeof(randseed);
922 if (random_source == NULL) {
923 if (getentropy(randseed, sizeof(randseed)) < 0)
924 err(EX_SOFTWARE, "getentropy");
928 rsfd = open(random_source, O_RDONLY | O_CLOEXEC);
930 err(EX_NOINPUT, "open: %s", random_source);
932 if (fstat(rsfd, &fsb) != 0)
933 err(EX_SOFTWARE, "fstat");
935 if (!S_ISREG(fsb.st_mode) && !S_ISCHR(fsb.st_mode))
937 "random seed isn't a regular file or /dev/random");
940 * Regular files: read up to maximum seed size and explicitly
941 * reject longer files.
943 if (S_ISREG(fsb.st_mode)) {
944 if (fsb.st_size > (off_t)sizeof(randseed))
945 errx(EX_USAGE, "random seed is too large (%jd >"
946 " %zu)!", (intmax_t)fsb.st_size,
948 else if (fsb.st_size < 1)
949 errx(EX_USAGE, "random seed is too small ("
952 memset(randseed, 0, sizeof(randseed));
954 rd = read(rsfd, randseed, fsb.st_size);
956 err(EX_SOFTWARE, "reading random seed file %s",
958 if (rd < (ssize_t)fsb.st_size)
959 errx(EX_SOFTWARE, "short read from %s", random_source);
960 } else if (S_ISCHR(fsb.st_mode)) {
961 if (stat("/dev/random", &rsb) < 0)
962 err(EX_SOFTWARE, "stat");
964 if (fsb.st_dev != rsb.st_dev ||
965 fsb.st_ino != rsb.st_ino)
966 errx(EX_USAGE, "random seed is a character "
967 "device other than /dev/random");
969 if (getentropy(randseed, sizeof(randseed)) < 0)
970 err(EX_SOFTWARE, "getentropy");
978 MD5Update(&md5_ctx, randseed, rd);
985 main(int argc, char **argv)
987 char *outfile, *real_outfile;
988 char *random_source = NULL;
990 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
991 { false, false, false, false, false, false };
994 outfile = sort_strdup("-");
997 struct sort_mods *sm = &default_sort_mods_object;
1001 set_signal_handler();
1008 fix_obsolete_keys(&argc, argv);
1010 while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
1013 check_mutually_exclusive_flags(c, mef_flags);
1015 if (!set_sort_modifier(sm, c)) {
1019 sort_opts_vals.cflag = true;
1021 if (!strcmp(optarg, "diagnose-first"))
1023 else if (!strcmp(optarg, "silent") ||
1024 !strcmp(optarg, "quiet"))
1025 sort_opts_vals.csilentflag = true;
1031 sort_opts_vals.cflag = true;
1032 sort_opts_vals.csilentflag = true;
1036 sort_opts_vals.complex_sort = true;
1037 sort_opts_vals.kflag = true;
1040 keys = sort_realloc(keys, keys_num *
1041 sizeof(struct key_specs));
1042 memset(&(keys[keys_num - 1]), 0,
1043 sizeof(struct key_specs));
1045 if (parse_k(optarg, &(keys[keys_num - 1]))
1047 errc(2, EINVAL, "-k %s", optarg);
1053 sort_opts_vals.mflag = true;
1056 outfile = sort_realloc(outfile, (strlen(optarg) + 1));
1057 strcpy(outfile, optarg);
1060 sort_opts_vals.sflag = true;
1063 available_free_memory =
1064 parse_memory_buffer_value(optarg);
1067 tmpdir = sort_strdup(optarg);
1070 while (strlen(optarg) > 1) {
1071 if (optarg[0] != '\\') {
1072 errc(2, EINVAL, "%s", optarg);
1075 if (*optarg == '0') {
1080 sort_opts_vals.tflag = true;
1081 sort_opts_vals.field_sep = btowc(optarg[0]);
1082 if (sort_opts_vals.field_sep == WEOF) {
1086 if (!gnusort_numeric_compatibility) {
1087 if (symbol_decimal_point == sort_opts_vals.field_sep)
1088 symbol_decimal_point = WEOF;
1089 if (symbol_thousands_sep == sort_opts_vals.field_sep)
1090 symbol_thousands_sep = WEOF;
1091 if (symbol_negative_sign == sort_opts_vals.field_sep)
1092 symbol_negative_sign = WEOF;
1093 if (symbol_positive_sign == sort_opts_vals.field_sep)
1094 symbol_positive_sign = WEOF;
1098 sort_opts_vals.uflag = true;
1099 /* stable sort for the correct unique val */
1100 sort_opts_vals.sflag = true;
1103 sort_opts_vals.zflag = true;
1107 if (!strcmp(optarg, "general-numeric"))
1108 set_sort_modifier(sm, 'g');
1109 else if (!strcmp(optarg, "human-numeric"))
1110 set_sort_modifier(sm, 'h');
1111 else if (!strcmp(optarg, "numeric"))
1112 set_sort_modifier(sm, 'n');
1113 else if (!strcmp(optarg, "month"))
1114 set_sort_modifier(sm, 'M');
1115 else if (!strcmp(optarg, "random"))
1116 set_sort_modifier(sm, 'R');
1121 #if defined(SORT_THREADS)
1123 nthreads = (size_t)(atoi(optarg));
1126 if (nthreads > 1024)
1131 sort_opts_vals.sort_method = SORT_QSORT;
1134 sort_opts_vals.sort_method = SORT_MERGESORT;
1140 sort_opts_vals.sort_method = SORT_HEAPSORT;
1143 sort_opts_vals.sort_method = SORT_RADIXSORT;
1145 case RANDOMSOURCE_OPT:
1146 random_source = strdup(optarg);
1148 case COMPRESSPROGRAM_OPT:
1149 compress_program = strdup(optarg);
1152 read_fns_from_file0(optarg);
1157 long mof = strtol(optarg, NULL, 10);
1159 err(2, "--batch-size");
1161 max_open_files = (size_t) mof + 1;
1165 printf("%s\n", VERSION);
1186 if (argv_from_file0) {
1187 argc = argc_from_file0;
1188 argv = argv_from_file0;
1191 if (sort_opts_vals.cflag && sort_opts_vals.mflag)
1192 errx(1, "%c:%c: %s", 'm', 'c', getstr(1));
1194 if (keys_num == 0) {
1196 keys = sort_realloc(keys, sizeof(struct key_specs));
1197 memset(&(keys[0]), 0, sizeof(struct key_specs));
1199 keys[0].pos1b = default_sort_mods->bflag;
1200 keys[0].pos2b = default_sort_mods->bflag;
1201 memcpy(&(keys[0].sm), default_sort_mods,
1202 sizeof(struct sort_mods));
1205 for (size_t i = 0; i < keys_num; i++) {
1206 struct key_specs *ks;
1210 if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) &&
1212 ks->pos1b = sm->bflag;
1213 ks->pos2b = sm->bflag;
1214 memcpy(&(ks->sm), sm, sizeof(struct sort_mods));
1217 ks->sm.func = get_sort_func(&(ks->sm));
1221 printf("Memory to be used for sorting: %llu\n",available_free_memory);
1222 #if defined(SORT_THREADS)
1223 printf("Number of CPUs: %d\n",(int)ncpu);
1226 printf("Using collate rules of %s locale\n",
1227 setlocale(LC_COLLATE, NULL));
1229 printf("Byte sort is used\n");
1230 if (print_symbols_on_debug) {
1231 printf("Decimal Point: <%lc>\n", symbol_decimal_point);
1232 if (symbol_thousands_sep)
1233 printf("Thousands separator: <%lc>\n",
1234 symbol_thousands_sep);
1235 printf("Positive sign: <%lc>\n", symbol_positive_sign);
1236 printf("Negative sign: <%lc>\n", symbol_negative_sign);
1241 get_random_seed(random_source);
1243 /* Case when the outfile equals one of the input files: */
1244 if (strcmp(outfile, "-")) {
1246 for(int i = 0; i < argc; ++i) {
1247 if (strcmp(argv[i], outfile) == 0) {
1248 real_outfile = sort_strdup(outfile);
1250 char* tmp = sort_malloc(strlen(outfile) +
1251 strlen(".tmp") + 1);
1253 strcpy(tmp, outfile);
1254 strcpy(tmp + strlen(tmp), ".tmp");
1257 if (access(outfile, F_OK) < 0)
1260 tmp_file_atexit(outfile);
1265 #if defined(SORT_THREADS)
1266 if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0))
1270 if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) {
1271 struct file_list fl;
1272 struct sort_list list;
1274 sort_list_init(&list);
1275 file_list_init(&fl, true);
1278 procfile("-", &list, &fl);
1281 procfile(*argv, &list, &fl);
1288 sort_list_to_file(&list, outfile);
1290 if (list.count > 0) {
1291 char *flast = new_tmp_file_name();
1293 sort_list_to_file(&list, flast);
1294 file_list_add(&fl, flast, false);
1296 merge_files(&fl, outfile);
1299 file_list_clean(&fl);
1302 * We are about to exit the program, so we can ignore
1303 * the clean-up for speed
1305 * sort_list_clean(&list);
1308 } else if (sort_opts_vals.cflag) {
1309 result = (argc == 0) ? (check("-")) : (check(*argv));
1310 } else if (sort_opts_vals.mflag) {
1311 struct file_list fl;
1313 file_list_init(&fl, false);
1314 /* No file arguments remaining means "read from stdin." */
1316 file_list_add(&fl, "-", true);
1318 file_list_populate(&fl, argc, argv, true);
1319 merge_files(&fl, outfile);
1320 file_list_clean(&fl);
1324 unlink(real_outfile);
1325 if (rename(outfile, real_outfile) < 0)
1327 sort_free(real_outfile);