2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
60 nl_catd catalog = (nl_catd)-1;
63 #define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz"
65 static bool need_random;
70 * Default messages to use when NLS is disabled or no catalogue
73 const char *nlsstr[] = { "",
74 /* 1*/"mutually exclusive flags",
75 /* 2*/"extra argument not allowed with -c",
76 /* 3*/"Unknown feature",
77 /* 4*/"Wrong memory buffer specification",
78 /* 5*/"0 field in key specs",
79 /* 6*/"0 column in key specs",
80 /* 7*/"Wrong file mode",
81 /* 8*/"Cannot open file for reading",
82 /* 9*/"Radix sort cannot be used with these sort options",
83 /*10*/"The chosen sort method cannot be used with stable and/or unique sort",
84 /*11*/"Invalid key position",
85 /*12*/"Usage: %s [-bcCdfigMmnrsuz] [-kPOS1[,POS2] ... ] "
86 "[+POS1 [-POS2]] [-S memsize] [-T tmpdir] [-t separator] "
87 "[-o outfile] [--batch-size size] [--files0-from file] "
88 "[--heapsort] [--mergesort] [--radixsort] [--qsort] "
90 #if defined(SORT_THREADS)
91 "[--parallel thread_no] "
93 "[--human-numeric-sort] "
94 "[--version-sort] [--random-sort [--random-source file]] "
95 "[--compress-program program] [file ...]\n" };
97 struct sort_opts sort_opts_vals;
104 #if defined(SORT_THREADS)
105 unsigned int ncpu = 1;
109 static bool gnusort_numeric_compatibility;
111 static struct sort_mods default_sort_mods_object;
112 struct sort_mods * const default_sort_mods = &default_sort_mods_object;
114 static bool print_symbols_on_debug;
117 * Arguments from file (when file0-from option is used:
119 static size_t argc_from_file0 = (size_t)-1;
120 static char **argv_from_file0;
123 * Placeholder symbols for options which have no single-character equivalent
127 SORT_OPT = CHAR_MAX + 1,
133 #if defined(SORT_THREADS)
145 #define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
146 static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };
148 static struct option long_options[] = {
149 { "batch-size", required_argument, NULL, BS_OPT },
150 { "buffer-size", required_argument, NULL, 'S' },
151 { "check", optional_argument, NULL, 'c' },
152 { "check=silent|quiet", optional_argument, NULL, 'C' },
153 { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT },
154 { "debug", no_argument, NULL, DEBUG_OPT },
155 { "dictionary-order", no_argument, NULL, 'd' },
156 { "field-separator", required_argument, NULL, 't' },
157 { "files0-from", required_argument, NULL, FF_OPT },
158 { "general-numeric-sort", no_argument, NULL, 'g' },
159 { "heapsort", no_argument, NULL, HEAPSORT_OPT },
160 { "help",no_argument, NULL, HELP_OPT },
161 { "human-numeric-sort", no_argument, NULL, 'h' },
162 { "ignore-leading-blanks", no_argument, NULL, 'b' },
163 { "ignore-case", no_argument, NULL, 'f' },
164 { "ignore-nonprinting", no_argument, NULL, 'i' },
165 { "key", required_argument, NULL, 'k' },
166 { "merge", no_argument, NULL, 'm' },
167 { "mergesort", no_argument, NULL, MERGESORT_OPT },
168 { "mmap", no_argument, NULL, MMAP_OPT },
169 { "month-sort", no_argument, NULL, 'M' },
170 { "numeric-sort", no_argument, NULL, 'n' },
171 { "output", required_argument, NULL, 'o' },
172 #if defined(SORT_THREADS)
173 { "parallel", required_argument, NULL, PARALLEL_OPT },
175 { "qsort", no_argument, NULL, QSORT_OPT },
176 { "radixsort", no_argument, NULL, RADIXSORT_OPT },
177 { "random-sort", no_argument, NULL, 'R' },
178 { "random-source", required_argument, NULL, RANDOMSOURCE_OPT },
179 { "reverse", no_argument, NULL, 'r' },
180 { "sort", required_argument, NULL, SORT_OPT },
181 { "stable", no_argument, NULL, 's' },
182 { "temporary-directory",required_argument, NULL, 'T' },
183 { "unique", no_argument, NULL, 'u' },
184 { "version", no_argument, NULL, VERSION_OPT },
185 { "version-sort",no_argument, NULL, 'V' },
186 { "zero-terminated", no_argument, NULL, 'z' },
187 { NULL, no_argument, NULL, 0 }
190 void fix_obsolete_keys(int *argc, char **argv);
193 * Check where sort modifier is present
196 sort_modifier_empty(struct sort_mods *sm)
201 return (!(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag ||
202 sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag));
206 * Print out usage text.
213 out = opt_err ? stderr : stdout;
215 fprintf(out, getstr(12), getprogname());
222 * Read input file names from a file (file0-from option).
225 read_fns_from_file0(const char *fn)
239 while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
241 if (argc_from_file0 == (size_t) - 1)
244 argv_from_file0 = sort_realloc(argv_from_file0,
245 argc_from_file0 * sizeof(char *));
246 if (argv_from_file0 == NULL)
248 argv_from_file0[argc_from_file0 - 1] = line;
256 err(2, "%s: getdelim", fn);
262 * Check how much RAM is available for the sort.
269 #if defined(SORT_THREADS)
273 pages = sysconf(_SC_PHYS_PAGES);
275 perror("sysconf pages");
278 psize = sysconf(_SC_PAGESIZE);
280 perror("sysconf psize");
283 #if defined(SORT_THREADS)
284 ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
293 free_memory = (unsigned long long) pages * (unsigned long long) psize;
294 available_free_memory = free_memory / 2;
296 if (available_free_memory < 1024)
297 available_free_memory = 1024;
301 * Convert "plain" symbol to wide symbol, with default value.
304 conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def)
310 res = mbtowc(wc, c, mb_cur_max);
317 * Set current locale symbols.
325 setlocale(LC_ALL, "");
327 mb_cur_max = MB_CUR_MAX;
332 /* obtain LC_NUMERIC info */
333 /* Convert to wide char form */
334 conv_mbtowc(&symbol_decimal_point, lc->decimal_point,
335 symbol_decimal_point);
336 conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep,
337 symbol_thousands_sep);
338 conv_mbtowc(&symbol_positive_sign, lc->positive_sign,
339 symbol_positive_sign);
340 conv_mbtowc(&symbol_negative_sign, lc->negative_sign,
341 symbol_negative_sign);
344 if (getenv("GNUSORT_NUMERIC_COMPATIBILITY"))
345 gnusort_numeric_compatibility = true;
347 locale = setlocale(LC_COLLATE, NULL);
351 const char *cclocale;
353 tmpl = sort_strdup(locale);
354 cclocale = setlocale(LC_COLLATE, "C");
355 if (cclocale && !strcmp(cclocale, tmpl))
358 const char *pclocale;
360 pclocale = setlocale(LC_COLLATE, "POSIX");
361 if (pclocale && !strcmp(pclocale, tmpl))
364 setlocale(LC_COLLATE, tmpl);
370 * Set directory temporary files.
377 td = getenv("TMPDIR");
379 tmpdir = sort_strdup(td);
385 static unsigned long long
386 parse_memory_buffer_value(const char *value)
390 return (available_free_memory);
393 unsigned long long membuf;
397 membuf = strtoll(value, &endptr, 10);
400 warn("%s",getstr(4));
401 membuf = available_free_memory;
432 membuf = (available_free_memory * membuf) /
436 warnc(EINVAL, "%s", optarg);
437 membuf = available_free_memory;
445 * Signal handler that clears the temporary files.
448 sig_handler(int sig __unused, siginfo_t *siginfo __unused,
449 void *context __unused)
457 * Set signal handler on panic signals.
460 set_signal_handler(void)
464 memset(&sa, 0, sizeof(sa));
465 sa.sa_sigaction = &sig_handler;
466 sa.sa_flags = SA_SIGINFO;
468 if (sigaction(SIGTERM, &sa, NULL) < 0) {
472 if (sigaction(SIGHUP, &sa, NULL) < 0) {
476 if (sigaction(SIGINT, &sa, NULL) < 0) {
480 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
484 if (sigaction(SIGABRT, &sa, NULL) < 0) {
488 if (sigaction(SIGBUS, &sa, NULL) < 0) {
492 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
496 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
500 if (sigaction(SIGUSR2, &sa, NULL) < 0) {
507 * Print "unknown" message and exit with status 2.
510 unknown(const char *what)
513 errx(2, "%s: %s", getstr(3), what);
517 * Check whether contradictory input options are used.
520 check_mutually_exclusive_flags(char c, bool *mef_flags)
523 bool found_others, found_this;
525 found_others = found_this = false;
528 for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {
529 mec = mutually_exclusive_flags[i];
534 errx(1, "%c:%c: %s", c, mec, getstr(1));
540 errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1));
548 * Initialise sort opts data.
554 memset(&default_sort_mods_object, 0,
555 sizeof(default_sort_mods_object));
556 memset(&sort_opts_vals, 0, sizeof(sort_opts_vals));
557 default_sort_mods_object.func =
558 get_sort_func(&default_sort_mods_object);
562 * Set a sort modifier on a sort modifiers object.
565 set_sort_modifier(struct sort_mods *sm, int c)
601 print_symbols_on_debug = true;
612 print_symbols_on_debug = true;
618 sort_opts_vals.complex_sort = true;
619 sm->func = get_sort_func(sm);
624 * Parse POS in -k option.
627 parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second)
629 regmatch_t pmatch[4];
632 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$";
640 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
643 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
646 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
649 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
652 len = pmatch[1].rm_eo - pmatch[1].rm_so;
653 f = sort_malloc((len + 1) * sizeof(char));
655 strncpy(f, s + pmatch[1].rm_so, len);
660 ks->f2 = (size_t) strtoul(f, NULL, 10);
664 warn("%s",getstr(5));
669 ks->f1 = (size_t) strtoul(f, NULL, 10);
673 warn("%s",getstr(5));
678 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
679 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
680 c = sort_malloc((len + 1) * sizeof(char));
682 strncpy(c, s + pmatch[2].rm_so + 1, len);
687 ks->c2 = (size_t) strtoul(c, NULL, 10);
692 ks->c1 = (size_t) strtoul(c, NULL, 10);
696 warn("%s",getstr(6));
707 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
710 for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) {
711 check_mutually_exclusive_flags(s[i], mef_flags);
717 } else if (!set_sort_modifier(&(ks->sm), s[i]))
736 * Parse -k option value.
739 parse_k(const char *s, struct key_specs *ks)
742 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
743 { false, false, false, false, false, false };
748 sptr = strchr(s, ',');
757 pos1 = sort_malloc((size1 + 1) * sizeof(char));
759 strncpy(pos1, s, size1);
762 ret = parse_pos(pos1, ks, mef_flags, false);
768 pos2 = sort_strdup(sptr + 1);
769 ret = parse_pos(pos2, ks, mef_flags, true);
772 ret = parse_pos(s, ks, mef_flags, false);
779 * Parse POS in +POS -POS option.
782 parse_pos_obs(const char *s, int *nf, int *nc, char* sopts)
785 regmatch_t pmatch[4];
787 const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$";
796 if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
799 if (regexec(&re, s, nmatch, pmatch, 0) != 0)
802 if (pmatch[0].rm_eo <= pmatch[0].rm_so)
805 if (pmatch[1].rm_eo <= pmatch[1].rm_so)
808 len = pmatch[1].rm_eo - pmatch[1].rm_so;
809 f = sort_malloc((len + 1) * sizeof(char));
811 strncpy(f, s + pmatch[1].rm_so, len);
815 *nf = (size_t) strtoul(f, NULL, 10);
817 errx(2, "%s", getstr(11));
819 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
820 len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
821 c = sort_malloc((len + 1) * sizeof(char));
823 strncpy(c, s + pmatch[2].rm_so + 1, len);
827 *nc = (size_t) strtoul(c, NULL, 10);
829 errx(2, "%s", getstr(11));
832 if (pmatch[3].rm_eo > pmatch[3].rm_so) {
834 len = pmatch[3].rm_eo - pmatch[3].rm_so;
836 strncpy(sopts, s + pmatch[3].rm_so, len);
853 * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax
856 fix_obsolete_keys(int *argc, char **argv)
860 for (int i = 1; i < *argc; i++) {
865 if (strcmp(arg1, "--") == 0) {
866 /* Following arguments are treated as filenames. */
870 if (strlen(arg1) > 1 && arg1[0] == '+') {
877 if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1) < 0)
883 char *arg2 = argv[i + 1];
885 if (strlen(arg2) > 1 &&
893 if (parse_pos_obs(arg2 + 1,
894 &f2, &c2, sopts2) >= 0) {
897 sprintf(sopt, "-k%d.%d%s,%d.%d%s",
898 f1, c1, sopts1, f2, c2, sopts2);
899 argv[i] = sort_strdup(sopt);
900 for (int j = i + 1; j + 1 < *argc; j++)
901 argv[j] = argv[j + 1];
907 sprintf(sopt, "-k%d.%d%s", f1, c1, sopts1);
908 argv[i] = sort_strdup(sopt);
918 get_random_seed(const char *random_source)
921 struct stat fsb, rsb;
926 rd = sizeof(randseed);
928 if (random_source == NULL) {
929 if (getentropy(randseed, sizeof(randseed)) < 0)
930 err(EX_SOFTWARE, "getentropy");
934 rsfd = open(random_source, O_RDONLY | O_CLOEXEC);
936 err(EX_NOINPUT, "open: %s", random_source);
938 if (fstat(rsfd, &fsb) != 0)
939 err(EX_SOFTWARE, "fstat");
941 if (!S_ISREG(fsb.st_mode) && !S_ISCHR(fsb.st_mode))
943 "random seed isn't a regular file or /dev/random");
946 * Regular files: read up to maximum seed size and explicitly
947 * reject longer files.
949 if (S_ISREG(fsb.st_mode)) {
950 if (fsb.st_size > (off_t)sizeof(randseed))
951 errx(EX_USAGE, "random seed is too large (%jd >"
952 " %zu)!", (intmax_t)fsb.st_size,
954 else if (fsb.st_size < 1)
955 errx(EX_USAGE, "random seed is too small ("
958 memset(randseed, 0, sizeof(randseed));
960 rd = read(rsfd, randseed, fsb.st_size);
962 err(EX_SOFTWARE, "reading random seed file %s",
964 if (rd < (ssize_t)fsb.st_size)
965 errx(EX_SOFTWARE, "short read from %s", random_source);
966 } else if (S_ISCHR(fsb.st_mode)) {
967 if (stat("/dev/random", &rsb) < 0)
968 err(EX_SOFTWARE, "stat");
970 if (fsb.st_dev != rsb.st_dev ||
971 fsb.st_ino != rsb.st_ino)
972 errx(EX_USAGE, "random seed is a character "
973 "device other than /dev/random");
975 if (getentropy(randseed, sizeof(randseed)) < 0)
976 err(EX_SOFTWARE, "getentropy");
984 MD5Update(&md5_ctx, randseed, rd);
991 main(int argc, char **argv)
993 char *outfile, *real_outfile;
994 char *random_source = NULL;
996 bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
997 { false, false, false, false, false, false };
1000 outfile = sort_strdup("-");
1001 real_outfile = NULL;
1003 struct sort_mods *sm = &default_sort_mods_object;
1007 set_signal_handler();
1015 catalog = catopen("sort", NL_CAT_LOCALE);
1018 fix_obsolete_keys(&argc, argv);
1020 while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
1023 check_mutually_exclusive_flags(c, mef_flags);
1025 if (!set_sort_modifier(sm, c)) {
1029 sort_opts_vals.cflag = true;
1031 if (!strcmp(optarg, "diagnose-first"))
1033 else if (!strcmp(optarg, "silent") ||
1034 !strcmp(optarg, "quiet"))
1035 sort_opts_vals.csilentflag = true;
1041 sort_opts_vals.cflag = true;
1042 sort_opts_vals.csilentflag = true;
1046 sort_opts_vals.complex_sort = true;
1047 sort_opts_vals.kflag = true;
1050 keys = sort_realloc(keys, keys_num *
1051 sizeof(struct key_specs));
1052 memset(&(keys[keys_num - 1]), 0,
1053 sizeof(struct key_specs));
1055 if (parse_k(optarg, &(keys[keys_num - 1]))
1057 errc(2, EINVAL, "-k %s", optarg);
1063 sort_opts_vals.mflag = true;
1066 outfile = sort_realloc(outfile, (strlen(optarg) + 1));
1067 strcpy(outfile, optarg);
1070 sort_opts_vals.sflag = true;
1073 available_free_memory =
1074 parse_memory_buffer_value(optarg);
1077 tmpdir = sort_strdup(optarg);
1080 while (strlen(optarg) > 1) {
1081 if (optarg[0] != '\\') {
1082 errc(2, EINVAL, "%s", optarg);
1085 if (*optarg == '0') {
1090 sort_opts_vals.tflag = true;
1091 sort_opts_vals.field_sep = btowc(optarg[0]);
1092 if (sort_opts_vals.field_sep == WEOF) {
1096 if (!gnusort_numeric_compatibility) {
1097 if (symbol_decimal_point == sort_opts_vals.field_sep)
1098 symbol_decimal_point = WEOF;
1099 if (symbol_thousands_sep == sort_opts_vals.field_sep)
1100 symbol_thousands_sep = WEOF;
1101 if (symbol_negative_sign == sort_opts_vals.field_sep)
1102 symbol_negative_sign = WEOF;
1103 if (symbol_positive_sign == sort_opts_vals.field_sep)
1104 symbol_positive_sign = WEOF;
1108 sort_opts_vals.uflag = true;
1109 /* stable sort for the correct unique val */
1110 sort_opts_vals.sflag = true;
1113 sort_opts_vals.zflag = true;
1117 if (!strcmp(optarg, "general-numeric"))
1118 set_sort_modifier(sm, 'g');
1119 else if (!strcmp(optarg, "human-numeric"))
1120 set_sort_modifier(sm, 'h');
1121 else if (!strcmp(optarg, "numeric"))
1122 set_sort_modifier(sm, 'n');
1123 else if (!strcmp(optarg, "month"))
1124 set_sort_modifier(sm, 'M');
1125 else if (!strcmp(optarg, "random"))
1126 set_sort_modifier(sm, 'R');
1131 #if defined(SORT_THREADS)
1133 nthreads = (size_t)(atoi(optarg));
1136 if (nthreads > 1024)
1141 sort_opts_vals.sort_method = SORT_QSORT;
1144 sort_opts_vals.sort_method = SORT_MERGESORT;
1150 sort_opts_vals.sort_method = SORT_HEAPSORT;
1153 sort_opts_vals.sort_method = SORT_RADIXSORT;
1155 case RANDOMSOURCE_OPT:
1156 random_source = strdup(optarg);
1158 case COMPRESSPROGRAM_OPT:
1159 compress_program = strdup(optarg);
1162 read_fns_from_file0(optarg);
1167 long mof = strtol(optarg, NULL, 10);
1169 err(2, "--batch-size");
1171 max_open_files = (size_t) mof + 1;
1175 printf("%s\n", VERSION);
1196 if (argv_from_file0) {
1197 argc = argc_from_file0;
1198 argv = argv_from_file0;
1201 if (sort_opts_vals.cflag && sort_opts_vals.mflag)
1202 errx(1, "%c:%c: %s", 'm', 'c', getstr(1));
1204 if (keys_num == 0) {
1206 keys = sort_realloc(keys, sizeof(struct key_specs));
1207 memset(&(keys[0]), 0, sizeof(struct key_specs));
1209 keys[0].pos1b = default_sort_mods->bflag;
1210 keys[0].pos2b = default_sort_mods->bflag;
1211 memcpy(&(keys[0].sm), default_sort_mods,
1212 sizeof(struct sort_mods));
1215 for (size_t i = 0; i < keys_num; i++) {
1216 struct key_specs *ks;
1220 if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) &&
1222 ks->pos1b = sm->bflag;
1223 ks->pos2b = sm->bflag;
1224 memcpy(&(ks->sm), sm, sizeof(struct sort_mods));
1227 ks->sm.func = get_sort_func(&(ks->sm));
1231 printf("Memory to be used for sorting: %llu\n",available_free_memory);
1232 #if defined(SORT_THREADS)
1233 printf("Number of CPUs: %d\n",(int)ncpu);
1236 printf("Using collate rules of %s locale\n",
1237 setlocale(LC_COLLATE, NULL));
1239 printf("Byte sort is used\n");
1240 if (print_symbols_on_debug) {
1241 printf("Decimal Point: <%lc>\n", symbol_decimal_point);
1242 if (symbol_thousands_sep)
1243 printf("Thousands separator: <%lc>\n",
1244 symbol_thousands_sep);
1245 printf("Positive sign: <%lc>\n", symbol_positive_sign);
1246 printf("Negative sign: <%lc>\n", symbol_negative_sign);
1251 get_random_seed(random_source);
1253 /* Case when the outfile equals one of the input files: */
1254 if (strcmp(outfile, "-")) {
1256 for(int i = 0; i < argc; ++i) {
1257 if (strcmp(argv[i], outfile) == 0) {
1258 real_outfile = sort_strdup(outfile);
1260 char* tmp = sort_malloc(strlen(outfile) +
1261 strlen(".tmp") + 1);
1263 strcpy(tmp, outfile);
1264 strcpy(tmp + strlen(tmp), ".tmp");
1267 if (access(outfile, F_OK) < 0)
1270 tmp_file_atexit(outfile);
1275 #if defined(SORT_THREADS)
1276 if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0))
1280 if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) {
1281 struct file_list fl;
1282 struct sort_list list;
1284 sort_list_init(&list);
1285 file_list_init(&fl, true);
1288 procfile("-", &list, &fl);
1291 procfile(*argv, &list, &fl);
1298 sort_list_to_file(&list, outfile);
1300 if (list.count > 0) {
1301 char *flast = new_tmp_file_name();
1303 sort_list_to_file(&list, flast);
1304 file_list_add(&fl, flast, false);
1306 merge_files(&fl, outfile);
1309 file_list_clean(&fl);
1312 * We are about to exit the program, so we can ignore
1313 * the clean-up for speed
1315 * sort_list_clean(&list);
1318 } else if (sort_opts_vals.cflag) {
1319 result = (argc == 0) ? (check("-")) : (check(*argv));
1320 } else if (sort_opts_vals.mflag) {
1321 struct file_list fl;
1323 file_list_init(&fl, false);
1324 /* No file arguments remaining means "read from stdin." */
1326 file_list_add(&fl, "-", true);
1328 file_list_populate(&fl, argc, argv, true);
1329 merge_files(&fl, outfile);
1330 file_list_clean(&fl);
1334 unlink(real_outfile);
1335 if (rename(outfile, real_outfile) < 0)
1337 sort_free(real_outfile);
1343 if (catalog != (nl_catd)-1)