summaryrefslogtreecommitdiff
path: root/usr.bin/sort/sort.c
blob: 6594ab63a73879d2275c26195d1cf789eb21e392 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
/*	$OpenBSD: sort.c,v 1.79 2015/04/05 13:56:04 millert Exp $	*/

/*-
 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/types.h>

#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <locale.h>
#include <md5.h>
#include <regex.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>

#include "coll.h"
#include "file.h"
#include "sort.h"

#ifdef GNUSORT_COMPATIBILITY
# define PERMUTE	""
#else
# define PERMUTE	"+"
#endif
#define	OPTIONS	PERMUTE"bCcdfgHhik:Mmno:RrS:st:T:uVz"

static bool need_random;
static const char *random_source;

MD5_CTX md5_ctx;

struct sort_opts sort_opts_vals;

bool debug_sort;
bool need_hint;

static bool gnusort_numeric_compatibility;

static struct sort_mods default_sort_mods_object;
struct sort_mods * const default_sort_mods = &default_sort_mods_object;

static bool print_symbols_on_debug;

/*
 * Arguments from file (when file0-from option is used:
 */
static size_t argc_from_file0 = (size_t)-1;
static char **argv_from_file0;

/*
 * Placeholder symbols for options which have no single-character equivalent
 */
enum {
	SORT_OPT = CHAR_MAX + 1,
	HELP_OPT,
	FF_OPT,
	BS_OPT,
	VERSION_OPT,
	DEBUG_OPT,
	RANDOMSOURCE_OPT,
	COMPRESSPROGRAM_OPT,
	QSORT_OPT,
	HEAPSORT_OPT,
	RADIXSORT_OPT,
	MMAP_OPT
};

#define	NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };

static const struct option long_options[] = {
    { "batch-size", required_argument, NULL, BS_OPT },
    { "buffer-size", required_argument, NULL, 'S' },
    { "check", optional_argument, NULL, 'c' },
    { "check=silent|quiet", optional_argument, NULL, 'C' },
    { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT },
    { "debug", no_argument, NULL, DEBUG_OPT },
    { "dictionary-order", no_argument, NULL, 'd' },
    { "field-separator", required_argument, NULL, 't' },
    { "files0-from", required_argument, NULL, FF_OPT },
    { "general-numeric-sort", no_argument, NULL, 'g' },
    { "heapsort", no_argument, NULL, HEAPSORT_OPT },
    { "help", no_argument, NULL, HELP_OPT },
    { "human-numeric-sort", no_argument, NULL, 'h' },
    { "ignore-leading-blanks", no_argument, NULL, 'b' },
    { "ignore-case", no_argument, NULL, 'f' },
    { "ignore-nonprinting", no_argument, NULL, 'i' },
    { "key", required_argument, NULL, 'k' },
    { "merge", no_argument, NULL, 'm' },
    { "mergesort", no_argument, NULL, 'H' },
    { "mmap", no_argument, NULL, MMAP_OPT },
    { "month-sort", no_argument, NULL, 'M' },
    { "numeric-sort", no_argument, NULL, 'n' },
    { "output", required_argument, NULL, 'o' },
    { "qsort", no_argument, NULL, QSORT_OPT },
    { "radixsort", no_argument, NULL, RADIXSORT_OPT },
    { "random-sort", no_argument, NULL, 'R' },
    { "random-source", required_argument, NULL, RANDOMSOURCE_OPT },
    { "reverse", no_argument, NULL, 'r' },
    { "sort", required_argument, NULL, SORT_OPT },
    { "stable", no_argument, NULL, 's' },
    { "temporary-directory", required_argument, NULL, 'T' },
    { "unique", no_argument, NULL, 'u' },
    { "version", no_argument, NULL, VERSION_OPT },
    { "version-sort", no_argument, NULL, 'V' },
    { "zero-terminated", no_argument, NULL, 'z' },
    { NULL, no_argument, NULL, 0 }
};

/*
 * Check where sort modifier is present
 */
static bool
sort_modifier_empty(struct sort_mods *sm)
{
	return !(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag ||
	    sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag);
}

/*
 * Print out usage text.
 */
static __dead void
usage(int exit_val)
{
	fprintf(exit_val ? stderr : stdout,
	    "usage: %s [-bCcdfgHhiMmnRrsuVz] [-k field1[,field2]] [-o output] "
	    "[-S size]\n\t[-T dir] [-t char] [file ...]\n", getprogname());
	exit(exit_val);
}

/*
 * Read input file names from a file (file0-from option).
 */
static void
read_fns_from_file0(const char *fn)
{
	FILE *f;
	char *line = NULL;
	size_t linesize = 0;
	ssize_t linelen;

	f = fopen(fn, "r");
	if (f == NULL)
		err(2, "%s", fn);

	while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
		if (*line != '\0') {
			if (argc_from_file0 == (size_t)-1)
				argc_from_file0 = 0;
			++argc_from_file0;
			argv_from_file0 = sort_reallocarray(argv_from_file0,
			    argc_from_file0, sizeof(char *));
			argv_from_file0[argc_from_file0 - 1] = line;
		} else {
			free(line);
		}
		line = NULL;
		linesize = 0;
	}
	if (ferror(f))
		err(2, "%s: getdelim", fn);

	closefile(f, fn);
}

/*
 * Check how much RAM is available for the sort.
 */
static void
set_hw_params(void)
{
	unsigned long long free_memory;
	long long user_memory;
	struct rlimit rl;
	size_t len;
	int mib[] = { CTL_HW, HW_USERMEM64 };

	/* Get total user (non-kernel) memory. */
	len = sizeof(user_memory);
	if (sysctl(mib, 2, &user_memory, &len, NULL, 0) == -1)
	    user_memory = -1;

	/* Increase our data size to the max */
	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
		free_memory = (unsigned long long)rl.rlim_cur;
		rl.rlim_cur = rl.rlim_max;
		if (setrlimit(RLIMIT_DATA, &rl) == 0) {
			free_memory = (unsigned long long)rl.rlim_max;
		} else {
			warn("Can't set resource limit to max data size");
		}
	} else {
		free_memory = 1000000;
		warn("Can't get resource limit for data size");
	}

	/* We prefer to use temp files rather than swap space. */
	if (user_memory != -1 && free_memory > user_memory)
		free_memory = user_memory;

	available_free_memory = free_memory / 2;
}

/*
 * Convert "plain" symbol to wide symbol, with default value.
 */
static void
conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def)
{
	int res;

	res = mbtowc(wc, c, MB_CUR_MAX);
	if (res < 1)
		*wc = def;
}

/*
 * Set current locale symbols.
 */
static void
set_locale(void)
{
	struct lconv *lc;
	const char *locale;

	setlocale(LC_ALL, "");

	/* Obtain LC_NUMERIC info */
	lc = localeconv();

	/* Convert to wide char form */
	conv_mbtowc(&symbol_decimal_point, lc->decimal_point,
	    symbol_decimal_point);
	conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep,
	    symbol_thousands_sep);
	conv_mbtowc(&symbol_positive_sign, lc->positive_sign,
	    symbol_positive_sign);
	conv_mbtowc(&symbol_negative_sign, lc->negative_sign,
	    symbol_negative_sign);

	if (getenv("GNUSORT_NUMERIC_COMPATIBILITY"))
		gnusort_numeric_compatibility = true;

	locale = setlocale(LC_COLLATE, NULL);
	if (locale != NULL) {
		char *tmpl;
		const char *byteclocale;

		tmpl = sort_strdup(locale);
		byteclocale = setlocale(LC_COLLATE, "C");
		if (byteclocale && strcmp(byteclocale, tmpl) == 0) {
			byte_sort = true;
		} else {
			byteclocale = setlocale(LC_COLLATE, "POSIX");
			if (byteclocale && strcmp(byteclocale, tmpl) == 0)
				byte_sort = true;
			else
				setlocale(LC_COLLATE, tmpl);
		}
		sort_free(tmpl);
	}
	if (!byte_sort)
		sort_mb_cur_max = MB_CUR_MAX;
}

/*
 * Set directory temporary files.
 */
static void
set_tmpdir(void)
{
	if (!issetugid()) {
		char *td;

		td = getenv("TMPDIR");
		if (td != NULL)
			tmpdir = td;
	}
}

/*
 * Parse -S option.
 */
static unsigned long long
parse_memory_buffer_value(const char *value)
{
	char *endptr;
	unsigned long long membuf;

	membuf = strtoll(value, &endptr, 10);
	if (endptr == value || (long long)membuf < 0 ||
	    (errno == ERANGE && membuf == LLONG_MAX))
		goto invalid;

	switch (*endptr) {
	case 'Y':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'Z':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'E':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'P':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'T':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'G':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'M':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case '\0':
	case 'K':
		if (membuf > ULLONG_MAX / 1024)
			goto invalid;
		membuf *= 1024;
		/* FALLTHROUGH */
	case 'b':
		break;
	case '%':
		if (available_free_memory != 0 &&
		    membuf > ULLONG_MAX / available_free_memory)
			goto invalid;
		membuf = (available_free_memory * membuf) /
		    100;
		break;
	default:
		warnc(EINVAL, "%s", optarg);
		membuf = available_free_memory;
	}
	if (membuf > SIZE_MAX)
		goto invalid;
	return membuf;
invalid:
	errx(2, "invalid memory buffer size: %s", value);
}

/*
 * Signal handler that clears the temporary files.
 */
static void
sig_handler(int sig __unused)
{
	clear_tmp_files();
	_exit(2);
}

/*
 * Set signal handler on panic signals.
 */
static void
set_signal_handler(void)
{
	struct sigaction sa;
	int i, signals[] = {SIGTERM, SIGHUP, SIGINT, SIGUSR1, SIGUSR2,
	    SIGPIPE, SIGXCPU, SIGXFSZ, 0};

	memset(&sa, 0, sizeof(sa));
	sigfillset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	sa.sa_handler = sig_handler;

	for (i = 0; signals[i] != 0; i++) {
		if (sigaction(signals[i], &sa, NULL) < 0) {
			warn("sigaction(%s)", strsignal(signals[i]));
			continue;
		}
	}
}

/*
 * Print "unknown" message and exit with status 2.
 */
static void
unknown(const char *what)
{
	errx(2, "Unknown feature: %s", what);
}

/*
 * Check whether contradictory input options are used.
 */
static void
check_mutually_exclusive_flags(char c, bool *mef_flags)
{
	int i, fo_index, mec;
	bool found_others, found_this;

	found_others = found_this = false;
	fo_index = 0;

	for (i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {
		mec = mutually_exclusive_flags[i];

		if (mec != c) {
			if (mef_flags[i]) {
				if (found_this) {
					errx(2,
					    "%c:%c: mutually exclusive flags",
					    c, mec);
				}
				found_others = true;
				fo_index = i;
			}
		} else {
			if (found_others) {
				errx(2,
				    "%c:%c: mutually exclusive flags",
				    c, mutually_exclusive_flags[fo_index]);
			}
			mef_flags[i] = true;
			found_this = true;
		}
	}
}

/*
 * Initialise sort opts data.
 */
static void
set_sort_opts(void)
{
	memset(&default_sort_mods_object, 0,
	    sizeof(default_sort_mods_object));
	memset(&sort_opts_vals, 0, sizeof(sort_opts_vals));
	default_sort_mods_object.func =
	    get_sort_func(&default_sort_mods_object);
}

/*
 * Set a sort modifier on a sort modifiers object.
 */
static bool
set_sort_modifier(struct sort_mods *sm, int c)
{
	switch (c) {
	case 'b':
		sm->bflag = true;
		break;
	case 'd':
		sm->dflag = true;
		break;
	case 'f':
		sm->fflag = true;
		break;
	case 'g':
		sm->gflag = true;
		need_hint = true;
		break;
	case 'i':
		sm->iflag = true;
		break;
	case 'R':
		sm->Rflag = true;
		need_random = true;
		break;
	case 'M':
		initialise_months();
		sm->Mflag = true;
		need_hint = true;
		break;
	case 'n':
		sm->nflag = true;
		need_hint = true;
		print_symbols_on_debug = true;
		break;
	case 'r':
		sm->rflag = true;
		break;
	case 'V':
		sm->Vflag = true;
		break;
	case 'h':
		sm->hflag = true;
		need_hint = true;
		print_symbols_on_debug = true;
		break;
	default:
		return false;
	}
	sort_opts_vals.complex_sort = true;
	sm->func = get_sort_func(sm);

	return true;
}

/*
 * Parse POS in -k option.
 */
static int
parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second)
{
	regmatch_t pmatch[4];
	regex_t re;
	char *c, *f;
	const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$";
	size_t len, nmatch;
	int ret;

	ret = -1;
	nmatch = 4;
	c = f = NULL;

	if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
		return -1;

	if (regexec(&re, s, nmatch, pmatch, 0) != 0)
		goto end;

	if (pmatch[0].rm_eo <= pmatch[0].rm_so)
		goto end;

	if (pmatch[1].rm_eo <= pmatch[1].rm_so)
		goto end;

	len = pmatch[1].rm_eo - pmatch[1].rm_so;

	f = sort_malloc(len + 1);
	memcpy(f, s + pmatch[1].rm_so, len);
	f[len] = '\0';

	if (second) {
		errno = 0;
		ks->f2 = (size_t)strtoul(f, NULL, 10);
		if (errno != 0)
			goto end;
		if (ks->f2 == 0) {
			warn("0 field in key specs");
			goto end;
		}
	} else {
		errno = 0;
		ks->f1 = (size_t)strtoul(f, NULL, 10);
		if (errno != 0)
			goto end;
		if (ks->f1 == 0) {
			warn("0 field in key specs");
			goto end;
		}
	}

	if (pmatch[2].rm_eo > pmatch[2].rm_so) {
		len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;

		c = sort_malloc(len + 1);
		memcpy(c, s + pmatch[2].rm_so + 1, len);
		c[len] = '\0';

		if (second) {
			errno = 0;
			ks->c2 = (size_t)strtoul(c, NULL, 10);
			if (errno != 0)
				goto end;
		} else {
			errno = 0;
			ks->c1 = (size_t)strtoul(c, NULL, 10);
			if (errno != 0)
				goto end;
			if (ks->c1 == 0) {
				warn("0 column in key specs");
				goto end;
			}
		}
	} else {
		if (second)
			ks->c2 = 0;
		else
			ks->c1 = 1;
	}

	if (pmatch[3].rm_eo > pmatch[3].rm_so) {
		regoff_t i = 0;

		for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) {
			check_mutually_exclusive_flags(s[i], mef_flags);
			if (s[i] == 'b') {
				if (second)
					ks->pos2b = true;
				else
					ks->pos1b = true;
			} else if (!set_sort_modifier(&(ks->sm), s[i]))
				goto end;
		}
	}

	ret = 0;

end:
	sort_free(c);
	sort_free(f);
	regfree(&re);

	return ret;
}

/*
 * Parse -k option value.
 */
static int
parse_k(const char *s, struct key_specs *ks)
{
	int ret = -1;
	bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
	    { false, false, false, false, false, false };

	if (*s != '\0') {
		char *sptr;

		sptr = strchr(s, ',');
		if (sptr) {
			size_t size1;
			char *pos1, *pos2;

			size1 = sptr - s;

			if (size1 < 1)
				return -1;

			pos1 = sort_malloc(size1 + 1);
			memcpy(pos1, s, size1);
			pos1[size1] = '\0';

			ret = parse_pos(pos1, ks, mef_flags, false);

			sort_free(pos1);
			if (ret < 0)
				return ret;

			pos2 = sort_strdup(sptr + 1);
			ret = parse_pos(pos2, ks, mef_flags, true);
			sort_free(pos2);
		} else
			ret = parse_pos(s, ks, mef_flags, false);
	}

	return ret;
}

/*
 * Parse POS in +POS -POS option.
 */
static int
parse_pos_obs(const char *s, size_t *nf, size_t *nc, char *sopts, size_t sopts_size)
{
	regex_t re;
	regmatch_t pmatch[4];
	char *c, *f;
	const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$";
	int ret;
	size_t len, nmatch;

	ret = -1;
	nmatch = 4;
	c = f = NULL;
	*nc = *nf = 0;

	if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
		return -1;

	if (regexec(&re, s, nmatch, pmatch, 0) != 0)
		goto end;

	if (pmatch[0].rm_eo <= pmatch[0].rm_so)
		goto end;

	if (pmatch[1].rm_eo <= pmatch[1].rm_so)
		goto end;

	len = pmatch[1].rm_eo - pmatch[1].rm_so;

	f = sort_malloc(len + 1);
	memcpy(f, s + pmatch[1].rm_so, len);
	f[len] = '\0';

	errno = 0;
	*nf = (size_t)strtoul(f, NULL, 10);
	if (errno != 0)
		errx(2, "Invalid key position");

	if (pmatch[2].rm_eo > pmatch[2].rm_so) {
		len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;

		c = sort_malloc(len + 1);
		memcpy(c, s + pmatch[2].rm_so + 1, len);
		c[len] = '\0';

		errno = 0;
		*nc = (size_t)strtoul(c, NULL, 10);
		if (errno != 0)
			errx(2, "Invalid key position");
	}

	if (pmatch[3].rm_eo > pmatch[3].rm_so) {

		len = pmatch[3].rm_eo - pmatch[3].rm_so;

		if (len >= sopts_size)
			errx(2, "Invalid key position");
		memcpy(sopts, s + pmatch[3].rm_so, len);
		sopts[len] = '\0';
	}

	ret = 0;

end:
	sort_free(c);
	sort_free(f);
	regfree(&re);

	return ret;
}

/*
 * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax
 */
static void
fix_obsolete_keys(int *argc, char **argv)
{
	char sopt[129];
	int i;

	for (i = 1; i < *argc; i++) {
		const char *arg1 = argv[i];

		if (arg1[0] == '+') {
			size_t c1, f1;
			char sopts1[128];

			sopts1[0] = 0;
			c1 = f1 = 0;

			if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1,
			    sizeof(sopts1)) < 0)
				continue;

			f1 += 1;
			c1 += 1;
			if (i + 1 < *argc) {
				const char *arg2 = argv[i + 1];

				if (arg2[0] == '-') {
					size_t c2, f2;
					char sopts2[128];

					sopts2[0] = 0;
					c2 = f2 = 0;

					if (parse_pos_obs(arg2 + 1, &f2, &c2,
					    sopts2, sizeof(sopts2)) >= 0) {
						int j;
						if (c2 > 0)
							f2 += 1;
						snprintf(sopt, sizeof(sopt),
						    "-k%zu.%zu%s,%zu.%zu%s",
						    f1, c1, sopts1, f2,
						    c2, sopts2);
						argv[i] = sort_strdup(sopt);
						for (j = i + 1; j + 1 < *argc; j++)
							argv[j] = argv[j + 1];
						*argc -= 1;
						continue;
					}
				}
			}
			snprintf(sopt, sizeof(sopt), "-k%zu.%zu%s",
			    f1, c1, sopts1);
			argv[i] = sort_strdup(sopt);
		}
	}
}

/*
 * Set random seed
 */
static void
set_random_seed(void)
{
	if (!need_random)
		return;

	MD5Init(&md5_ctx);
	if (random_source != NULL) {
		unsigned char buf[BUFSIZ];
		size_t nr;
		FILE *fp;

		if ((fp = fopen(random_source, "r")) == NULL)
			err(2, "%s", random_source);
		while ((nr = fread(buf, 1, sizeof(buf), fp)) != 0)
			MD5Update(&md5_ctx, buf, nr);
		if (ferror(fp))
			err(2, "%s", random_source);
		fclose(fp);
	} else {
		unsigned char rsd[1024];

		arc4random_buf(rsd, sizeof(rsd));
		MD5Update(&md5_ctx, rsd, sizeof(rsd));
	}
}

/*
 * Main function.
 */
int
main(int argc, char *argv[])
{
	char *outfile, *real_outfile, *sflag;
	int c;
	size_t i;
	struct sort_mods *sm = &default_sort_mods_object;
	bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
	    { false, false, false, false, false, false };

	outfile = "-";
	real_outfile = NULL;
	sflag = NULL;

	init_tmp_files();

	set_signal_handler();

	atexit(clear_tmp_files);

	set_hw_params();
	set_locale();
	set_tmpdir();
	set_sort_opts();

	fix_obsolete_keys(&argc, argv);

	while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
	    != -1)) {

		check_mutually_exclusive_flags(c, mef_flags);

		if (!set_sort_modifier(sm, c)) {
			switch (c) {
			case 'c':
				sort_opts_vals.cflag = true;
				if (optarg) {
					if (!strcmp(optarg, "diagnose-first"))
						;
					else if (!strcmp(optarg, "silent") ||
					    !strcmp(optarg, "quiet"))
						sort_opts_vals.csilentflag = true;
					else if (*optarg)
						unknown(optarg);
				}
				break;
			case 'C':
				sort_opts_vals.cflag = true;
				sort_opts_vals.csilentflag = true;
				break;
			case 'k':
			{
				sort_opts_vals.complex_sort = true;
				sort_opts_vals.kflag = true;

				keys = sort_reallocarray(keys, keys_num + 1,
				    sizeof(struct key_specs));
				memset(&(keys[keys_num]), 0,
				    sizeof(struct key_specs));
#ifndef GNUSORT_COMPATIBILITY
				keys[keys_num].pos1b = default_sort_mods->bflag;
				keys[keys_num].pos2b = default_sort_mods->bflag;
#endif

				if (parse_k(optarg, &(keys[keys_num++])) < 0)
					errc(2, EINVAL, "-k %s", optarg);

				break;
			}
			case 'm':
				sort_opts_vals.mflag = true;
				break;
			case 'o':
				outfile = optarg;
				break;
			case 's':
				sort_opts_vals.sflag = true;
				break;
			case 'S':
				sflag = optarg;
				break;
			case 'T':
				tmpdir = optarg;
				break;
			case 't':
				while (strlen(optarg) > 1) {
					if (optarg[0] != '\\') {
						errc(2, EINVAL, "%s", optarg);
					}
					optarg += 1;
					if (*optarg == '0') {
						*optarg = 0;
						break;
					}
				}
				sort_opts_vals.tflag = true;
				sort_opts_vals.field_sep = btowc(optarg[0]);
				if (sort_opts_vals.field_sep == WEOF) {
					errno = EINVAL;
					err(2, NULL);
				}
				if (!gnusort_numeric_compatibility) {
					if (symbol_decimal_point == sort_opts_vals.field_sep)
						symbol_decimal_point = WEOF;
					if (symbol_thousands_sep == sort_opts_vals.field_sep)
						symbol_thousands_sep = WEOF;
					if (symbol_negative_sign == sort_opts_vals.field_sep)
						symbol_negative_sign = WEOF;
					if (symbol_positive_sign == sort_opts_vals.field_sep)
						symbol_positive_sign = WEOF;
				}
				break;
			case 'u':
				sort_opts_vals.uflag = true;
				/* stable sort for the correct unique val */
				sort_opts_vals.sflag = true;
				break;
			case 'z':
				sort_opts_vals.zflag = true;
				break;
			case SORT_OPT:
				if (!strcmp(optarg, "general-numeric"))
					set_sort_modifier(sm, 'g');
				else if (!strcmp(optarg, "human-numeric"))
					set_sort_modifier(sm, 'h');
				else if (!strcmp(optarg, "numeric"))
					set_sort_modifier(sm, 'n');
				else if (!strcmp(optarg, "month"))
					set_sort_modifier(sm, 'M');
				else if (!strcmp(optarg, "random"))
					set_sort_modifier(sm, 'R');
				else
					unknown(optarg);
				break;
			case QSORT_OPT:
				sort_opts_vals.sort_method = SORT_QSORT;
				break;
			case 'H':
				sort_opts_vals.sort_method = SORT_MERGESORT;
				break;
			case MMAP_OPT:
				use_mmap = true;
				break;
			case HEAPSORT_OPT:
				sort_opts_vals.sort_method = SORT_HEAPSORT;
				break;
			case RADIXSORT_OPT:
				sort_opts_vals.sort_method = SORT_RADIXSORT;
				break;
			case RANDOMSOURCE_OPT:
				random_source = optarg;
				break;
			case COMPRESSPROGRAM_OPT:
				compress_program = optarg;
				break;
			case FF_OPT:
				read_fns_from_file0(optarg);
				break;
			case BS_OPT:
			{
				const char *errstr;

				max_open_files = strtonum(optarg, 2,
				    UINT_MAX - 1, &errstr) + 1;
				if (errstr != NULL)
					errx(2, "--batch-size argument is %s",
					    errstr);
				break;
			}
			case VERSION_OPT:
				printf("%s\n", VERSION);
				exit(EXIT_SUCCESS);
				/* NOTREACHED */
				break;
			case DEBUG_OPT:
				debug_sort = true;
				break;
			case HELP_OPT:
				usage(0);
				/* NOTREACHED */
				break;
			default:
				usage(2);
				/* NOTREACHED */
			}
		}
	}
	argc -= optind;
	argv += optind;

#ifndef GNUSORT_COMPATIBILITY
	if (argc > 2 && strcmp(argv[argc - 2], "-o") == 0) {
		outfile = argv[argc - 1];
		argc -= 2;
	}
#endif

	if (sort_opts_vals.cflag && argc > 1)
		errx(2, "only one input file is allowed with the -%c flag",
		    sort_opts_vals.csilentflag ? 'C' : 'c');

	if (sflag != NULL)
		available_free_memory = parse_memory_buffer_value(sflag);

	if (keys_num == 0) {
		keys_num = 1;
		keys = sort_reallocarray(keys, 1, sizeof(struct key_specs));
		memset(&(keys[0]), 0, sizeof(struct key_specs));
		keys[0].c1 = 1;
#ifdef GNUSORT_COMPATIBILITY
		keys[0].pos1b = sm->bflag;
		keys[0].pos2b = sm->bflag;
#endif
		memcpy(&(keys[0].sm), sm, sizeof(struct sort_mods));
	}

	for (i = 0; i < keys_num; i++) {
		struct key_specs *ks;

		ks = &(keys[i]);

		if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) &&
		    !(ks->pos2b)) {
#ifdef GNUSORT_COMPATIBILITY
			ks->pos1b = sm->bflag;
			ks->pos2b = sm->bflag;
#endif
			memcpy(&(ks->sm), sm, sizeof(struct sort_mods));
		}

		ks->sm.func = get_sort_func(&(ks->sm));
	}

	if (argv_from_file0) {
		argc = argc_from_file0;
		argv = argv_from_file0;
	}

	if (debug_sort) {
		printf("Memory to be used for sorting: %llu\n",
		    available_free_memory);
		printf("Using collate rules of %s locale\n",
		    setlocale(LC_COLLATE, NULL));
		if (byte_sort)
			printf("Byte sort is used\n");
		if (print_symbols_on_debug) {
			printf("Decimal Point: <%lc>\n", symbol_decimal_point);
			if (symbol_thousands_sep)
				printf("Thousands separator: <%lc>\n",
				    symbol_thousands_sep);
			printf("Positive sign: <%lc>\n", symbol_positive_sign);
			printf("Negative sign: <%lc>\n", symbol_negative_sign);
		}
	}

	if (sort_opts_vals.cflag)
		return check(argc ? *argv : "-");

	set_random_seed();

	/* Case when the outfile equals one of the input files: */
	if (strcmp(outfile, "-") != 0) {
		struct stat sb;
		int fd, i;

		for (i = 0; i < argc; ++i) {
			if (strcmp(argv[i], outfile) == 0) {
				if (stat(outfile, &sb) == -1)
					err(2, "%s", outfile);
				if (access(outfile, W_OK) == -1)
					err(2, "%s", outfile);
				real_outfile = outfile;
				sort_asprintf(&outfile, "%s.XXXXXXXXXX",
				    real_outfile);
				if ((fd = mkstemp(outfile)) == -1 ||
				    fchmod(fd, sb.st_mode & ALLPERMS) == -1)
					err(2, "%s", outfile);
				close(fd);
				tmp_file_atexit(outfile);
				break;
			}
		}
	}

	if (!sort_opts_vals.mflag) {
		struct file_list fl;
		struct sort_list list;

		sort_list_init(&list);
		file_list_init(&fl, true);

		if (argc < 1)
			procfile("-", &list, &fl);
		else {
			while (argc > 0) {
				procfile(*argv, &list, &fl);
				--argc;
				++argv;
			}
		}

		if (fl.count < 1)
			sort_list_to_file(&list, outfile);
		else {
			if (list.count > 0) {
				char *flast = new_tmp_file_name();

				sort_list_to_file(&list, flast);
				file_list_add(&fl, flast, false);
			}
			merge_files(&fl, outfile);
		}

		file_list_clean(&fl);

		/*
		 * We are about to exit the program, so we can ignore
		 * the clean-up for speed
		 *
		 * sort_list_clean(&list);
		 */

	} else {
		struct file_list fl;

		file_list_init(&fl, false);
		file_list_populate(&fl, argc, argv, true);
		merge_files(&fl, outfile);
		file_list_clean(&fl);
	}

	if (real_outfile) {
		if (rename(outfile, real_outfile) < 0)
			err(2, "%s", real_outfile);
		sort_free(outfile);
	}

	return 0;
}