sort: simplify -o handling to avoid fdopen, assert
[platform/upstream/coreutils.git] / src / sort.c
1 /* sort - sort lines of text (with all kinds of options).
2    Copyright (C) 1988-2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17    Written December 1988 by Mike Haertel.
18    The author may be reached (Email) at the address mike@gnu.ai.mit.edu,
19    or (US mail) as Mike Haertel c/o Free Software Foundation.
20
21    Ørn E. Hansen added NLS support in 1997.  */
22
23 #include <config.h>
24
25 #include <getopt.h>
26 #include <pthread.h>
27 #include <sys/resource.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <signal.h>
31 #include <assert.h>
32 #include "system.h"
33 #include "argmatch.h"
34 #include "error.h"
35 #include "fadvise.h"
36 #include "filevercmp.h"
37 #include "hard-locale.h"
38 #include "hash.h"
39 #include "heap.h"
40 #include "ignore-value.h"
41 #include "md5.h"
42 #include "mbswidth.h"
43 #include "nproc.h"
44 #include "physmem.h"
45 #include "posixver.h"
46 #include "quote.h"
47 #include "quotearg.h"
48 #include "randread.h"
49 #include "readtokens0.h"
50 #include "stdio--.h"
51 #include "stdlib--.h"
52 #include "strnumcmp.h"
53 #include "xmemcoll.h"
54 #include "xnanosleep.h"
55 #include "xstrtol.h"
56
57 #ifndef RLIMIT_DATA
58 struct rlimit { size_t rlim_cur; };
59 # define getrlimit(Resource, Rlp) (-1)
60 #endif
61
62 /* The official name of this program (e.g., no 'g' prefix).  */
63 #define PROGRAM_NAME "sort"
64
65 #define AUTHORS \
66   proper_name ("Mike Haertel"), \
67   proper_name ("Paul Eggert")
68
69 #if HAVE_LANGINFO_CODESET
70 # include <langinfo.h>
71 #endif
72
73 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
74    present.  */
75 #ifndef SA_NOCLDSTOP
76 # define SA_NOCLDSTOP 0
77 /* No sigprocmask.  Always 'return' zero. */
78 # define sigprocmask(How, Set, Oset) (0)
79 # define sigset_t int
80 # if ! HAVE_SIGINTERRUPT
81 #  define siginterrupt(sig, flag) /* empty */
82 # endif
83 #endif
84
85 #if !defined OPEN_MAX && defined NR_OPEN
86 # define OPEN_MAX NR_OPEN
87 #endif
88 #if !defined OPEN_MAX
89 # define OPEN_MAX 20
90 #endif
91
92 #define UCHAR_LIM (UCHAR_MAX + 1)
93
94 #if HAVE_C99_STRTOLD
95 # define long_double long double
96 #else
97 # define long_double double
98 # undef strtold
99 # define strtold strtod
100 #endif
101
102 #ifndef DEFAULT_TMPDIR
103 # define DEFAULT_TMPDIR "/tmp"
104 #endif
105
106 /* Maximum number of lines to merge every time a NODE is taken from
107    the merge queue.  Node is at LEVEL in the binary merge tree,
108    and is responsible for merging TOTAL lines. */
109 #define MAX_MERGE(total, level) (((total) >> (2 * ((level) + 1))) + 1)
110
111 /* Heuristic value for the number of lines for which it is worth creating
112    a subthread, during an internal merge sort.  I.e., it is a small number
113    of "average" lines for which sorting via two threads is faster than
114    sorting via one on an "average" system.  On a dual-core 2.0 GHz i686
115    system with 3GB of RAM and 2MB of L2 cache, a file containing 128K
116    lines of gensort -a output is sorted slightly faster with --parallel=2
117    than with --parallel=1.  By contrast, using --parallel=1 is about 10%
118    faster than using --parallel=2 with a 64K-line input.  */
119 enum { SUBTHREAD_LINES_HEURISTIC = 128 * 1024 };
120 verify (4 <= SUBTHREAD_LINES_HEURISTIC);
121
122 /* The number of threads after which there are
123    diminishing performance gains.  */
124 enum { DEFAULT_MAX_THREADS = 8 };
125
126 /* Exit statuses.  */
127 enum
128   {
129     /* POSIX says to exit with status 1 if invoked with -c and the
130        input is not properly sorted.  */
131     SORT_OUT_OF_ORDER = 1,
132
133     /* POSIX says any other irregular exit must exit with a status
134        code greater than 1.  */
135     SORT_FAILURE = 2
136   };
137
138 enum
139   {
140     /* The number of times we should try to fork a compression process
141        (we retry if the fork call fails).  We don't _need_ to compress
142        temp files, this is just to reduce disk access, so this number
143        can be small.  Each retry doubles in duration.  */
144     MAX_FORK_TRIES_COMPRESS = 4,
145
146     /* The number of times we should try to fork a decompression process.
147        If we can't fork a decompression process, we can't sort, so this
148        number should be big.  Each retry doubles in duration.  */
149     MAX_FORK_TRIES_DECOMPRESS = 9
150   };
151
152 enum
153   {
154     /* Level of the end-of-merge node, one level above the root. */
155     MERGE_END = 0,
156
157     /* Level of the root node in merge tree. */
158     MERGE_ROOT = 1
159   };
160
161 /* The representation of the decimal point in the current locale.  */
162 static int decimal_point;
163
164 /* Thousands separator; if -1, then there isn't one.  */
165 static int thousands_sep;
166
167 /* Nonzero if the corresponding locales are hard.  */
168 static bool hard_LC_COLLATE;
169 #if HAVE_NL_LANGINFO
170 static bool hard_LC_TIME;
171 #endif
172
173 #define NONZERO(x) ((x) != 0)
174
175 /* The kind of blanks for '-b' to skip in various options. */
176 enum blanktype { bl_start, bl_end, bl_both };
177
178 /* The character marking end of line. Default to \n. */
179 static char eolchar = '\n';
180
181 /* Lines are held in core as counted strings. */
182 struct line
183 {
184   char *text;                   /* Text of the line. */
185   size_t length;                /* Length including final newline. */
186   char *keybeg;                 /* Start of first key. */
187   char *keylim;                 /* Limit of first key. */
188 };
189
190 /* Input buffers. */
191 struct buffer
192 {
193   char *buf;                    /* Dynamically allocated buffer,
194                                    partitioned into 3 regions:
195                                    - input data;
196                                    - unused area;
197                                    - an array of lines, in reverse order.  */
198   size_t used;                  /* Number of bytes used for input data.  */
199   size_t nlines;                /* Number of lines in the line array.  */
200   size_t alloc;                 /* Number of bytes allocated. */
201   size_t left;                  /* Number of bytes left from previous reads. */
202   size_t line_bytes;            /* Number of bytes to reserve for each line. */
203   bool eof;                     /* An EOF has been read.  */
204 };
205
206 /* Sort key.  */
207 struct keyfield
208 {
209   size_t sword;                 /* Zero-origin 'word' to start at. */
210   size_t schar;                 /* Additional characters to skip. */
211   size_t eword;                 /* Zero-origin last 'word' of key. */
212   size_t echar;                 /* Additional characters in field. */
213   bool const *ignore;           /* Boolean array of characters to ignore. */
214   char const *translate;        /* Translation applied to characters. */
215   bool skipsblanks;             /* Skip leading blanks when finding start.  */
216   bool skipeblanks;             /* Skip leading blanks when finding end.  */
217   bool numeric;                 /* Flag for numeric comparison.  Handle
218                                    strings of digits with optional decimal
219                                    point, but no exponential notation. */
220   bool random;                  /* Sort by random hash of key.  */
221   bool general_numeric;         /* Flag for general, numeric comparison.
222                                    Handle numbers in exponential notation. */
223   bool human_numeric;           /* Flag for sorting by human readable
224                                    units with either SI xor IEC prefixes. */
225   bool month;                   /* Flag for comparison by month name. */
226   bool reverse;                 /* Reverse the sense of comparison. */
227   bool version;                 /* sort by version number */
228   bool obsolete_used;           /* obsolescent key option format is used. */
229   struct keyfield *next;        /* Next keyfield to try. */
230 };
231
232 struct month
233 {
234   char const *name;
235   int val;
236 };
237
238 /* Binary merge tree node. */
239 struct merge_node
240 {
241   struct line *lo;              /* Lines to merge from LO child node. */
242   struct line *hi;              /* Lines to merge from HI child ndoe. */
243   struct line *end_lo;          /* End of available lines from LO. */
244   struct line *end_hi;          /* End of available lines from HI. */
245   struct line **dest;           /* Pointer to destination of merge. */
246   size_t nlo;                   /* Total Lines remaining from LO. */
247   size_t nhi;                   /* Total lines remaining from HI. */
248   struct merge_node *parent;    /* Parent node. */
249   struct merge_node *lo_child;  /* LO child node. */
250   struct merge_node *hi_child;  /* HI child node. */
251   unsigned int level;           /* Level in merge tree. */
252   bool queued;                  /* Node is already in heap. */
253   pthread_mutex_t lock;         /* Lock for node operations. */
254 };
255
256 /* Priority queue of merge nodes. */
257 struct merge_node_queue
258 {
259   struct heap *priority_queue;  /* Priority queue of merge tree nodes. */
260   pthread_mutex_t mutex;        /* Lock for queue operations. */
261   pthread_cond_t cond;          /* Conditional wait for empty queue to populate
262                                    when popping. */
263 };
264
265 /* FIXME: None of these tables work with multibyte character sets.
266    Also, there are many other bugs when handling multibyte characters.
267    One way to fix this is to rewrite 'sort' to use wide characters
268    internally, but doing this with good performance is a bit
269    tricky.  */
270
271 /* Table of blanks.  */
272 static bool blanks[UCHAR_LIM];
273
274 /* Table of non-printing characters. */
275 static bool nonprinting[UCHAR_LIM];
276
277 /* Table of non-dictionary characters (not letters, digits, or blanks). */
278 static bool nondictionary[UCHAR_LIM];
279
280 /* Translation table folding lower case to upper.  */
281 static char fold_toupper[UCHAR_LIM];
282
283 #define MONTHS_PER_YEAR 12
284
285 /* Table mapping month names to integers.
286    Alphabetic order allows binary search. */
287 static struct month monthtab[] =
288 {
289   {"APR", 4},
290   {"AUG", 8},
291   {"DEC", 12},
292   {"FEB", 2},
293   {"JAN", 1},
294   {"JUL", 7},
295   {"JUN", 6},
296   {"MAR", 3},
297   {"MAY", 5},
298   {"NOV", 11},
299   {"OCT", 10},
300   {"SEP", 9}
301 };
302
303 /* During the merge phase, the number of files to merge at once. */
304 #define NMERGE_DEFAULT 16
305
306 /* Minimum size for a merge or check buffer.  */
307 #define MIN_MERGE_BUFFER_SIZE (2 + sizeof (struct line))
308
309 /* Minimum sort size; the code might not work with smaller sizes.  */
310 #define MIN_SORT_SIZE (nmerge * MIN_MERGE_BUFFER_SIZE)
311
312 /* The number of bytes needed for a merge or check buffer, which can
313    function relatively efficiently even if it holds only one line.  If
314    a longer line is seen, this value is increased.  */
315 static size_t merge_buffer_size = MAX (MIN_MERGE_BUFFER_SIZE, 256 * 1024);
316
317 /* The approximate maximum number of bytes of main memory to use, as
318    specified by the user.  Zero if the user has not specified a size.  */
319 static size_t sort_size;
320
321 /* The initial allocation factor for non-regular files.
322    This is used, e.g., when reading from a pipe.
323    Don't make it too big, since it is multiplied by ~130 to
324    obtain the size of the actual buffer sort will allocate.
325    Also, there may be 8 threads all doing this at the same time.  */
326 #define INPUT_FILE_SIZE_GUESS (128 * 1024)
327
328 /* Array of directory names in which any temporary files are to be created. */
329 static char const **temp_dirs;
330
331 /* Number of temporary directory names used.  */
332 static size_t temp_dir_count;
333
334 /* Number of allocated slots in temp_dirs.  */
335 static size_t temp_dir_alloc;
336
337 /* Flag to reverse the order of all comparisons. */
338 static bool reverse;
339
340 /* Flag for stable sort.  This turns off the last ditch bytewise
341    comparison of lines, and instead leaves lines in the same order
342    they were read if all keys compare equal.  */
343 static bool stable;
344
345 /* If TAB has this value, blanks separate fields.  */
346 enum { TAB_DEFAULT = CHAR_MAX + 1 };
347
348 /* Tab character separating fields.  If TAB_DEFAULT, then fields are
349    separated by the empty string between a non-blank character and a blank
350    character. */
351 static int tab = TAB_DEFAULT;
352
353 /* Flag to remove consecutive duplicate lines from the output.
354    Only the last of a sequence of equal lines will be output. */
355 static bool unique;
356
357 /* Nonzero if any of the input files are the standard input. */
358 static bool have_read_stdin;
359
360 /* List of key field comparisons to be tried.  */
361 static struct keyfield *keylist;
362
363 /* Program used to (de)compress temp files.  Must accept -d.  */
364 static char const *compress_program;
365
366 /* Annotate the output with extra info to aid the user.  */
367 static bool debug;
368
369 /* Maximum number of files to merge in one go.  If more than this
370    number are present, temp files will be used. */
371 static unsigned int nmerge = NMERGE_DEFAULT;
372
373 /* Report MESSAGE for FILE, then clean up and exit.
374    If FILE is null, it represents standard output.  */
375
376 static void die (char const *, char const *) ATTRIBUTE_NORETURN;
377 static void
378 die (char const *message, char const *file)
379 {
380   error (0, errno, "%s: %s", message, file ? file : _("standard output"));
381   exit (SORT_FAILURE);
382 }
383
384 void
385 usage (int status)
386 {
387   if (status != EXIT_SUCCESS)
388     emit_try_help ();
389   else
390     {
391       printf (_("\
392 Usage: %s [OPTION]... [FILE]...\n\
393   or:  %s [OPTION]... --files0-from=F\n\
394 "),
395               program_name, program_name);
396       fputs (_("\
397 Write sorted concatenation of all FILE(s) to standard output.\n\
398 \n\
399 "), stdout);
400       fputs (_("\
401 Mandatory arguments to long options are mandatory for short options too.\n\
402 "), stdout);
403       fputs (_("\
404 Ordering options:\n\
405 \n\
406 "), stdout);
407       fputs (_("\
408   -b, --ignore-leading-blanks  ignore leading blanks\n\
409   -d, --dictionary-order      consider only blanks and alphanumeric characters\
410 \n\
411   -f, --ignore-case           fold lower case to upper case characters\n\
412 "), stdout);
413       fputs (_("\
414   -g, --general-numeric-sort  compare according to general numerical value\n\
415   -i, --ignore-nonprinting    consider only printable characters\n\
416   -M, --month-sort            compare (unknown) < 'JAN' < ... < 'DEC'\n\
417 "), stdout);
418       fputs (_("\
419   -h, --human-numeric-sort    compare human readable numbers (e.g., 2K 1G)\n\
420 "), stdout);
421       fputs (_("\
422   -n, --numeric-sort          compare according to string numerical value\n\
423   -R, --random-sort           sort by random hash of keys\n\
424       --random-source=FILE    get random bytes from FILE\n\
425   -r, --reverse               reverse the result of comparisons\n\
426 "), stdout);
427       fputs (_("\
428       --sort=WORD             sort according to WORD:\n\
429                                 general-numeric -g, human-numeric -h, month -M,\
430 \n\
431                                 numeric -n, random -R, version -V\n\
432   -V, --version-sort          natural sort of (version) numbers within text\n\
433 \n\
434 "), stdout);
435       fputs (_("\
436 Other options:\n\
437 \n\
438 "), stdout);
439       fputs (_("\
440       --batch-size=NMERGE   merge at most NMERGE inputs at once;\n\
441                             for more use temp files\n\
442 "), stdout);
443       fputs (_("\
444   -c, --check, --check=diagnose-first  check for sorted input; do not sort\n\
445   -C, --check=quiet, --check=silent  like -c, but do not report first bad line\
446 \n\
447       --compress-program=PROG  compress temporaries with PROG;\n\
448                               decompress them with PROG -d\n\
449 "), stdout);
450       fputs (_("\
451       --debug               annotate the part of the line used to sort,\n\
452                               and warn about questionable usage to stderr\n\
453       --files0-from=F       read input from the files specified by\n\
454                             NUL-terminated names in file F;\n\
455                             If F is - then read names from standard input\n\
456 "), stdout);
457       fputs (_("\
458   -k, --key=KEYDEF          sort via a key; KEYDEF gives location and type\n\
459   -m, --merge               merge already sorted files; do not sort\n\
460 "), stdout);
461       fputs (_("\
462   -o, --output=FILE         write result to FILE instead of standard output\n\
463   -s, --stable              stabilize sort by disabling last-resort comparison\
464 \n\
465   -S, --buffer-size=SIZE    use SIZE for main memory buffer\n\
466 "), stdout);
467       printf (_("\
468   -t, --field-separator=SEP  use SEP instead of non-blank to blank transition\n\
469   -T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or %s;\n\
470                               multiple options specify multiple directories\n\
471       --parallel=N          change the number of sorts run concurrently to N\n\
472   -u, --unique              with -c, check for strict ordering;\n\
473                               without -c, output only the first of an equal run\
474 \n\
475 "), DEFAULT_TMPDIR);
476       fputs (_("\
477   -z, --zero-terminated     end lines with 0 byte, not newline\n\
478 "), stdout);
479       fputs (HELP_OPTION_DESCRIPTION, stdout);
480       fputs (VERSION_OPTION_DESCRIPTION, stdout);
481       fputs (_("\
482 \n\
483 KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a\n\
484 field number and C a character position in the field; both are origin 1, and\n\
485 the stop position defaults to the line's end.  If neither -t nor -b is in\n\
486 effect, characters in a field are counted from the beginning of the preceding\n\
487 whitespace.  OPTS is one or more single-letter ordering options [bdfgiMhnRrV],\
488 \n\
489 which override global ordering options for that key.  If no key is given, use\n\
490 the entire line as the key.\n\
491 \n\
492 SIZE may be followed by the following multiplicative suffixes:\n\
493 "), stdout);
494       fputs (_("\
495 % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.\n\
496 \n\
497 With no FILE, or when FILE is -, read standard input.\n\
498 \n\
499 *** WARNING ***\n\
500 The locale specified by the environment affects sort order.\n\
501 Set LC_ALL=C to get the traditional sort order that uses\n\
502 native byte values.\n\
503 "), stdout );
504       emit_ancillary_info ();
505     }
506
507   exit (status);
508 }
509
510 /* For long options that have no equivalent short option, use a
511    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
512 enum
513 {
514   CHECK_OPTION = CHAR_MAX + 1,
515   COMPRESS_PROGRAM_OPTION,
516   DEBUG_PROGRAM_OPTION,
517   FILES0_FROM_OPTION,
518   NMERGE_OPTION,
519   RANDOM_SOURCE_OPTION,
520   SORT_OPTION,
521   PARALLEL_OPTION
522 };
523
524 static char const short_options[] = "-bcCdfghik:mMno:rRsS:t:T:uVy:z";
525
526 static struct option const long_options[] =
527 {
528   {"ignore-leading-blanks", no_argument, NULL, 'b'},
529   {"check", optional_argument, NULL, CHECK_OPTION},
530   {"compress-program", required_argument, NULL, COMPRESS_PROGRAM_OPTION},
531   {"debug", no_argument, NULL, DEBUG_PROGRAM_OPTION},
532   {"dictionary-order", no_argument, NULL, 'd'},
533   {"ignore-case", no_argument, NULL, 'f'},
534   {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
535   {"general-numeric-sort", no_argument, NULL, 'g'},
536   {"ignore-nonprinting", no_argument, NULL, 'i'},
537   {"key", required_argument, NULL, 'k'},
538   {"merge", no_argument, NULL, 'm'},
539   {"month-sort", no_argument, NULL, 'M'},
540   {"numeric-sort", no_argument, NULL, 'n'},
541   {"human-numeric-sort", no_argument, NULL, 'h'},
542   {"version-sort", no_argument, NULL, 'V'},
543   {"random-sort", no_argument, NULL, 'R'},
544   {"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
545   {"sort", required_argument, NULL, SORT_OPTION},
546   {"output", required_argument, NULL, 'o'},
547   {"reverse", no_argument, NULL, 'r'},
548   {"stable", no_argument, NULL, 's'},
549   {"batch-size", required_argument, NULL, NMERGE_OPTION},
550   {"buffer-size", required_argument, NULL, 'S'},
551   {"field-separator", required_argument, NULL, 't'},
552   {"temporary-directory", required_argument, NULL, 'T'},
553   {"unique", no_argument, NULL, 'u'},
554   {"zero-terminated", no_argument, NULL, 'z'},
555   {"parallel", required_argument, NULL, PARALLEL_OPTION},
556   {GETOPT_HELP_OPTION_DECL},
557   {GETOPT_VERSION_OPTION_DECL},
558   {NULL, 0, NULL, 0},
559 };
560
561 #define CHECK_TABLE \
562   _ct_("quiet",          'C') \
563   _ct_("silent",         'C') \
564   _ct_("diagnose-first", 'c')
565
566 static char const *const check_args[] =
567 {
568 #define _ct_(_s, _c) _s,
569   CHECK_TABLE NULL
570 #undef  _ct_
571 };
572 static char const check_types[] =
573 {
574 #define _ct_(_s, _c) _c,
575   CHECK_TABLE
576 #undef  _ct_
577 };
578
579 #define SORT_TABLE \
580   _st_("general-numeric", 'g') \
581   _st_("human-numeric",   'h') \
582   _st_("month",           'M') \
583   _st_("numeric",         'n') \
584   _st_("random",          'R') \
585   _st_("version",         'V')
586
587 static char const *const sort_args[] =
588 {
589 #define _st_(_s, _c) _s,
590   SORT_TABLE NULL
591 #undef  _st_
592 };
593 static char const sort_types[] =
594 {
595 #define _st_(_s, _c) _c,
596   SORT_TABLE
597 #undef  _st_
598 };
599
600 /* The set of signals that are caught.  */
601 static sigset_t caught_signals;
602
603 /* Critical section status.  */
604 struct cs_status
605 {
606   bool valid;
607   sigset_t sigs;
608 };
609
610 /* Enter a critical section.  */
611 static struct cs_status
612 cs_enter (void)
613 {
614   struct cs_status status;
615   status.valid = (sigprocmask (SIG_BLOCK, &caught_signals, &status.sigs) == 0);
616   return status;
617 }
618
619 /* Leave a critical section.  */
620 static void
621 cs_leave (struct cs_status status)
622 {
623   if (status.valid)
624     {
625       /* Ignore failure when restoring the signal mask. */
626       sigprocmask (SIG_SETMASK, &status.sigs, NULL);
627     }
628 }
629
630 /* Possible states for a temp file.  If compressed, the file's status
631    is unreaped or reaped, depending on whether 'sort' has waited for
632    the subprocess to finish.  */
633 enum { UNCOMPRESSED, UNREAPED, REAPED };
634
635 /* The list of temporary files. */
636 struct tempnode
637 {
638   struct tempnode *volatile next;
639   pid_t pid;     /* The subprocess PID; undefined if state == UNCOMPRESSED.  */
640   char state;
641   char name[1];  /* Actual size is 1 + file name length.  */
642 };
643 static struct tempnode *volatile temphead;
644 static struct tempnode *volatile *temptail = &temphead;
645
646 /* A file to be sorted.  */
647 struct sortfile
648 {
649   /* The file's name.  */
650   char const *name;
651
652   /* Nonnull if this is a temporary file, in which case NAME == TEMP->name.  */
653   struct tempnode *temp;
654 };
655
656 /* Map PIDs of unreaped subprocesses to their struct tempnode objects.  */
657 static Hash_table *proctab;
658
659 enum { INIT_PROCTAB_SIZE = 47 };
660
661 static size_t
662 proctab_hasher (void const *entry, size_t tabsize)
663 {
664   struct tempnode const *node = entry;
665   return node->pid % tabsize;
666 }
667
668 static bool
669 proctab_comparator (void const *e1, void const *e2)
670 {
671   struct tempnode const *n1 = e1;
672   struct tempnode const *n2 = e2;
673   return n1->pid == n2->pid;
674 }
675
676 /* The number of unreaped child processes.  */
677 static pid_t nprocs;
678
679 static bool delete_proc (pid_t);
680
681 /* If PID is positive, wait for the child process with that PID to
682    exit, and assume that PID has already been removed from the process
683    table.  If PID is 0 or -1, clean up some child that has exited (by
684    waiting for it, and removing it from the proc table) and return the
685    child's process ID.  However, if PID is 0 and no children have
686    exited, return 0 without waiting.  */
687
688 static pid_t
689 reap (pid_t pid)
690 {
691   int status;
692   pid_t cpid = waitpid ((pid ? pid : -1), &status, (pid ? 0 : WNOHANG));
693
694   if (cpid < 0)
695     error (SORT_FAILURE, errno, _("waiting for %s [-d]"),
696            compress_program);
697   else if (0 < cpid && (0 < pid || delete_proc (cpid)))
698     {
699       if (! WIFEXITED (status) || WEXITSTATUS (status))
700         error (SORT_FAILURE, 0, _("%s [-d] terminated abnormally"),
701                compress_program);
702       --nprocs;
703     }
704
705   return cpid;
706 }
707
708 /* TEMP represents a new process; add it to the process table.  Create
709    the process table the first time it's called.  */
710
711 static void
712 register_proc (struct tempnode *temp)
713 {
714   if (! proctab)
715     {
716       proctab = hash_initialize (INIT_PROCTAB_SIZE, NULL,
717                                  proctab_hasher,
718                                  proctab_comparator,
719                                  NULL);
720       if (! proctab)
721         xalloc_die ();
722     }
723
724   temp->state = UNREAPED;
725
726   if (! hash_insert (proctab, temp))
727     xalloc_die ();
728 }
729
730 /* If PID is in the process table, remove it and return true.
731    Otherwise, return false.  */
732
733 static bool
734 delete_proc (pid_t pid)
735 {
736   struct tempnode test;
737
738   test.pid = pid;
739   struct tempnode *node = hash_delete (proctab, &test);
740   if (! node)
741     return false;
742   node->state = REAPED;
743   return true;
744 }
745
746 /* Remove PID from the process table, and wait for it to exit if it
747    hasn't already.  */
748
749 static void
750 wait_proc (pid_t pid)
751 {
752   if (delete_proc (pid))
753     reap (pid);
754 }
755
756 /* Reap any exited children.  Do not block; reap only those that have
757    already exited.  */
758
759 static void
760 reap_exited (void)
761 {
762   while (0 < nprocs && reap (0))
763     continue;
764 }
765
766 /* Reap at least one exited child, waiting if necessary.  */
767
768 static void
769 reap_some (void)
770 {
771   reap (-1);
772   reap_exited ();
773 }
774
775 /* Reap all children, waiting if necessary.  */
776
777 static void
778 reap_all (void)
779 {
780   while (0 < nprocs)
781     reap (-1);
782 }
783
784 /* Clean up any remaining temporary files.  */
785
786 static void
787 cleanup (void)
788 {
789   struct tempnode const *node;
790
791   for (node = temphead; node; node = node->next)
792     unlink (node->name);
793   temphead = NULL;
794 }
795
796 /* Cleanup actions to take when exiting.  */
797
798 static void
799 exit_cleanup (void)
800 {
801   if (temphead)
802     {
803       /* Clean up any remaining temporary files in a critical section so
804          that a signal handler does not try to clean them too.  */
805       struct cs_status cs = cs_enter ();
806       cleanup ();
807       cs_leave (cs);
808     }
809
810   close_stdout ();
811 }
812
813 /* Create a new temporary file, returning its newly allocated tempnode.
814    Store into *PFD the file descriptor open for writing.
815    If the creation fails, return NULL and store -1 into *PFD if the
816    failure is due to file descriptor exhaustion and
817    SURVIVE_FD_EXHAUSTION; otherwise, die.  */
818
819 static struct tempnode *
820 create_temp_file (int *pfd, bool survive_fd_exhaustion)
821 {
822   static char const slashbase[] = "/sortXXXXXX";
823   static size_t temp_dir_index;
824   int fd;
825   int saved_errno;
826   char const *temp_dir = temp_dirs[temp_dir_index];
827   size_t len = strlen (temp_dir);
828   struct tempnode *node =
829     xmalloc (offsetof (struct tempnode, name) + len + sizeof slashbase);
830   char *file = node->name;
831   struct cs_status cs;
832
833   memcpy (file, temp_dir, len);
834   memcpy (file + len, slashbase, sizeof slashbase);
835   node->next = NULL;
836   if (++temp_dir_index == temp_dir_count)
837     temp_dir_index = 0;
838
839   /* Create the temporary file in a critical section, to avoid races.  */
840   cs = cs_enter ();
841   fd = mkstemp (file);
842   if (0 <= fd)
843     {
844       *temptail = node;
845       temptail = &node->next;
846     }
847   saved_errno = errno;
848   cs_leave (cs);
849   errno = saved_errno;
850
851   if (fd < 0)
852     {
853       if (! (survive_fd_exhaustion && errno == EMFILE))
854         error (SORT_FAILURE, errno, _("cannot create temporary file in %s"),
855                quote (temp_dir));
856       free (node);
857       node = NULL;
858     }
859
860   *pfd = fd;
861   return node;
862 }
863
864 /* Return a stream for FILE, opened with mode HOW.  A null FILE means
865    standard output; HOW should be "w".  When opening for input, "-"
866    means standard input.  To avoid confusion, do not return file
867    descriptors STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO when
868    opening an ordinary FILE.  Return NULL if unsuccessful.
869
870    fadvise() is used to specify an access pattern for input files.
871    There are a few hints we could possibly provide,
872    and after careful testing it was decided that
873    specifying POSIX_FADV_SEQUENTIAL was not detrimental
874    to any cases.  On Linux 2.6.31, this option doubles
875    the size of read ahead performed and thus was seen to
876    benefit these cases:
877      Merging
878      Sorting with a smaller internal buffer
879      Reading from faster flash devices
880
881    In _addition_ one could also specify other hints...
882
883    POSIX_FADV_WILLNEED was tested, but Linux 2.6.31
884    at least uses that to _synchronously_ prepopulate the cache
885    with the specified range.  While sort does need to
886    read all of its input before outputting, a synchronous
887    read of the whole file up front precludes any processing
888    that sort could do in parallel with the system doing
889    read ahead of the data. This was seen to have negative effects
890    in a couple of cases:
891      Merging
892      Sorting with a smaller internal buffer
893    Note this option was seen to shorten the runtime for sort
894    on a multicore system with lots of RAM and other processes
895    competing for CPU.  It could be argued that more explicit
896    scheduling hints with 'nice' et. al. are more appropriate
897    for this situation.
898
899    POSIX_FADV_NOREUSE is a possibility as it could lower
900    the priority of input data in the cache as sort will
901    only need to process it once.  However its functionality
902    has changed over Linux kernel versions and as of 2.6.31
903    it does nothing and thus we can't depend on what it might
904    do in future.
905
906    POSIX_FADV_DONTNEED is not appropriate for user specified
907    input files, but for temp files we do want to drop the
908    cache immediately after processing.  This is done implicitly
909    however when the files are unlinked.  */
910
911 static FILE *
912 stream_open (char const *file, char const *how)
913 {
914   FILE *fp;
915
916   if (*how == 'r')
917     {
918       if (STREQ (file, "-"))
919         {
920           have_read_stdin = true;
921           fp = stdin;
922         }
923       else
924         fp = fopen (file, how);
925       fadvise (fp, FADVISE_SEQUENTIAL);
926     }
927   else if (*how == 'w')
928     {
929       if (file && ftruncate (STDOUT_FILENO, 0) != 0)
930         error (EXIT_FAILURE, errno, _("%s: error truncating"),
931                quote (file));
932       fp = stdout;
933     }
934   else
935     assert (!"unexpected mode passed to stream_open");
936
937   return fp;
938 }
939
940 /* Same as stream_open, except always return a non-null value; die on
941    failure.  */
942
943 static FILE *
944 xfopen (char const *file, char const *how)
945 {
946   FILE *fp = stream_open (file, how);
947   if (!fp)
948     die (_("open failed"), file);
949   return fp;
950 }
951
952 /* Close FP, whose name is FILE, and report any errors.  */
953
954 static void
955 xfclose (FILE *fp, char const *file)
956 {
957   switch (fileno (fp))
958     {
959     case STDIN_FILENO:
960       /* Allow reading stdin from tty more than once.  */
961       if (feof (fp))
962         clearerr (fp);
963       break;
964
965     case STDOUT_FILENO:
966       /* Don't close stdout just yet.  close_stdout does that.  */
967       if (fflush (fp) != 0)
968         die (_("fflush failed"), file);
969       break;
970
971     default:
972       if (fclose (fp) != 0)
973         die (_("close failed"), file);
974       break;
975     }
976 }
977
978 static void
979 move_fd_or_die (int oldfd, int newfd)
980 {
981   if (oldfd != newfd)
982     {
983       if (dup2 (oldfd, newfd) < 0)
984         error (SORT_FAILURE, errno, _("dup2 failed"));
985       close (oldfd);
986     }
987 }
988
989 /* Fork a child process for piping to and do common cleanup.  The
990    TRIES parameter tells us how many times to try to fork before
991    giving up.  Return the PID of the child, or -1 (setting errno)
992    on failure. */
993
994 static pid_t
995 pipe_fork (int pipefds[2], size_t tries)
996 {
997 #if HAVE_WORKING_FORK
998   struct tempnode *saved_temphead;
999   int saved_errno;
1000   double wait_retry = 0.25;
1001   pid_t pid IF_LINT ( = -1);
1002   struct cs_status cs;
1003
1004   if (pipe (pipefds) < 0)
1005     return -1;
1006
1007   /* At least NMERGE + 1 subprocesses are needed.  More could be created, but
1008      uncontrolled subprocess generation can hurt performance significantly.
1009      Allow at most NMERGE + 2 subprocesses, on the theory that there
1010      may be some useful parallelism by letting compression for the
1011      previous merge finish (1 subprocess) in parallel with the current
1012      merge (NMERGE + 1 subprocesses).  */
1013
1014   if (nmerge + 1 < nprocs)
1015     reap_some ();
1016
1017   while (tries--)
1018     {
1019       /* This is so the child process won't delete our temp files
1020          if it receives a signal before exec-ing.  */
1021       cs = cs_enter ();
1022       saved_temphead = temphead;
1023       temphead = NULL;
1024
1025       pid = fork ();
1026       saved_errno = errno;
1027       if (pid)
1028         temphead = saved_temphead;
1029
1030       cs_leave (cs);
1031       errno = saved_errno;
1032
1033       if (0 <= pid || errno != EAGAIN)
1034         break;
1035       else
1036         {
1037           xnanosleep (wait_retry);
1038           wait_retry *= 2;
1039           reap_exited ();
1040         }
1041     }
1042
1043   if (pid < 0)
1044     {
1045       saved_errno = errno;
1046       close (pipefds[0]);
1047       close (pipefds[1]);
1048       errno = saved_errno;
1049     }
1050   else if (pid == 0)
1051     {
1052       close (STDIN_FILENO);
1053       close (STDOUT_FILENO);
1054     }
1055   else
1056     ++nprocs;
1057
1058   return pid;
1059
1060 #else  /* ! HAVE_WORKING_FORK */
1061   return -1;
1062 #endif
1063 }
1064
1065 /* Create a temporary file and, if asked for, start a compressor
1066    to that file.  Set *PFP to the file handle and return
1067    the address of the new temp node.  If the creation
1068    fails, return NULL if the failure is due to file descriptor
1069    exhaustion and SURVIVE_FD_EXHAUSTION; otherwise, die.  */
1070
1071 static struct tempnode *
1072 maybe_create_temp (FILE **pfp, bool survive_fd_exhaustion)
1073 {
1074   int tempfd;
1075   struct tempnode *node = create_temp_file (&tempfd, survive_fd_exhaustion);
1076   if (! node)
1077     return NULL;
1078
1079   node->state = UNCOMPRESSED;
1080
1081   if (compress_program)
1082     {
1083       int pipefds[2];
1084
1085       node->pid = pipe_fork (pipefds, MAX_FORK_TRIES_COMPRESS);
1086       if (0 < node->pid)
1087         {
1088           close (tempfd);
1089           close (pipefds[0]);
1090           tempfd = pipefds[1];
1091
1092           register_proc (node);
1093         }
1094       else if (node->pid == 0)
1095         {
1096           close (pipefds[1]);
1097           move_fd_or_die (tempfd, STDOUT_FILENO);
1098           move_fd_or_die (pipefds[0], STDIN_FILENO);
1099
1100           if (execlp (compress_program, compress_program, (char *) NULL) < 0)
1101             error (SORT_FAILURE, errno, _("couldn't execute %s"),
1102                    compress_program);
1103         }
1104     }
1105
1106   *pfp = fdopen (tempfd, "w");
1107   if (! *pfp)
1108     die (_("couldn't create temporary file"), node->name);
1109
1110   return node;
1111 }
1112
1113 /* Create a temporary file and, if asked for, start a compressor
1114    to that file.  Set *PFP to the file handle and return the address
1115    of the new temp node.  Die on failure.  */
1116
1117 static struct tempnode *
1118 create_temp (FILE **pfp)
1119 {
1120   return maybe_create_temp (pfp, false);
1121 }
1122
1123 /* Open a compressed temp file and start a decompression process through
1124    which to filter the input.  Return NULL (setting errno to
1125    EMFILE) if we ran out of file descriptors, and die on any other
1126    kind of failure.  */
1127
1128 static FILE *
1129 open_temp (struct tempnode *temp)
1130 {
1131   int tempfd, pipefds[2];
1132   FILE *fp = NULL;
1133
1134   if (temp->state == UNREAPED)
1135     wait_proc (temp->pid);
1136
1137   tempfd = open (temp->name, O_RDONLY);
1138   if (tempfd < 0)
1139     return NULL;
1140
1141   pid_t child = pipe_fork (pipefds, MAX_FORK_TRIES_DECOMPRESS);
1142
1143   switch (child)
1144     {
1145     case -1:
1146       if (errno != EMFILE)
1147         error (SORT_FAILURE, errno, _("couldn't create process for %s -d"),
1148                compress_program);
1149       close (tempfd);
1150       errno = EMFILE;
1151       break;
1152
1153     case 0:
1154       close (pipefds[0]);
1155       move_fd_or_die (tempfd, STDIN_FILENO);
1156       move_fd_or_die (pipefds[1], STDOUT_FILENO);
1157
1158       execlp (compress_program, compress_program, "-d", (char *) NULL);
1159       error (SORT_FAILURE, errno, _("couldn't execute %s -d"),
1160              compress_program);
1161
1162     default:
1163       temp->pid = child;
1164       register_proc (temp);
1165       close (tempfd);
1166       close (pipefds[1]);
1167
1168       fp = fdopen (pipefds[0], "r");
1169       if (! fp)
1170         {
1171           int saved_errno = errno;
1172           close (pipefds[0]);
1173           errno = saved_errno;
1174         }
1175       break;
1176     }
1177
1178   return fp;
1179 }
1180
1181 /* Append DIR to the array of temporary directory names.  */
1182 static void
1183 add_temp_dir (char const *dir)
1184 {
1185   if (temp_dir_count == temp_dir_alloc)
1186     temp_dirs = X2NREALLOC (temp_dirs, &temp_dir_alloc);
1187
1188   temp_dirs[temp_dir_count++] = dir;
1189 }
1190
1191 /* Remove NAME from the list of temporary files.  */
1192
1193 static void
1194 zaptemp (char const *name)
1195 {
1196   struct tempnode *volatile *pnode;
1197   struct tempnode *node;
1198   struct tempnode *next;
1199   int unlink_status;
1200   int unlink_errno = 0;
1201   struct cs_status cs;
1202
1203   for (pnode = &temphead; (node = *pnode)->name != name; pnode = &node->next)
1204     continue;
1205
1206   if (node->state == UNREAPED)
1207     wait_proc (node->pid);
1208
1209   /* Unlink the temporary file in a critical section to avoid races.  */
1210   next = node->next;
1211   cs = cs_enter ();
1212   unlink_status = unlink (name);
1213   unlink_errno = errno;
1214   *pnode = next;
1215   cs_leave (cs);
1216
1217   if (unlink_status != 0)
1218     error (0, unlink_errno, _("warning: cannot remove: %s"), name);
1219   if (! next)
1220     temptail = pnode;
1221   free (node);
1222 }
1223
1224 #if HAVE_NL_LANGINFO
1225
1226 static int
1227 struct_month_cmp (void const *m1, void const *m2)
1228 {
1229   struct month const *month1 = m1;
1230   struct month const *month2 = m2;
1231   return strcmp (month1->name, month2->name);
1232 }
1233
1234 #endif
1235
1236 /* Initialize the character class tables. */
1237
1238 static void
1239 inittables (void)
1240 {
1241   size_t i;
1242
1243   for (i = 0; i < UCHAR_LIM; ++i)
1244     {
1245       blanks[i] = !! isblank (i);
1246       nonprinting[i] = ! isprint (i);
1247       nondictionary[i] = ! isalnum (i) && ! isblank (i);
1248       fold_toupper[i] = toupper (i);
1249     }
1250
1251 #if HAVE_NL_LANGINFO
1252   /* If we're not in the "C" locale, read different names for months.  */
1253   if (hard_LC_TIME)
1254     {
1255       for (i = 0; i < MONTHS_PER_YEAR; i++)
1256         {
1257           char const *s;
1258           size_t s_len;
1259           size_t j, k;
1260           char *name;
1261
1262           s = nl_langinfo (ABMON_1 + i);
1263           s_len = strlen (s);
1264           monthtab[i].name = name = xmalloc (s_len + 1);
1265           monthtab[i].val = i + 1;
1266
1267           for (j = k = 0; j < s_len; j++)
1268             if (! isblank (to_uchar (s[j])))
1269               name[k++] = fold_toupper[to_uchar (s[j])];
1270           name[k] = '\0';
1271         }
1272       qsort (monthtab, MONTHS_PER_YEAR, sizeof *monthtab, struct_month_cmp);
1273     }
1274 #endif
1275 }
1276
1277 /* Specify how many inputs may be merged at once.
1278    This may be set on the command-line with the
1279    --batch-size option. */
1280 static void
1281 specify_nmerge (int oi, char c, char const *s)
1282 {
1283   uintmax_t n;
1284   struct rlimit rlimit;
1285   enum strtol_error e = xstrtoumax (s, NULL, 10, &n, NULL);
1286
1287   /* Try to find out how many file descriptors we'll be able
1288      to open.  We need at least nmerge + 3 (STDIN_FILENO,
1289      STDOUT_FILENO and STDERR_FILENO). */
1290   unsigned int max_nmerge = ((getrlimit (RLIMIT_NOFILE, &rlimit) == 0
1291                               ? rlimit.rlim_cur
1292                               : OPEN_MAX)
1293                              - 3);
1294
1295   if (e == LONGINT_OK)
1296     {
1297       nmerge = n;
1298       if (nmerge != n)
1299         e = LONGINT_OVERFLOW;
1300       else
1301         {
1302           if (nmerge < 2)
1303             {
1304               error (0, 0, _("invalid --%s argument %s"),
1305                      long_options[oi].name, quote (s));
1306               error (SORT_FAILURE, 0,
1307                      _("minimum --%s argument is %s"),
1308                      long_options[oi].name, quote ("2"));
1309             }
1310           else if (max_nmerge < nmerge)
1311             {
1312               e = LONGINT_OVERFLOW;
1313             }
1314           else
1315             return;
1316         }
1317     }
1318
1319   if (e == LONGINT_OVERFLOW)
1320     {
1321       char max_nmerge_buf[INT_BUFSIZE_BOUND (max_nmerge)];
1322       error (0, 0, _("--%s argument %s too large"),
1323              long_options[oi].name, quote (s));
1324       error (SORT_FAILURE, 0,
1325              _("maximum --%s argument with current rlimit is %s"),
1326              long_options[oi].name,
1327              uinttostr (max_nmerge, max_nmerge_buf));
1328     }
1329   else
1330     xstrtol_fatal (e, oi, c, long_options, s);
1331 }
1332
1333 /* Specify the amount of main memory to use when sorting.  */
1334 static void
1335 specify_sort_size (int oi, char c, char const *s)
1336 {
1337   uintmax_t n;
1338   char *suffix;
1339   enum strtol_error e = xstrtoumax (s, &suffix, 10, &n, "EgGkKmMPtTYZ");
1340
1341   /* The default unit is KiB.  */
1342   if (e == LONGINT_OK && ISDIGIT (suffix[-1]))
1343     {
1344       if (n <= UINTMAX_MAX / 1024)
1345         n *= 1024;
1346       else
1347         e = LONGINT_OVERFLOW;
1348     }
1349
1350   /* A 'b' suffix means bytes; a '%' suffix means percent of memory.  */
1351   if (e == LONGINT_INVALID_SUFFIX_CHAR && ISDIGIT (suffix[-1]) && ! suffix[1])
1352     switch (suffix[0])
1353       {
1354       case 'b':
1355         e = LONGINT_OK;
1356         break;
1357
1358       case '%':
1359         {
1360           double mem = physmem_total () * n / 100;
1361
1362           /* Use "<", not "<=", to avoid problems with rounding.  */
1363           if (mem < UINTMAX_MAX)
1364             {
1365               n = mem;
1366               e = LONGINT_OK;
1367             }
1368           else
1369             e = LONGINT_OVERFLOW;
1370         }
1371         break;
1372       }
1373
1374   if (e == LONGINT_OK)
1375     {
1376       /* If multiple sort sizes are specified, take the maximum, so
1377          that option order does not matter.  */
1378       if (n < sort_size)
1379         return;
1380
1381       sort_size = n;
1382       if (sort_size == n)
1383         {
1384           sort_size = MAX (sort_size, MIN_SORT_SIZE);
1385           return;
1386         }
1387
1388       e = LONGINT_OVERFLOW;
1389     }
1390
1391   xstrtol_fatal (e, oi, c, long_options, s);
1392 }
1393
1394 /* Specify the number of threads to spawn during internal sort.  */
1395 static size_t
1396 specify_nthreads (int oi, char c, char const *s)
1397 {
1398   unsigned long int nthreads;
1399   enum strtol_error e = xstrtoul (s, NULL, 10, &nthreads, "");
1400   if (e == LONGINT_OVERFLOW)
1401     return SIZE_MAX;
1402   if (e != LONGINT_OK)
1403     xstrtol_fatal (e, oi, c, long_options, s);
1404   if (SIZE_MAX < nthreads)
1405     nthreads = SIZE_MAX;
1406   if (nthreads == 0)
1407     error (SORT_FAILURE, 0, _("number in parallel must be nonzero"));
1408   return nthreads;
1409 }
1410
1411 /* Return the default sort size.  */
1412 static size_t
1413 default_sort_size (void)
1414 {
1415   /* Let SIZE be MEM, but no more than the maximum object size or
1416      system resource limits.  Don't bother to check for values like
1417      RLIM_INFINITY since in practice they are not much less than SIZE_MAX.  */
1418   size_t size = SIZE_MAX;
1419   struct rlimit rlimit;
1420   if (getrlimit (RLIMIT_DATA, &rlimit) == 0 && rlimit.rlim_cur < size)
1421     size = rlimit.rlim_cur;
1422 #ifdef RLIMIT_AS
1423   if (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur < size)
1424     size = rlimit.rlim_cur;
1425 #endif
1426
1427   /* Leave a large safety margin for the above limits, as failure can
1428      occur when they are exceeded.  */
1429   size /= 2;
1430
1431 #ifdef RLIMIT_RSS
1432   /* Leave a 1/16 margin for RSS to leave room for code, stack, etc.
1433      Exceeding RSS is not fatal, but can be quite slow.  */
1434   if (getrlimit (RLIMIT_RSS, &rlimit) == 0 && rlimit.rlim_cur / 16 * 15 < size)
1435     size = rlimit.rlim_cur / 16 * 15;
1436 #endif
1437
1438   /* Let MEM be available memory or 1/8 of total memory, whichever
1439      is greater.  */
1440   double avail = physmem_available ();
1441   double total = physmem_total ();
1442   double mem = MAX (avail, total / 8);
1443
1444   /* Return the minimum of MEM and SIZE, but no less than
1445      MIN_SORT_SIZE.  Avoid the MIN macro here, as it is not quite
1446      right when only one argument is floating point.  */
1447   if (mem < size)
1448     size = mem;
1449   return MAX (size, MIN_SORT_SIZE);
1450 }
1451
1452 /* Return the sort buffer size to use with the input files identified
1453    by FPS and FILES, which are alternate names of the same files.
1454    NFILES gives the number of input files; NFPS may be less.  Assume
1455    that each input line requires LINE_BYTES extra bytes' worth of line
1456    information.  Do not exceed the size bound specified by the user
1457    (or a default size bound, if the user does not specify one).  */
1458
1459 static size_t
1460 sort_buffer_size (FILE *const *fps, size_t nfps,
1461                   char *const *files, size_t nfiles,
1462                   size_t line_bytes)
1463 {
1464   /* A bound on the input size.  If zero, the bound hasn't been
1465      determined yet.  */
1466   static size_t size_bound;
1467
1468   /* In the worst case, each input byte is a newline.  */
1469   size_t worst_case_per_input_byte = line_bytes + 1;
1470
1471   /* Keep enough room for one extra input line and an extra byte.
1472      This extra room might be needed when preparing to read EOF.  */
1473   size_t size = worst_case_per_input_byte + 1;
1474
1475   size_t i;
1476
1477   for (i = 0; i < nfiles; i++)
1478     {
1479       struct stat st;
1480       off_t file_size;
1481       size_t worst_case;
1482
1483       if ((i < nfps ? fstat (fileno (fps[i]), &st)
1484            : STREQ (files[i], "-") ? fstat (STDIN_FILENO, &st)
1485            : stat (files[i], &st))
1486           != 0)
1487         die (_("stat failed"), files[i]);
1488
1489       if (S_ISREG (st.st_mode))
1490         file_size = st.st_size;
1491       else
1492         {
1493           /* The file has unknown size.  If the user specified a sort
1494              buffer size, use that; otherwise, guess the size.  */
1495           if (sort_size)
1496             return sort_size;
1497           file_size = INPUT_FILE_SIZE_GUESS;
1498         }
1499
1500       if (! size_bound)
1501         {
1502           size_bound = sort_size;
1503           if (! size_bound)
1504             size_bound = default_sort_size ();
1505         }
1506
1507       /* Add the amount of memory needed to represent the worst case
1508          where the input consists entirely of newlines followed by a
1509          single non-newline.  Check for overflow.  */
1510       worst_case = file_size * worst_case_per_input_byte + 1;
1511       if (file_size != worst_case / worst_case_per_input_byte
1512           || size_bound - size <= worst_case)
1513         return size_bound;
1514       size += worst_case;
1515     }
1516
1517   return size;
1518 }
1519
1520 /* Initialize BUF.  Reserve LINE_BYTES bytes for each line; LINE_BYTES
1521    must be at least sizeof (struct line).  Allocate ALLOC bytes
1522    initially.  */
1523
1524 static void
1525 initbuf (struct buffer *buf, size_t line_bytes, size_t alloc)
1526 {
1527   /* Ensure that the line array is properly aligned.  If the desired
1528      size cannot be allocated, repeatedly halve it until allocation
1529      succeeds.  The smaller allocation may hurt overall performance,
1530      but that's better than failing.  */
1531   while (true)
1532     {
1533       alloc += sizeof (struct line) - alloc % sizeof (struct line);
1534       buf->buf = malloc (alloc);
1535       if (buf->buf)
1536         break;
1537       alloc /= 2;
1538       if (alloc <= line_bytes + 1)
1539         xalloc_die ();
1540     }
1541
1542   buf->line_bytes = line_bytes;
1543   buf->alloc = alloc;
1544   buf->used = buf->left = buf->nlines = 0;
1545   buf->eof = false;
1546 }
1547
1548 /* Return one past the limit of the line array.  */
1549
1550 static inline struct line *
1551 buffer_linelim (struct buffer const *buf)
1552 {
1553   return (struct line *) (buf->buf + buf->alloc);
1554 }
1555
1556 /* Return a pointer to the first character of the field specified
1557    by KEY in LINE. */
1558
1559 static char *
1560 begfield (struct line const *line, struct keyfield const *key)
1561 {
1562   char *ptr = line->text, *lim = ptr + line->length - 1;
1563   size_t sword = key->sword;
1564   size_t schar = key->schar;
1565
1566   /* The leading field separator itself is included in a field when -t
1567      is absent.  */
1568
1569   if (tab != TAB_DEFAULT)
1570     while (ptr < lim && sword--)
1571       {
1572         while (ptr < lim && *ptr != tab)
1573           ++ptr;
1574         if (ptr < lim)
1575           ++ptr;
1576       }
1577   else
1578     while (ptr < lim && sword--)
1579       {
1580         while (ptr < lim && blanks[to_uchar (*ptr)])
1581           ++ptr;
1582         while (ptr < lim && !blanks[to_uchar (*ptr)])
1583           ++ptr;
1584       }
1585
1586   /* If we're ignoring leading blanks when computing the Start
1587      of the field, skip past them here.  */
1588   if (key->skipsblanks)
1589     while (ptr < lim && blanks[to_uchar (*ptr)])
1590       ++ptr;
1591
1592   /* Advance PTR by SCHAR (if possible), but no further than LIM.  */
1593   ptr = MIN (lim, ptr + schar);
1594
1595   return ptr;
1596 }
1597
1598 /* Return the limit of (a pointer to the first character after) the field
1599    in LINE specified by KEY. */
1600
1601 static char *
1602 limfield (struct line const *line, struct keyfield const *key)
1603 {
1604   char *ptr = line->text, *lim = ptr + line->length - 1;
1605   size_t eword = key->eword, echar = key->echar;
1606
1607   if (echar == 0)
1608     eword++; /* Skip all of end field.  */
1609
1610   /* Move PTR past EWORD fields or to one past the last byte on LINE,
1611      whichever comes first.  If there are more than EWORD fields, leave
1612      PTR pointing at the beginning of the field having zero-based index,
1613      EWORD.  If a delimiter character was specified (via -t), then that
1614      'beginning' is the first character following the delimiting TAB.
1615      Otherwise, leave PTR pointing at the first 'blank' character after
1616      the preceding field.  */
1617   if (tab != TAB_DEFAULT)
1618     while (ptr < lim && eword--)
1619       {
1620         while (ptr < lim && *ptr != tab)
1621           ++ptr;
1622         if (ptr < lim && (eword || echar))
1623           ++ptr;
1624       }
1625   else
1626     while (ptr < lim && eword--)
1627       {
1628         while (ptr < lim && blanks[to_uchar (*ptr)])
1629           ++ptr;
1630         while (ptr < lim && !blanks[to_uchar (*ptr)])
1631           ++ptr;
1632       }
1633
1634 #ifdef POSIX_UNSPECIFIED
1635   /* The following block of code makes GNU sort incompatible with
1636      standard Unix sort, so it's ifdef'd out for now.
1637      The POSIX spec isn't clear on how to interpret this.
1638      FIXME: request clarification.
1639
1640      From: kwzh@gnu.ai.mit.edu (Karl Heuer)
1641      Date: Thu, 30 May 96 12:20:41 -0400
1642      [Translated to POSIX 1003.1-2001 terminology by Paul Eggert.]
1643
1644      [...]I believe I've found another bug in 'sort'.
1645
1646      $ cat /tmp/sort.in
1647      a b c 2 d
1648      pq rs 1 t
1649      $ textutils-1.15/src/sort -k1.7,1.7 </tmp/sort.in
1650      a b c 2 d
1651      pq rs 1 t
1652      $ /bin/sort -k1.7,1.7 </tmp/sort.in
1653      pq rs 1 t
1654      a b c 2 d
1655
1656      Unix sort produced the answer I expected: sort on the single character
1657      in column 7.  GNU sort produced different results, because it disagrees
1658      on the interpretation of the key-end spec "M.N".  Unix sort reads this
1659      as "skip M-1 fields, then N-1 characters"; but GNU sort wants it to mean
1660      "skip M-1 fields, then either N-1 characters or the rest of the current
1661      field, whichever comes first".  This extra clause applies only to
1662      key-ends, not key-starts.
1663      */
1664
1665   /* Make LIM point to the end of (one byte past) the current field.  */
1666   if (tab != TAB_DEFAULT)
1667     {
1668       char *newlim;
1669       newlim = memchr (ptr, tab, lim - ptr);
1670       if (newlim)
1671         lim = newlim;
1672     }
1673   else
1674     {
1675       char *newlim;
1676       newlim = ptr;
1677       while (newlim < lim && blanks[to_uchar (*newlim)])
1678         ++newlim;
1679       while (newlim < lim && !blanks[to_uchar (*newlim)])
1680         ++newlim;
1681       lim = newlim;
1682     }
1683 #endif
1684
1685   if (echar != 0) /* We need to skip over a portion of the end field.  */
1686     {
1687       /* If we're ignoring leading blanks when computing the End
1688          of the field, skip past them here.  */
1689       if (key->skipeblanks)
1690         while (ptr < lim && blanks[to_uchar (*ptr)])
1691           ++ptr;
1692
1693       /* Advance PTR by ECHAR (if possible), but no further than LIM.  */
1694       ptr = MIN (lim, ptr + echar);
1695     }
1696
1697   return ptr;
1698 }
1699
1700 /* Fill BUF reading from FP, moving buf->left bytes from the end
1701    of buf->buf to the beginning first.  If EOF is reached and the
1702    file wasn't terminated by a newline, supply one.  Set up BUF's line
1703    table too.  FILE is the name of the file corresponding to FP.
1704    Return true if some input was read.  */
1705
1706 static bool
1707 fillbuf (struct buffer *buf, FILE *fp, char const *file)
1708 {
1709   struct keyfield const *key = keylist;
1710   char eol = eolchar;
1711   size_t line_bytes = buf->line_bytes;
1712   size_t mergesize = merge_buffer_size - MIN_MERGE_BUFFER_SIZE;
1713
1714   if (buf->eof)
1715     return false;
1716
1717   if (buf->used != buf->left)
1718     {
1719       memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
1720       buf->used = buf->left;
1721       buf->nlines = 0;
1722     }
1723
1724   while (true)
1725     {
1726       char *ptr = buf->buf + buf->used;
1727       struct line *linelim = buffer_linelim (buf);
1728       struct line *line = linelim - buf->nlines;
1729       size_t avail = (char *) linelim - buf->nlines * line_bytes - ptr;
1730       char *line_start = buf->nlines ? line->text + line->length : buf->buf;
1731
1732       while (line_bytes + 1 < avail)
1733         {
1734           /* Read as many bytes as possible, but do not read so many
1735              bytes that there might not be enough room for the
1736              corresponding line array.  The worst case is when the
1737              rest of the input file consists entirely of newlines,
1738              except that the last byte is not a newline.  */
1739           size_t readsize = (avail - 1) / (line_bytes + 1);
1740           size_t bytes_read = fread (ptr, 1, readsize, fp);
1741           char *ptrlim = ptr + bytes_read;
1742           char *p;
1743           avail -= bytes_read;
1744
1745           if (bytes_read != readsize)
1746             {
1747               if (ferror (fp))
1748                 die (_("read failed"), file);
1749               if (feof (fp))
1750                 {
1751                   buf->eof = true;
1752                   if (buf->buf == ptrlim)
1753                     return false;
1754                   if (line_start != ptrlim && ptrlim[-1] != eol)
1755                     *ptrlim++ = eol;
1756                 }
1757             }
1758
1759           /* Find and record each line in the just-read input.  */
1760           while ((p = memchr (ptr, eol, ptrlim - ptr)))
1761             {
1762               /* Delimit the line with NUL. This eliminates the need to
1763                  temporarily replace the last byte with NUL when calling
1764                  xmemcoll(), which increases performance.  */
1765               *p = '\0';
1766               ptr = p + 1;
1767               line--;
1768               line->text = line_start;
1769               line->length = ptr - line_start;
1770               mergesize = MAX (mergesize, line->length);
1771               avail -= line_bytes;
1772
1773               if (key)
1774                 {
1775                   /* Precompute the position of the first key for
1776                      efficiency.  */
1777                   line->keylim = (key->eword == SIZE_MAX
1778                                   ? p
1779                                   : limfield (line, key));
1780
1781                   if (key->sword != SIZE_MAX)
1782                     line->keybeg = begfield (line, key);
1783                   else
1784                     {
1785                       if (key->skipsblanks)
1786                         while (blanks[to_uchar (*line_start)])
1787                           line_start++;
1788                       line->keybeg = line_start;
1789                     }
1790                 }
1791
1792               line_start = ptr;
1793             }
1794
1795           ptr = ptrlim;
1796           if (buf->eof)
1797             break;
1798         }
1799
1800       buf->used = ptr - buf->buf;
1801       buf->nlines = buffer_linelim (buf) - line;
1802       if (buf->nlines != 0)
1803         {
1804           buf->left = ptr - line_start;
1805           merge_buffer_size = mergesize + MIN_MERGE_BUFFER_SIZE;
1806           return true;
1807         }
1808
1809       {
1810         /* The current input line is too long to fit in the buffer.
1811            Double the buffer size and try again, keeping it properly
1812            aligned.  */
1813         size_t line_alloc = buf->alloc / sizeof (struct line);
1814         buf->buf = x2nrealloc (buf->buf, &line_alloc, sizeof (struct line));
1815         buf->alloc = line_alloc * sizeof (struct line);
1816       }
1817     }
1818 }
1819
1820 /* Table that maps characters to order-of-magnitude values.  */
1821 static char const unit_order[UCHAR_LIM] =
1822   {
1823 #if ! ('K' == 75 && 'M' == 77 && 'G' == 71 && 'T' == 84 && 'P' == 80 \
1824      && 'E' == 69 && 'Z' == 90 && 'Y' == 89 && 'k' == 107)
1825     /* This initializer syntax works on all C99 hosts.  For now, use
1826        it only on non-ASCII hosts, to ease the pain of porting to
1827        pre-C99 ASCII hosts.  */
1828     ['K']=1, ['M']=2, ['G']=3, ['T']=4, ['P']=5, ['E']=6, ['Z']=7, ['Y']=8,
1829     ['k']=1,
1830 #else
1831     /* Generate the following table with this command:
1832        perl -e 'my %a=(k=>1, K=>1, M=>2, G=>3, T=>4, P=>5, E=>6, Z=>7, Y=>8);
1833        foreach my $i (0..255) {my $c=chr($i); $a{$c} ||= 0;print "$a{$c}, "}'\
1834        |fmt  */
1835     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1836     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1837     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 3,
1838     0, 0, 0, 1, 0, 2, 0, 0, 5, 0, 0, 0, 4, 0, 0, 0, 0, 8, 7, 0, 0, 0, 0, 0,
1839     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1840     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1841     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1842     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1843     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1844     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1845     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1846 #endif
1847   };
1848
1849 /* Return an integer that represents the order of magnitude of the
1850    unit following the number.  The number may contain thousands
1851    separators and a decimal point, but it may not contain leading blanks.
1852    Negative numbers get negative orders; zero numbers have a zero order.  */
1853
1854 static int _GL_ATTRIBUTE_PURE
1855 find_unit_order (char const *number)
1856 {
1857   bool minus_sign = (*number == '-');
1858   char const *p = number + minus_sign;
1859   int nonzero = 0;
1860   unsigned char ch;
1861
1862   /* Scan to end of number.
1863      Decimals or separators not followed by digits stop the scan.
1864      Numbers ending in decimals or separators are thus considered
1865      to be lacking in units.
1866      FIXME: add support for multibyte thousands_sep and decimal_point.  */
1867
1868   do
1869     {
1870       while (ISDIGIT (ch = *p++))
1871         nonzero |= ch - '0';
1872     }
1873   while (ch == thousands_sep);
1874
1875   if (ch == decimal_point)
1876     while (ISDIGIT (ch = *p++))
1877       nonzero |= ch - '0';
1878
1879   if (nonzero)
1880     {
1881       int order = unit_order[ch];
1882       return (minus_sign ? -order : order);
1883     }
1884   else
1885     return 0;
1886 }
1887
1888 /* Compare numbers A and B ending in units with SI or IEC prefixes
1889        <none/unknown> < K/k < M < G < T < P < E < Z < Y  */
1890
1891 static int
1892 human_numcompare (char const *a, char const *b)
1893 {
1894   while (blanks[to_uchar (*a)])
1895     a++;
1896   while (blanks[to_uchar (*b)])
1897     b++;
1898
1899   int diff = find_unit_order (a) - find_unit_order (b);
1900   return (diff ? diff : strnumcmp (a, b, decimal_point, thousands_sep));
1901 }
1902
1903 /* Compare strings A and B as numbers without explicitly converting them to
1904    machine numbers.  Comparatively slow for short strings, but asymptotically
1905    hideously fast. */
1906
1907 static int
1908 numcompare (char const *a, char const *b)
1909 {
1910   while (blanks[to_uchar (*a)])
1911     a++;
1912   while (blanks[to_uchar (*b)])
1913     b++;
1914
1915   return strnumcmp (a, b, decimal_point, thousands_sep);
1916 }
1917
1918 /* Work around a problem whereby the long double value returned by glibc's
1919    strtold ("NaN", ...) contains uninitialized bits: clear all bytes of
1920    A and B before calling strtold.  FIXME: remove this function once
1921    gnulib guarantees that strtold's result is always well defined.  */
1922 static int
1923 nan_compare (char const *sa, char const *sb)
1924 {
1925   long_double a;
1926   memset (&a, 0, sizeof a);
1927   a = strtold (sa, NULL);
1928
1929   long_double b;
1930   memset (&b, 0, sizeof b);
1931   b = strtold (sb, NULL);
1932
1933   return memcmp (&a, &b, sizeof a);
1934 }
1935
1936 static int
1937 general_numcompare (char const *sa, char const *sb)
1938 {
1939   /* FIXME: maybe add option to try expensive FP conversion
1940      only if A and B can't be compared more cheaply/accurately.  */
1941
1942   char *ea;
1943   char *eb;
1944   long_double a = strtold (sa, &ea);
1945   long_double b = strtold (sb, &eb);
1946
1947   /* Put conversion errors at the start of the collating sequence.  */
1948   if (sa == ea)
1949     return sb == eb ? 0 : -1;
1950   if (sb == eb)
1951     return 1;
1952
1953   /* Sort numbers in the usual way, where -0 == +0.  Put NaNs after
1954      conversion errors but before numbers; sort them by internal
1955      bit-pattern, for lack of a more portable alternative.  */
1956   return (a < b ? -1
1957           : a > b ? 1
1958           : a == b ? 0
1959           : b == b ? -1
1960           : a == a ? 1
1961           : nan_compare (sa, sb));
1962 }
1963
1964 /* Return an integer in 1..12 of the month name MONTH.
1965    Return 0 if the name in S is not recognized.  */
1966
1967 static int
1968 getmonth (char const *month, char **ea)
1969 {
1970   size_t lo = 0;
1971   size_t hi = MONTHS_PER_YEAR;
1972
1973   while (blanks[to_uchar (*month)])
1974     month++;
1975
1976   do
1977     {
1978       size_t ix = (lo + hi) / 2;
1979       char const *m = month;
1980       char const *n = monthtab[ix].name;
1981
1982       for (;; m++, n++)
1983         {
1984           if (!*n)
1985             {
1986               if (ea)
1987                 *ea = (char *) m;
1988               return monthtab[ix].val;
1989             }
1990           if (to_uchar (fold_toupper[to_uchar (*m)]) < to_uchar (*n))
1991             {
1992               hi = ix;
1993               break;
1994             }
1995           else if (to_uchar (fold_toupper[to_uchar (*m)]) > to_uchar (*n))
1996             {
1997               lo = ix + 1;
1998               break;
1999             }
2000         }
2001     }
2002   while (lo < hi);
2003
2004   return 0;
2005 }
2006
2007 /* A randomly chosen MD5 state, used for random comparison.  */
2008 static struct md5_ctx random_md5_state;
2009
2010 /* Initialize the randomly chosen MD5 state.  */
2011
2012 static void
2013 random_md5_state_init (char const *random_source)
2014 {
2015   unsigned char buf[MD5_DIGEST_SIZE];
2016   struct randread_source *r = randread_new (random_source, sizeof buf);
2017   if (! r)
2018     die (_("open failed"), random_source);
2019   randread (r, buf, sizeof buf);
2020   if (randread_free (r) != 0)
2021     die (_("close failed"), random_source);
2022   md5_init_ctx (&random_md5_state);
2023   md5_process_bytes (buf, sizeof buf, &random_md5_state);
2024 }
2025
2026 /* This is like strxfrm, except it reports any error and exits.  */
2027
2028 static size_t
2029 xstrxfrm (char *restrict dest, char const *restrict src, size_t destsize)
2030 {
2031   errno = 0;
2032   size_t translated_size = strxfrm (dest, src, destsize);
2033
2034   if (errno)
2035     {
2036       error (0, errno, _("string transformation failed"));
2037       error (0, 0, _("set LC_ALL='C' to work around the problem"));
2038       error (SORT_FAILURE, 0,
2039              _("the untransformed string was %s"),
2040              quotearg_n_style (0, locale_quoting_style, src));
2041     }
2042
2043   return translated_size;
2044 }
2045
2046 /* Compare the keys TEXTA (of length LENA) and TEXTB (of length LENB)
2047    using one or more random hash functions.  TEXTA[LENA] and
2048    TEXTB[LENB] must be zero.  */
2049
2050 static int
2051 compare_random (char *restrict texta, size_t lena,
2052                 char *restrict textb, size_t lenb)
2053 {
2054   /* XFRM_DIFF records the equivalent of memcmp on the transformed
2055      data.  This is used to break ties if there is a checksum
2056      collision, and this is good enough given the astronomically low
2057      probability of a collision.  */
2058   int xfrm_diff = 0;
2059
2060   char stackbuf[4000];
2061   char *buf = stackbuf;
2062   size_t bufsize = sizeof stackbuf;
2063   void *allocated = NULL;
2064   uint32_t dig[2][MD5_DIGEST_SIZE / sizeof (uint32_t)];
2065   struct md5_ctx s[2];
2066   s[0] = s[1] = random_md5_state;
2067
2068   if (hard_LC_COLLATE)
2069     {
2070       char const *lima = texta + lena;
2071       char const *limb = textb + lenb;
2072
2073       while (true)
2074         {
2075           /* Transform the text into the basis of comparison, so that byte
2076              strings that would otherwise considered to be equal are
2077              considered equal here even if their bytes differ.
2078
2079              Each time through this loop, transform one
2080              null-terminated string's worth from TEXTA or from TEXTB
2081              or both.  That way, there's no need to store the
2082              transformation of the whole line, if it contains many
2083              null-terminated strings.  */
2084
2085           /* Store the transformed data into a big-enough buffer.  */
2086
2087           /* A 3X size guess avoids the overhead of calling strxfrm
2088              twice on typical implementations.  Don't worry about
2089              size_t overflow, as the guess need not be correct.  */
2090           size_t guess_bufsize = 3 * (lena + lenb) + 2;
2091           if (bufsize < guess_bufsize)
2092             {
2093               bufsize = MAX (guess_bufsize, bufsize * 3 / 2);
2094               free (allocated);
2095               buf = allocated = malloc (bufsize);
2096               if (! buf)
2097                 {
2098                   buf = stackbuf;
2099                   bufsize = sizeof stackbuf;
2100                 }
2101             }
2102
2103           size_t sizea =
2104             (texta < lima ? xstrxfrm (buf, texta, bufsize) + 1 : 0);
2105           bool a_fits = sizea <= bufsize;
2106           size_t sizeb =
2107             (textb < limb
2108              ? (xstrxfrm ((a_fits ? buf + sizea : NULL), textb,
2109                           (a_fits ? bufsize - sizea : 0))
2110                 + 1)
2111              : 0);
2112
2113           if (! (a_fits && sizea + sizeb <= bufsize))
2114             {
2115               bufsize = sizea + sizeb;
2116               if (bufsize < SIZE_MAX / 3)
2117                 bufsize = bufsize * 3 / 2;
2118               free (allocated);
2119               buf = allocated = xmalloc (bufsize);
2120               if (texta < lima)
2121                 strxfrm (buf, texta, sizea);
2122               if (textb < limb)
2123                 strxfrm (buf + sizea, textb, sizeb);
2124             }
2125
2126           /* Advance past NULs to the next part of each input string,
2127              exiting the loop if both strings are exhausted.  When
2128              exiting the loop, prepare to finish off the tiebreaker
2129              comparison properly.  */
2130           if (texta < lima)
2131             texta += strlen (texta) + 1;
2132           if (textb < limb)
2133             textb += strlen (textb) + 1;
2134           if (! (texta < lima || textb < limb))
2135             {
2136               lena = sizea; texta = buf;
2137               lenb = sizeb; textb = buf + sizea;
2138               break;
2139             }
2140
2141           /* Accumulate the transformed data in the corresponding
2142              checksums.  */
2143           md5_process_bytes (buf, sizea, &s[0]);
2144           md5_process_bytes (buf + sizea, sizeb, &s[1]);
2145
2146           /* Update the tiebreaker comparison of the transformed data.  */
2147           if (! xfrm_diff)
2148             {
2149               xfrm_diff = memcmp (buf, buf + sizea, MIN (sizea, sizeb));
2150               if (! xfrm_diff)
2151                 xfrm_diff = (sizea > sizeb) - (sizea < sizeb);
2152             }
2153         }
2154     }
2155
2156   /* Compute and compare the checksums.  */
2157   md5_process_bytes (texta, lena, &s[0]); md5_finish_ctx (&s[0], dig[0]);
2158   md5_process_bytes (textb, lenb, &s[1]); md5_finish_ctx (&s[1], dig[1]);
2159   int diff = memcmp (dig[0], dig[1], sizeof dig[0]);
2160
2161   /* Fall back on the tiebreaker if the checksums collide.  */
2162   if (! diff)
2163     {
2164       if (! xfrm_diff)
2165         {
2166           xfrm_diff = memcmp (texta, textb, MIN (lena, lenb));
2167           if (! xfrm_diff)
2168             xfrm_diff = (lena > lenb) - (lena < lenb);
2169         }
2170
2171       diff = xfrm_diff;
2172     }
2173
2174   free (allocated);
2175
2176   return diff;
2177 }
2178
2179 /* Return the printable width of the block of memory starting at
2180    TEXT and ending just before LIM, counting each tab as one byte.
2181    FIXME: Should we generally be counting non printable chars?  */
2182
2183 static size_t
2184 debug_width (char const *text, char const *lim)
2185 {
2186   size_t width = mbsnwidth (text, lim - text, 0);
2187   while (text < lim)
2188     width += (*text++ == '\t');
2189   return width;
2190 }
2191
2192 /* For debug mode, "underline" a key at the
2193    specified offset and screen width.  */
2194
2195 static void
2196 mark_key (size_t offset, size_t width)
2197 {
2198   while (offset--)
2199     putchar (' ');
2200
2201   if (!width)
2202     printf (_("^ no match for key\n"));
2203   else
2204     {
2205       do
2206         putchar ('_');
2207       while (--width);
2208
2209       putchar ('\n');
2210     }
2211 }
2212
2213 /* Return true if KEY is a numeric key.  */
2214
2215 static inline bool
2216 key_numeric (struct keyfield const *key)
2217 {
2218   return key->numeric || key->general_numeric || key->human_numeric;
2219 }
2220
2221 /* For LINE, output a debugging line that underlines KEY in LINE.
2222    If KEY is null, underline the whole line.  */
2223
2224 static void
2225 debug_key (struct line const *line, struct keyfield const *key)
2226 {
2227   char *text = line->text;
2228   char *beg = text;
2229   char *lim = text + line->length - 1;
2230
2231   if (key)
2232     {
2233       if (key->sword != SIZE_MAX)
2234         beg = begfield (line, key);
2235       if (key->eword != SIZE_MAX)
2236         lim = limfield (line, key);
2237
2238       if (key->skipsblanks || key->month || key_numeric (key))
2239         {
2240           char saved = *lim;
2241           *lim = '\0';
2242
2243           while (blanks[to_uchar (*beg)])
2244             beg++;
2245
2246           char *tighter_lim = beg;
2247
2248           if (lim < beg)
2249             tighter_lim = lim;
2250           else if (key->month)
2251             getmonth (beg, &tighter_lim);
2252           else if (key->general_numeric)
2253             ignore_value (strtold (beg, &tighter_lim));
2254           else if (key->numeric || key->human_numeric)
2255             {
2256               char *p = beg + (beg < lim && *beg == '-');
2257               bool found_digit = false;
2258               unsigned char ch;
2259
2260               do
2261                 {
2262                   while (ISDIGIT (ch = *p++))
2263                     found_digit = true;
2264                 }
2265               while (ch == thousands_sep);
2266
2267               if (ch == decimal_point)
2268                 while (ISDIGIT (ch = *p++))
2269                   found_digit = true;
2270
2271               if (found_digit)
2272                 tighter_lim = p - ! (key->human_numeric && unit_order[ch]);
2273             }
2274           else
2275             tighter_lim = lim;
2276
2277           *lim = saved;
2278           lim = tighter_lim;
2279         }
2280     }
2281
2282   size_t offset = debug_width (text, beg);
2283   size_t width = debug_width (beg, lim);
2284   mark_key (offset, width);
2285 }
2286
2287 /* Debug LINE by underlining its keys.  */
2288
2289 static void
2290 debug_line (struct line const *line)
2291 {
2292   struct keyfield const *key = keylist;
2293
2294   do
2295     debug_key (line, key);
2296   while (key && ((key = key->next) || ! (unique || stable)));
2297 }
2298
2299 /* Return whether sorting options specified for key.  */
2300
2301 static bool
2302 default_key_compare (struct keyfield const *key)
2303 {
2304   return ! (key->ignore
2305             || key->translate
2306             || key->skipsblanks
2307             || key->skipeblanks
2308             || key_numeric (key)
2309             || key->month
2310             || key->version
2311             || key->random
2312             /* || key->reverse */
2313            );
2314 }
2315
2316 /* Convert a key to the short options used to specify it.  */
2317
2318 static void
2319 key_to_opts (struct keyfield const *key, char *opts)
2320 {
2321   if (key->skipsblanks || key->skipeblanks)
2322     *opts++ = 'b';/* either disables global -b  */
2323   if (key->ignore == nondictionary)
2324     *opts++ = 'd';
2325   if (key->translate)
2326     *opts++ = 'f';
2327   if (key->general_numeric)
2328     *opts++ = 'g';
2329   if (key->human_numeric)
2330     *opts++ = 'h';
2331   if (key->ignore == nonprinting)
2332     *opts++ = 'i';
2333   if (key->month)
2334     *opts++ = 'M';
2335   if (key->numeric)
2336     *opts++ = 'n';
2337   if (key->random)
2338     *opts++ = 'R';
2339   if (key->reverse)
2340     *opts++ = 'r';
2341   if (key->version)
2342     *opts++ = 'V';
2343   *opts = '\0';
2344 }
2345
2346 /* Output data independent key warnings to stderr.  */
2347
2348 static void
2349 key_warnings (struct keyfield const *gkey, bool gkey_only)
2350 {
2351   struct keyfield const *key;
2352   struct keyfield ugkey = *gkey;
2353   unsigned long keynum = 1;
2354
2355   for (key = keylist; key; key = key->next, keynum++)
2356     {
2357       if (key->obsolete_used)
2358         {
2359           size_t sword = key->sword;
2360           size_t eword = key->eword;
2361           char tmp[INT_BUFSIZE_BOUND (uintmax_t)];
2362           /* obsolescent syntax +A.x -B.y is equivalent to:
2363                -k A+1.x+1,B.y   (when y = 0)
2364                -k A+1.x+1,B+1.y (when y > 0)  */
2365           char obuf[INT_BUFSIZE_BOUND (sword) * 2 + 4]; /* +# -#  */
2366           char nbuf[INT_BUFSIZE_BOUND (sword) * 2 + 5]; /* -k #,#  */
2367           char *po = obuf;
2368           char *pn = nbuf;
2369
2370           if (sword == SIZE_MAX)
2371             sword++;
2372
2373           po = stpcpy (stpcpy (po, "+"), umaxtostr (sword, tmp));
2374           pn = stpcpy (stpcpy (pn, "-k "), umaxtostr (sword + 1, tmp));
2375           if (key->eword != SIZE_MAX)
2376             {
2377               stpcpy (stpcpy (po, " -"), umaxtostr (eword + 1, tmp));
2378               stpcpy (stpcpy (pn, ","),
2379                       umaxtostr (eword + 1
2380                                  + (key->echar == SIZE_MAX), tmp));
2381             }
2382           error (0, 0, _("obsolescent key %s used; consider %s instead"),
2383                  quote_n (0, obuf), quote_n (1, nbuf));
2384         }
2385
2386       /* Warn about field specs that will never match.  */
2387       if (key->sword != SIZE_MAX && key->eword < key->sword)
2388         error (0, 0, _("key %lu has zero width and will be ignored"), keynum);
2389
2390       /* Warn about significant leading blanks.  */
2391       bool implicit_skip = key_numeric (key) || key->month;
2392       bool maybe_space_aligned = !hard_LC_COLLATE && default_key_compare (key)
2393                                  && !(key->schar || key->echar);
2394       bool line_offset = key->eword == 0 && key->echar != 0; /* -k1.x,1.y  */
2395       if (!gkey_only && tab == TAB_DEFAULT && !line_offset
2396           && ((!key->skipsblanks && !(implicit_skip || maybe_space_aligned))
2397               || (!key->skipsblanks && key->schar)
2398               || (!key->skipeblanks && key->echar)))
2399         error (0, 0, _("leading blanks are significant in key %lu; "
2400                        "consider also specifying 'b'"), keynum);
2401
2402       /* Warn about numeric comparisons spanning fields,
2403          as field delimiters could be interpreted as part
2404          of the number (maybe only in other locales).  */
2405       if (!gkey_only && key_numeric (key))
2406         {
2407           size_t sword = key->sword + 1;
2408           size_t eword = key->eword + 1;
2409           if (!sword)
2410             sword++;
2411           if (!eword || sword < eword)
2412             error (0, 0, _("key %lu is numeric and spans multiple fields"),
2413                    keynum);
2414         }
2415
2416       /* Flag global options not copied or specified in any key.  */
2417       if (ugkey.ignore && (ugkey.ignore == key->ignore))
2418         ugkey.ignore = NULL;
2419       if (ugkey.translate && (ugkey.translate == key->translate))
2420         ugkey.translate = NULL;
2421       ugkey.skipsblanks &= !key->skipsblanks;
2422       ugkey.skipeblanks &= !key->skipeblanks;
2423       ugkey.month &= !key->month;
2424       ugkey.numeric &= !key->numeric;
2425       ugkey.general_numeric &= !key->general_numeric;
2426       ugkey.human_numeric &= !key->human_numeric;
2427       ugkey.random &= !key->random;
2428       ugkey.version &= !key->version;
2429       ugkey.reverse &= !key->reverse;
2430     }
2431
2432   /* Warn about ignored global options flagged above.
2433      Note if gkey is the only one in the list, all flags are cleared.  */
2434   if (!default_key_compare (&ugkey)
2435       || (ugkey.reverse && (stable || unique) && keylist))
2436     {
2437       bool ugkey_reverse = ugkey.reverse;
2438       if (!(stable || unique))
2439         ugkey.reverse = false;
2440       /* The following is too big, but guaranteed to be "big enough".  */
2441       char opts[sizeof short_options];
2442       key_to_opts (&ugkey, opts);
2443       error (0, 0,
2444              ngettext ("option '-%s' is ignored",
2445                        "options '-%s' are ignored",
2446                        select_plural (strlen (opts))), opts);
2447       ugkey.reverse = ugkey_reverse;
2448     }
2449   if (ugkey.reverse && !(stable || unique) && keylist)
2450     error (0, 0, _("option '-r' only applies to last-resort comparison"));
2451 }
2452
2453 /* Compare two lines A and B trying every key in sequence until there
2454    are no more keys or a difference is found. */
2455
2456 static int
2457 keycompare (struct line const *a, struct line const *b)
2458 {
2459   struct keyfield *key = keylist;
2460
2461   /* For the first iteration only, the key positions have been
2462      precomputed for us. */
2463   char *texta = a->keybeg;
2464   char *textb = b->keybeg;
2465   char *lima = a->keylim;
2466   char *limb = b->keylim;
2467
2468   int diff;
2469
2470   while (true)
2471     {
2472       char const *translate = key->translate;
2473       bool const *ignore = key->ignore;
2474
2475       /* Treat field ends before field starts as empty fields.  */
2476       lima = MAX (texta, lima);
2477       limb = MAX (textb, limb);
2478
2479       /* Find the lengths. */
2480       size_t lena = lima - texta;
2481       size_t lenb = limb - textb;
2482
2483       if (hard_LC_COLLATE || key_numeric (key)
2484           || key->month || key->random || key->version)
2485         {
2486           char *ta;
2487           char *tb;
2488           size_t tlena;
2489           size_t tlenb;
2490
2491           char enda IF_LINT (= 0);
2492           char endb IF_LINT (= 0);
2493           void *allocated IF_LINT (= NULL);
2494           char stackbuf[4000];
2495
2496           if (ignore || translate)
2497             {
2498               /* Compute with copies of the keys, which are the result of
2499                  translating or ignoring characters, and which need their
2500                  own storage.  */
2501
2502               size_t i;
2503
2504               /* Allocate space for copies.  */
2505               size_t size = lena + 1 + lenb + 1;
2506               if (size <= sizeof stackbuf)
2507                 ta = stackbuf, allocated = NULL;
2508               else
2509                 ta = allocated = xmalloc (size);
2510               tb = ta + lena + 1;
2511
2512               /* Put into each copy a version of the key in which the
2513                  requested characters are ignored or translated.  */
2514               for (tlena = i = 0; i < lena; i++)
2515                 if (! (ignore && ignore[to_uchar (texta[i])]))
2516                   ta[tlena++] = (translate
2517                                  ? translate[to_uchar (texta[i])]
2518                                  : texta[i]);
2519               ta[tlena] = '\0';
2520
2521               for (tlenb = i = 0; i < lenb; i++)
2522                 if (! (ignore && ignore[to_uchar (textb[i])]))
2523                   tb[tlenb++] = (translate
2524                                  ? translate[to_uchar (textb[i])]
2525                                  : textb[i]);
2526               tb[tlenb] = '\0';
2527             }
2528           else
2529             {
2530               /* Use the keys in-place, temporarily null-terminated.  */
2531               ta = texta; tlena = lena; enda = ta[tlena]; ta[tlena] = '\0';
2532               tb = textb; tlenb = lenb; endb = tb[tlenb]; tb[tlenb] = '\0';
2533             }
2534
2535           if (key->numeric)
2536             diff = numcompare (ta, tb);
2537           else if (key->general_numeric)
2538             diff = general_numcompare (ta, tb);
2539           else if (key->human_numeric)
2540             diff = human_numcompare (ta, tb);
2541           else if (key->month)
2542             diff = getmonth (ta, NULL) - getmonth (tb, NULL);
2543           else if (key->random)
2544             diff = compare_random (ta, tlena, tb, tlenb);
2545           else if (key->version)
2546             diff = filevercmp (ta, tb);
2547           else
2548             {
2549               /* Locale-dependent string sorting.  This is slower than
2550                  C-locale sorting, which is implemented below.  */
2551               if (tlena == 0)
2552                 diff = - NONZERO (tlenb);
2553               else if (tlenb == 0)
2554                 diff = 1;
2555               else
2556                 diff = xmemcoll0 (ta, tlena + 1, tb, tlenb + 1);
2557             }
2558
2559           if (ignore || translate)
2560             free (allocated);
2561           else
2562             {
2563               ta[tlena] = enda;
2564               tb[tlenb] = endb;
2565             }
2566         }
2567       else if (ignore)
2568         {
2569 #define CMP_WITH_IGNORE(A, B)                                           \
2570   do                                                                    \
2571     {                                                                   \
2572           while (true)                                                  \
2573             {                                                           \
2574               while (texta < lima && ignore[to_uchar (*texta)])         \
2575                 ++texta;                                                \
2576               while (textb < limb && ignore[to_uchar (*textb)])         \
2577                 ++textb;                                                \
2578               if (! (texta < lima && textb < limb))                     \
2579                 break;                                                  \
2580               diff = to_uchar (A) - to_uchar (B);                       \
2581               if (diff)                                                 \
2582                 goto not_equal;                                         \
2583               ++texta;                                                  \
2584               ++textb;                                                  \
2585             }                                                           \
2586                                                                         \
2587           diff = (texta < lima) - (textb < limb);                       \
2588     }                                                                   \
2589   while (0)
2590
2591           if (translate)
2592             CMP_WITH_IGNORE (translate[to_uchar (*texta)],
2593                              translate[to_uchar (*textb)]);
2594           else
2595             CMP_WITH_IGNORE (*texta, *textb);
2596         }
2597       else if (lena == 0)
2598         diff = - NONZERO (lenb);
2599       else if (lenb == 0)
2600         goto greater;
2601       else
2602         {
2603           if (translate)
2604             {
2605               while (texta < lima && textb < limb)
2606                 {
2607                   diff = (to_uchar (translate[to_uchar (*texta++)])
2608                           - to_uchar (translate[to_uchar (*textb++)]));
2609                   if (diff)
2610                     goto not_equal;
2611                 }
2612             }
2613           else
2614             {
2615               diff = memcmp (texta, textb, MIN (lena, lenb));
2616               if (diff)
2617                 goto not_equal;
2618             }
2619           diff = lena < lenb ? -1 : lena != lenb;
2620         }
2621
2622       if (diff)
2623         goto not_equal;
2624
2625       key = key->next;
2626       if (! key)
2627         break;
2628
2629       /* Find the beginning and limit of the next field.  */
2630       if (key->eword != SIZE_MAX)
2631         lima = limfield (a, key), limb = limfield (b, key);
2632       else
2633         lima = a->text + a->length - 1, limb = b->text + b->length - 1;
2634
2635       if (key->sword != SIZE_MAX)
2636         texta = begfield (a, key), textb = begfield (b, key);
2637       else
2638         {
2639           texta = a->text, textb = b->text;
2640           if (key->skipsblanks)
2641             {
2642               while (texta < lima && blanks[to_uchar (*texta)])
2643                 ++texta;
2644               while (textb < limb && blanks[to_uchar (*textb)])
2645                 ++textb;
2646             }
2647         }
2648     }
2649
2650   return 0;
2651
2652  greater:
2653   diff = 1;
2654  not_equal:
2655   return key->reverse ? -diff : diff;
2656 }
2657
2658 /* Compare two lines A and B, returning negative, zero, or positive
2659    depending on whether A compares less than, equal to, or greater than B. */
2660
2661 static int
2662 compare (struct line const *a, struct line const *b)
2663 {
2664   int diff;
2665   size_t alen, blen;
2666
2667   /* First try to compare on the specified keys (if any).
2668      The only two cases with no key at all are unadorned sort,
2669      and unadorned sort -r. */
2670   if (keylist)
2671     {
2672       diff = keycompare (a, b);
2673       if (diff || unique || stable)
2674         return diff;
2675     }
2676
2677   /* If the keys all compare equal (or no keys were specified)
2678      fall through to the default comparison.  */
2679   alen = a->length - 1, blen = b->length - 1;
2680
2681   if (alen == 0)
2682     diff = - NONZERO (blen);
2683   else if (blen == 0)
2684     diff = 1;
2685   else if (hard_LC_COLLATE)
2686     {
2687       /* Note xmemcoll0 is a performance enhancement as
2688          it will not unconditionally write '\0' after the
2689          passed in buffers, which was seen to give around
2690          a 3% increase in performance for short lines.  */
2691       diff = xmemcoll0 (a->text, alen + 1, b->text, blen + 1);
2692     }
2693   else if (! (diff = memcmp (a->text, b->text, MIN (alen, blen))))
2694     diff = alen < blen ? -1 : alen != blen;
2695
2696   return reverse ? -diff : diff;
2697 }
2698
2699 /* Write LINE to output stream FP; the output file's name is
2700    OUTPUT_FILE if OUTPUT_FILE is nonnull, and is the standard output
2701    otherwise.  If debugging is enabled and FP is standard output,
2702    append some debugging information.  */
2703
2704 static void
2705 write_line (struct line const *line, FILE *fp, char const *output_file)
2706 {
2707   char *buf = line->text;
2708   size_t n_bytes = line->length;
2709   char *ebuf = buf + n_bytes;
2710
2711   if (!output_file && debug)
2712     {
2713       /* Convert TAB to '>' and EOL to \n, and then output debugging info.  */
2714       char const *c = buf;
2715
2716       while (c < ebuf)
2717         {
2718           char wc = *c++;
2719           if (wc == '\t')
2720             wc = '>';
2721           else if (c == ebuf)
2722             wc = '\n';
2723           if (fputc (wc, fp) == EOF)
2724             die (_("write failed"), output_file);
2725         }
2726
2727       debug_line (line);
2728     }
2729   else
2730     {
2731       ebuf[-1] = eolchar;
2732       if (fwrite (buf, 1, n_bytes, fp) != n_bytes)
2733         die (_("write failed"), output_file);
2734       ebuf[-1] = '\0';
2735     }
2736 }
2737
2738 /* Check that the lines read from FILE_NAME come in order.  Return
2739    true if they are in order.  If CHECKONLY == 'c', also print a
2740    diagnostic (FILE_NAME, line number, contents of line) to stderr if
2741    they are not in order.  */
2742
2743 static bool
2744 check (char const *file_name, char checkonly)
2745 {
2746   FILE *fp = xfopen (file_name, "r");
2747   struct buffer buf;            /* Input buffer. */
2748   struct line temp;             /* Copy of previous line. */
2749   size_t alloc = 0;
2750   uintmax_t line_number = 0;
2751   struct keyfield const *key = keylist;
2752   bool nonunique = ! unique;
2753   bool ordered = true;
2754
2755   initbuf (&buf, sizeof (struct line),
2756            MAX (merge_buffer_size, sort_size));
2757   temp.text = NULL;
2758
2759   while (fillbuf (&buf, fp, file_name))
2760     {
2761       struct line const *line = buffer_linelim (&buf);
2762       struct line const *linebase = line - buf.nlines;
2763
2764       /* Make sure the line saved from the old buffer contents is
2765          less than or equal to the first line of the new buffer. */
2766       if (alloc && nonunique <= compare (&temp, line - 1))
2767         {
2768         found_disorder:
2769           {
2770             if (checkonly == 'c')
2771               {
2772                 struct line const *disorder_line = line - 1;
2773                 uintmax_t disorder_line_number =
2774                   buffer_linelim (&buf) - disorder_line + line_number;
2775                 char hr_buf[INT_BUFSIZE_BOUND (disorder_line_number)];
2776                 fprintf (stderr, _("%s: %s:%s: disorder: "),
2777                          program_name, file_name,
2778                          umaxtostr (disorder_line_number, hr_buf));
2779                 write_line (disorder_line, stderr, _("standard error"));
2780               }
2781
2782             ordered = false;
2783             break;
2784           }
2785         }
2786
2787       /* Compare each line in the buffer with its successor.  */
2788       while (linebase < --line)
2789         if (nonunique <= compare (line, line - 1))
2790           goto found_disorder;
2791
2792       line_number += buf.nlines;
2793
2794       /* Save the last line of the buffer.  */
2795       if (alloc < line->length)
2796         {
2797           do
2798             {
2799               alloc *= 2;
2800               if (! alloc)
2801                 {
2802                   alloc = line->length;
2803                   break;
2804                 }
2805             }
2806           while (alloc < line->length);
2807
2808           free (temp.text);
2809           temp.text = xmalloc (alloc);
2810         }
2811       memcpy (temp.text, line->text, line->length);
2812       temp.length = line->length;
2813       if (key)
2814         {
2815           temp.keybeg = temp.text + (line->keybeg - line->text);
2816           temp.keylim = temp.text + (line->keylim - line->text);
2817         }
2818     }
2819
2820   xfclose (fp, file_name);
2821   free (buf.buf);
2822   free (temp.text);
2823   return ordered;
2824 }
2825
2826 /* Open FILES (there are NFILES of them) and store the resulting array
2827    of stream pointers into (*PFPS).  Allocate the array.  Return the
2828    number of successfully opened files, setting errno if this value is
2829    less than NFILES.  */
2830
2831 static size_t
2832 open_input_files (struct sortfile *files, size_t nfiles, FILE ***pfps)
2833 {
2834   FILE **fps = *pfps = xnmalloc (nfiles, sizeof *fps);
2835   int i;
2836
2837   /* Open as many input files as we can.  */
2838   for (i = 0; i < nfiles; i++)
2839     {
2840       fps[i] = (files[i].temp && files[i].temp->state != UNCOMPRESSED
2841                 ? open_temp (files[i].temp)
2842                 : stream_open (files[i].name, "r"));
2843       if (!fps[i])
2844         break;
2845     }
2846
2847   return i;
2848 }
2849
2850 /* Merge lines from FILES onto OFP.  NTEMPS is the number of temporary
2851    files (all of which are at the start of the FILES array), and
2852    NFILES is the number of files; 0 <= NTEMPS <= NFILES <= NMERGE.
2853    FPS is the vector of open stream corresponding to the files.
2854    Close input and output streams before returning.
2855    OUTPUT_FILE gives the name of the output file.  If it is NULL,
2856    the output file is standard output.  */
2857
2858 static void
2859 mergefps (struct sortfile *files, size_t ntemps, size_t nfiles,
2860           FILE *ofp, char const *output_file, FILE **fps)
2861 {
2862   struct buffer *buffer = xnmalloc (nfiles, sizeof *buffer);
2863                                 /* Input buffers for each file. */
2864   struct line saved;            /* Saved line storage for unique check. */
2865   struct line const *savedline = NULL;
2866                                 /* &saved if there is a saved line. */
2867   size_t savealloc = 0;         /* Size allocated for the saved line. */
2868   struct line const **cur = xnmalloc (nfiles, sizeof *cur);
2869                                 /* Current line in each line table. */
2870   struct line const **base = xnmalloc (nfiles, sizeof *base);
2871                                 /* Base of each line table.  */
2872   size_t *ord = xnmalloc (nfiles, sizeof *ord);
2873                                 /* Table representing a permutation of fps,
2874                                    such that cur[ord[0]] is the smallest line
2875                                    and will be next output. */
2876   size_t i;
2877   size_t j;
2878   size_t t;
2879   struct keyfield const *key = keylist;
2880   saved.text = NULL;
2881
2882   /* Read initial lines from each input file. */
2883   for (i = 0; i < nfiles; )
2884     {
2885       initbuf (&buffer[i], sizeof (struct line),
2886                MAX (merge_buffer_size, sort_size / nfiles));
2887       if (fillbuf (&buffer[i], fps[i], files[i].name))
2888         {
2889           struct line const *linelim = buffer_linelim (&buffer[i]);
2890           cur[i] = linelim - 1;
2891           base[i] = linelim - buffer[i].nlines;
2892           i++;
2893         }
2894       else
2895         {
2896           /* fps[i] is empty; eliminate it from future consideration.  */
2897           xfclose (fps[i], files[i].name);
2898           if (i < ntemps)
2899             {
2900               ntemps--;
2901               zaptemp (files[i].name);
2902             }
2903           free (buffer[i].buf);
2904           --nfiles;
2905           for (j = i; j < nfiles; ++j)
2906             {
2907               files[j] = files[j + 1];
2908               fps[j] = fps[j + 1];
2909             }
2910         }
2911     }
2912
2913   /* Set up the ord table according to comparisons among input lines.
2914      Since this only reorders two items if one is strictly greater than
2915      the other, it is stable. */
2916   for (i = 0; i < nfiles; ++i)
2917     ord[i] = i;
2918   for (i = 1; i < nfiles; ++i)
2919     if (0 < compare (cur[ord[i - 1]], cur[ord[i]]))
2920       t = ord[i - 1], ord[i - 1] = ord[i], ord[i] = t, i = 0;
2921
2922   /* Repeatedly output the smallest line until no input remains. */
2923   while (nfiles)
2924     {
2925       struct line const *smallest = cur[ord[0]];
2926
2927       /* If uniquified output is turned on, output only the first of
2928          an identical series of lines. */
2929       if (unique)
2930         {
2931           if (savedline && compare (savedline, smallest))
2932             {
2933               savedline = NULL;
2934               write_line (&saved, ofp, output_file);
2935             }
2936           if (!savedline)
2937             {
2938               savedline = &saved;
2939               if (savealloc < smallest->length)
2940                 {
2941                   do
2942                     if (! savealloc)
2943                       {
2944                         savealloc = smallest->length;
2945                         break;
2946                       }
2947                   while ((savealloc *= 2) < smallest->length);
2948
2949                   free (saved.text);
2950                   saved.text = xmalloc (savealloc);
2951                 }
2952               saved.length = smallest->length;
2953               memcpy (saved.text, smallest->text, saved.length);
2954               if (key)
2955                 {
2956                   saved.keybeg =
2957                     saved.text + (smallest->keybeg - smallest->text);
2958                   saved.keylim =
2959                     saved.text + (smallest->keylim - smallest->text);
2960                 }
2961             }
2962         }
2963       else
2964         write_line (smallest, ofp, output_file);
2965
2966       /* Check if we need to read more lines into core. */
2967       if (base[ord[0]] < smallest)
2968         cur[ord[0]] = smallest - 1;
2969       else
2970         {
2971           if (fillbuf (&buffer[ord[0]], fps[ord[0]], files[ord[0]].name))
2972             {
2973               struct line const *linelim = buffer_linelim (&buffer[ord[0]]);
2974               cur[ord[0]] = linelim - 1;
2975               base[ord[0]] = linelim - buffer[ord[0]].nlines;
2976             }
2977           else
2978             {
2979               /* We reached EOF on fps[ord[0]].  */
2980               for (i = 1; i < nfiles; ++i)
2981                 if (ord[i] > ord[0])
2982                   --ord[i];
2983               --nfiles;
2984               xfclose (fps[ord[0]], files[ord[0]].name);
2985               if (ord[0] < ntemps)
2986                 {
2987                   ntemps--;
2988                   zaptemp (files[ord[0]].name);
2989                 }
2990               free (buffer[ord[0]].buf);
2991               for (i = ord[0]; i < nfiles; ++i)
2992                 {
2993                   fps[i] = fps[i + 1];
2994                   files[i] = files[i + 1];
2995                   buffer[i] = buffer[i + 1];
2996                   cur[i] = cur[i + 1];
2997                   base[i] = base[i + 1];
2998                 }
2999               for (i = 0; i < nfiles; ++i)
3000                 ord[i] = ord[i + 1];
3001               continue;
3002             }
3003         }
3004
3005       /* The new line just read in may be larger than other lines
3006          already in main memory; push it back in the queue until we
3007          encounter a line larger than it.  Optimize for the common
3008          case where the new line is smallest.  */
3009       {
3010         size_t lo = 1;
3011         size_t hi = nfiles;
3012         size_t probe = lo;
3013         size_t ord0 = ord[0];
3014         size_t count_of_smaller_lines;
3015
3016         while (lo < hi)
3017           {
3018             int cmp = compare (cur[ord0], cur[ord[probe]]);
3019             if (cmp < 0 || (cmp == 0 && ord0 < ord[probe]))
3020               hi = probe;
3021             else
3022               lo = probe + 1;
3023             probe = (lo + hi) / 2;
3024           }
3025
3026         count_of_smaller_lines = lo - 1;
3027         for (j = 0; j < count_of_smaller_lines; j++)
3028           ord[j] = ord[j + 1];
3029         ord[count_of_smaller_lines] = ord0;
3030       }
3031     }
3032
3033   if (unique && savedline)
3034     {
3035       write_line (&saved, ofp, output_file);
3036       free (saved.text);
3037     }
3038
3039   xfclose (ofp, output_file);
3040   free (fps);
3041   free (buffer);
3042   free (ord);
3043   free (base);
3044   free (cur);
3045 }
3046
3047 /* Merge lines from FILES onto OFP.  NTEMPS is the number of temporary
3048    files (all of which are at the start of the FILES array), and
3049    NFILES is the number of files; 0 <= NTEMPS <= NFILES <= NMERGE.
3050    Close input and output files before returning.
3051    OUTPUT_FILE gives the name of the output file.
3052
3053    Return the number of files successfully merged.  This number can be
3054    less than NFILES if we ran low on file descriptors, but in this
3055    case it is never less than 2.  */
3056
3057 static size_t
3058 mergefiles (struct sortfile *files, size_t ntemps, size_t nfiles,
3059             FILE *ofp, char const *output_file)
3060 {
3061   FILE **fps;
3062   size_t nopened = open_input_files (files, nfiles, &fps);
3063   if (nopened < nfiles && nopened < 2)
3064     die (_("open failed"), files[nopened].name);
3065   mergefps (files, ntemps, nopened, ofp, output_file, fps);
3066   return nopened;
3067 }
3068
3069 /* Merge into T (of size NLINES) the two sorted arrays of lines
3070    LO (with NLINES / 2 members), and
3071    T - (NLINES / 2) (with NLINES - NLINES / 2 members).
3072    T and LO point just past their respective arrays, and the arrays
3073    are in reverse order.  NLINES must be at least 2.  */
3074
3075 static void
3076 mergelines (struct line *restrict t, size_t nlines,
3077             struct line const *restrict lo)
3078 {
3079   size_t nlo = nlines / 2;
3080   size_t nhi = nlines - nlo;
3081   struct line *hi = t - nlo;
3082
3083   while (true)
3084     if (compare (lo - 1, hi - 1) <= 0)
3085       {
3086         *--t = *--lo;
3087         if (! --nlo)
3088           {
3089             /* HI must equal T now, and there is no need to copy from
3090                HI to T. */
3091             return;
3092           }
3093       }
3094     else
3095       {
3096         *--t = *--hi;
3097         if (! --nhi)
3098           {
3099             do
3100               *--t = *--lo;
3101             while (--nlo);
3102
3103             return;
3104           }
3105       }
3106 }
3107
3108 /* Sort the array LINES with NLINES members, using TEMP for temporary space.
3109    Do this all within one thread.  NLINES must be at least 2.
3110    If TO_TEMP, put the sorted output into TEMP, and TEMP is as large as LINES.
3111    Otherwise the sort is in-place and TEMP is half-sized.
3112    The input and output arrays are in reverse order, and LINES and
3113    TEMP point just past the end of their respective arrays.
3114
3115    Use a recursive divide-and-conquer algorithm, in the style
3116    suggested by Knuth volume 3 (2nd edition), exercise 5.2.4-23.  Use
3117    the optimization suggested by exercise 5.2.4-10; this requires room
3118    for only 1.5*N lines, rather than the usual 2*N lines.  Knuth
3119    writes that this memory optimization was originally published by
3120    D. A. Bell, Comp J. 1 (1958), 75.  */
3121
3122 static void
3123 sequential_sort (struct line *restrict lines, size_t nlines,
3124                  struct line *restrict temp, bool to_temp)
3125 {
3126   if (nlines == 2)
3127     {
3128       /* Declare 'swap' as int, not bool, to work around a bug
3129          <http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html>
3130          in the IBM xlc 6.0.0.0 compiler in 64-bit mode.  */
3131       int swap = (0 < compare (&lines[-1], &lines[-2]));
3132       if (to_temp)
3133         {
3134           temp[-1] = lines[-1 - swap];
3135           temp[-2] = lines[-2 + swap];
3136         }
3137       else if (swap)
3138         {
3139           temp[-1] = lines[-1];
3140           lines[-1] = lines[-2];
3141           lines[-2] = temp[-1];
3142         }
3143     }
3144   else
3145     {
3146       size_t nlo = nlines / 2;
3147       size_t nhi = nlines - nlo;
3148       struct line *lo = lines;
3149       struct line *hi = lines - nlo;
3150
3151       sequential_sort (hi, nhi, temp - (to_temp ? nlo : 0), to_temp);
3152       if (1 < nlo)
3153         sequential_sort (lo, nlo, temp, !to_temp);
3154       else if (!to_temp)
3155         temp[-1] = lo[-1];
3156
3157       struct line *dest;
3158       struct line const *sorted_lo;
3159       if (to_temp)
3160         {
3161           dest = temp;
3162           sorted_lo = lines;
3163         }
3164       else
3165         {
3166           dest = lines;
3167           sorted_lo = temp;
3168         }
3169       mergelines (dest, nlines, sorted_lo);
3170     }
3171 }
3172
3173 static struct merge_node *init_node (struct merge_node *restrict,
3174                                      struct merge_node *restrict,
3175                                      struct line *, size_t, size_t, bool);
3176
3177
3178 /* Create and return a merge tree for NTHREADS threads, sorting NLINES
3179    lines, with destination DEST.  */
3180 static struct merge_node *
3181 merge_tree_init (size_t nthreads, size_t nlines, struct line *dest)
3182 {
3183   struct merge_node *merge_tree = xmalloc (2 * sizeof *merge_tree * nthreads);
3184
3185   struct merge_node *root = merge_tree;
3186   root->lo = root->hi = root->end_lo = root->end_hi = NULL;
3187   root->dest = NULL;
3188   root->nlo = root->nhi = nlines;
3189   root->parent = NULL;
3190   root->level = MERGE_END;
3191   root->queued = false;
3192   pthread_mutex_init (&root->lock, NULL);
3193
3194   init_node (root, root + 1, dest, nthreads, nlines, false);
3195   return merge_tree;
3196 }
3197
3198 /* Destroy the merge tree. */
3199 static void
3200 merge_tree_destroy (struct merge_node *merge_tree)
3201 {
3202   free (merge_tree);
3203 }
3204
3205 /* Initialize a merge tree node and its descendants.  The node's
3206    parent is PARENT.  The node and its descendants are taken from the
3207    array of nodes NODE_POOL.  Their destination starts at DEST; they
3208    will consume NTHREADS threads.  The total number of sort lines is
3209    TOTAL_LINES.  IS_LO_CHILD is true if the node is the low child of
3210    its parent.  */
3211
3212 static struct merge_node *
3213 init_node (struct merge_node *restrict parent,
3214            struct merge_node *restrict node_pool,
3215            struct line *dest, size_t nthreads,
3216            size_t total_lines, bool is_lo_child)
3217 {
3218   size_t nlines = (is_lo_child ? parent->nlo : parent->nhi);
3219   size_t nlo = nlines / 2;
3220   size_t nhi = nlines - nlo;
3221   struct line *lo = dest - total_lines;
3222   struct line *hi = lo - nlo;
3223   struct line **parent_end = (is_lo_child ? &parent->end_lo : &parent->end_hi);
3224
3225   struct merge_node *node = node_pool++;
3226   node->lo = node->end_lo = lo;
3227   node->hi = node->end_hi = hi;
3228   node->dest = parent_end;
3229   node->nlo = nlo;
3230   node->nhi = nhi;
3231   node->parent = parent;
3232   node->level = parent->level + 1;
3233   node->queued = false;
3234   pthread_mutex_init (&node->lock, NULL);
3235
3236   if (nthreads > 1)
3237     {
3238       size_t lo_threads = nthreads / 2;
3239       size_t hi_threads = nthreads - lo_threads;
3240       node->lo_child = node_pool;
3241       node_pool = init_node (node, node_pool, lo, lo_threads,
3242                              total_lines, true);
3243       node->hi_child = node_pool;
3244       node_pool = init_node (node, node_pool, hi, hi_threads,
3245                              total_lines, false);
3246     }
3247   else
3248     {
3249       node->lo_child = NULL;
3250       node->hi_child = NULL;
3251     }
3252   return node_pool;
3253 }
3254
3255
3256 /* Compare two merge nodes A and B for priority.  */
3257
3258 static int
3259 compare_nodes (void const *a, void const *b)
3260 {
3261   struct merge_node const *nodea = a;
3262   struct merge_node const *nodeb = b;
3263   if (nodea->level == nodeb->level)
3264       return (nodea->nlo + nodea->nhi) < (nodeb->nlo + nodeb->nhi);
3265   return nodea->level < nodeb->level;
3266 }
3267
3268 /* Lock a merge tree NODE.  */
3269
3270 static inline void
3271 lock_node (struct merge_node *node)
3272 {
3273   pthread_mutex_lock (&node->lock);
3274 }
3275
3276 /* Unlock a merge tree NODE. */
3277
3278 static inline void
3279 unlock_node (struct merge_node *node)
3280 {
3281   pthread_mutex_unlock (&node->lock);
3282 }
3283
3284 /* Destroy merge QUEUE. */
3285
3286 static void
3287 queue_destroy (struct merge_node_queue *queue)
3288 {
3289   heap_free (queue->priority_queue);
3290   pthread_cond_destroy (&queue->cond);
3291   pthread_mutex_destroy (&queue->mutex);
3292 }
3293
3294 /* Initialize merge QUEUE, allocating space suitable for a maximum of
3295    NTHREADS threads.  */
3296
3297 static void
3298 queue_init (struct merge_node_queue *queue, size_t nthreads)
3299 {
3300   /* Though it's highly unlikely all nodes are in the heap at the same
3301      time, the heap should accommodate all of them.  Counting a NULL
3302      dummy head for the heap, reserve 2 * NTHREADS nodes.  */
3303   queue->priority_queue = heap_alloc (compare_nodes, 2 * nthreads);
3304   pthread_mutex_init (&queue->mutex, NULL);
3305   pthread_cond_init (&queue->cond, NULL);
3306 }
3307
3308 /* Insert NODE into QUEUE.  The caller either holds a lock on NODE, or
3309    does not need to lock NODE.  */
3310
3311 static void
3312 queue_insert (struct merge_node_queue *queue, struct merge_node *node)
3313 {
3314   pthread_mutex_lock (&queue->mutex);
3315   heap_insert (queue->priority_queue, node);
3316   node->queued = true;
3317   pthread_mutex_unlock (&queue->mutex);
3318   pthread_cond_signal (&queue->cond);
3319 }
3320
3321 /* Pop the top node off the priority QUEUE, lock the node, return it.  */
3322
3323 static struct merge_node *
3324 queue_pop (struct merge_node_queue *queue)
3325 {
3326   struct merge_node *node;
3327   pthread_mutex_lock (&queue->mutex);
3328   while (! (node = heap_remove_top (queue->priority_queue)))
3329     pthread_cond_wait (&queue->cond, &queue->mutex);
3330   pthread_mutex_unlock (&queue->mutex);
3331   lock_node (node);
3332   node->queued = false;
3333   return node;
3334 }
3335
3336 /* Output LINE to TFP, unless -u is specified and the line compares
3337    equal to the previous line.  TEMP_OUTPUT is the name of TFP, or
3338    is null if TFP is standard output.
3339
3340    This function does not save the line for comparison later, so it is
3341    appropriate only for internal sort.  */
3342
3343 static void
3344 write_unique (struct line const *line, FILE *tfp, char const *temp_output)
3345 {
3346   static struct line saved;
3347
3348   if (unique)
3349     {
3350       if (saved.text && ! compare (line, &saved))
3351         return;
3352       saved = *line;
3353     }
3354
3355   write_line (line, tfp, temp_output);
3356 }
3357
3358 /* Merge the lines currently available to a NODE in the binary
3359    merge tree.  Merge a number of lines appropriate for this merge
3360    level, assuming TOTAL_LINES is the total number of lines.
3361
3362    If merging at the top level, send output to TFP.  TEMP_OUTPUT is
3363    the name of TFP, or is null if TFP is standard output.  */
3364
3365 static void
3366 mergelines_node (struct merge_node *restrict node, size_t total_lines,
3367                  FILE *tfp, char const *temp_output)
3368 {
3369   struct line *lo_orig = node->lo;
3370   struct line *hi_orig = node->hi;
3371   size_t to_merge = MAX_MERGE (total_lines, node->level);
3372   size_t merged_lo;
3373   size_t merged_hi;
3374
3375   if (node->level > MERGE_ROOT)
3376     {
3377       /* Merge to destination buffer. */
3378       struct line *dest = *node->dest;
3379       while (node->lo != node->end_lo && node->hi != node->end_hi && to_merge--)
3380         if (compare (node->lo - 1, node->hi - 1) <= 0)
3381           *--dest = *--node->lo;
3382         else
3383           *--dest = *--node->hi;
3384
3385       merged_lo = lo_orig - node->lo;
3386       merged_hi = hi_orig - node->hi;
3387
3388       if (node->nhi == merged_hi)
3389         while (node->lo != node->end_lo && to_merge--)
3390           *--dest = *--node->lo;
3391       else if (node->nlo == merged_lo)
3392         while (node->hi != node->end_hi && to_merge--)
3393           *--dest = *--node->hi;
3394       *node->dest = dest;
3395     }
3396   else
3397     {
3398       /* Merge directly to output. */
3399       while (node->lo != node->end_lo && node->hi != node->end_hi && to_merge--)
3400         {
3401           if (compare (node->lo - 1, node->hi - 1) <= 0)
3402             write_unique (--node->lo, tfp, temp_output);
3403           else
3404             write_unique (--node->hi, tfp, temp_output);
3405         }
3406
3407       merged_lo = lo_orig - node->lo;
3408       merged_hi = hi_orig - node->hi;
3409
3410       if (node->nhi == merged_hi)
3411         {
3412           while (node->lo != node->end_lo && to_merge--)
3413             write_unique (--node->lo, tfp, temp_output);
3414         }
3415       else if (node->nlo == merged_lo)
3416         {
3417           while (node->hi != node->end_hi && to_merge--)
3418             write_unique (--node->hi, tfp, temp_output);
3419         }
3420     }
3421
3422   /* Update NODE. */
3423   merged_lo = lo_orig - node->lo;
3424   merged_hi = hi_orig - node->hi;
3425   node->nlo -= merged_lo;
3426   node->nhi -= merged_hi;
3427 }
3428
3429 /* Into QUEUE, insert NODE if it is not already queued, and if one of
3430    NODE's children has available lines and the other either has
3431    available lines or has exhausted its lines.  */
3432
3433 static void
3434 queue_check_insert (struct merge_node_queue *queue, struct merge_node *node)
3435 {
3436   if (! node->queued)
3437     {
3438       bool lo_avail = (node->lo - node->end_lo) != 0;
3439       bool hi_avail = (node->hi - node->end_hi) != 0;
3440       if (lo_avail ? hi_avail || ! node->nhi : hi_avail && ! node->nlo)
3441         queue_insert (queue, node);
3442     }
3443 }
3444
3445 /* Into QUEUE, insert NODE's parent if the parent can now be worked on.  */
3446
3447 static void
3448 queue_check_insert_parent (struct merge_node_queue *queue,
3449                            struct merge_node *node)
3450 {
3451   if (node->level > MERGE_ROOT)
3452     {
3453       lock_node (node->parent);
3454       queue_check_insert (queue, node->parent);
3455       unlock_node (node->parent);
3456     }
3457   else if (node->nlo + node->nhi == 0)
3458     {
3459       /* If the MERGE_ROOT NODE has finished merging, insert the
3460          MERGE_END node.  */
3461       queue_insert (queue, node->parent);
3462     }
3463 }
3464
3465 /* Repeatedly pop QUEUE for a node with lines to merge, and merge at least
3466    some of those lines, until the MERGE_END node is popped.
3467    TOTAL_LINES is the total number of lines.  If merging at the top
3468    level, send output to TFP.  TEMP_OUTPUT is the name of TFP, or is
3469    null if TFP is standard output.  */
3470
3471 static void
3472 merge_loop (struct merge_node_queue *queue,
3473             size_t total_lines, FILE *tfp, char const *temp_output)
3474 {
3475   while (1)
3476     {
3477       struct merge_node *node = queue_pop (queue);
3478
3479       if (node->level == MERGE_END)
3480         {
3481           unlock_node (node);
3482           /* Reinsert so other threads can pop it. */
3483           queue_insert (queue, node);
3484           break;
3485         }
3486       mergelines_node (node, total_lines, tfp, temp_output);
3487       queue_check_insert (queue, node);
3488       queue_check_insert_parent (queue, node);
3489
3490       unlock_node (node);
3491     }
3492 }
3493
3494
3495 static void sortlines (struct line *restrict, size_t, size_t,
3496                        struct merge_node *, struct merge_node_queue *,
3497                        FILE *, char const *);
3498
3499 /* Thread arguments for sortlines_thread. */
3500
3501 struct thread_args
3502 {
3503   /* Source, i.e., the array of lines to sort.  This points just past
3504      the end of the array.  */
3505   struct line *lines;
3506
3507   /* Number of threads to use.  If 0 or 1, sort single-threaded.  */
3508   size_t nthreads;
3509
3510   /* Number of lines in LINES and DEST.  */
3511   size_t const total_lines;
3512
3513   /* Merge node. Lines from this node and this node's sibling will merged
3514      to this node's parent. */
3515   struct merge_node *const node;
3516
3517   /* The priority queue controlling available work for the entire
3518      internal sort.  */
3519   struct merge_node_queue *const queue;
3520
3521   /* If at the top level, the file to output to, and the file's name.
3522      If the file is standard output, the file's name is null.  */
3523   FILE *tfp;
3524   char const *output_temp;
3525 };
3526
3527 /* Like sortlines, except with a signature acceptable to pthread_create.  */
3528
3529 static void *
3530 sortlines_thread (void *data)
3531 {
3532   struct thread_args const *args = data;
3533   sortlines (args->lines, args->nthreads, args->total_lines,
3534              args->node, args->queue, args->tfp,
3535              args->output_temp);
3536   return NULL;
3537 }
3538
3539 /* Sort lines, possibly in parallel.  The arguments are as in struct
3540    thread_args above.
3541
3542    The algorithm has three phases: node creation, sequential sort,
3543    and binary merge.
3544
3545    During node creation, sortlines recursively visits each node in the
3546    binary merge tree and creates a NODE structure corresponding to all the
3547    future line merging NODE is responsible for. For each call to
3548    sortlines, half the available threads are assigned to each recursive
3549    call, until a leaf node having only 1 available thread is reached.
3550
3551    Each leaf node then performs two sequential sorts, one on each half of
3552    the lines it is responsible for. It records in its NODE structure that
3553    there are two sorted sublists available to merge from, and inserts its
3554    NODE into the priority queue.
3555
3556    The binary merge phase then begins. Each thread drops into a loop
3557    where the thread retrieves a NODE from the priority queue, merges lines
3558    available to that NODE, and potentially insert NODE or its parent back
3559    into the queue if there are sufficient available lines for them to
3560    merge. This continues until all lines at all nodes of the merge tree
3561    have been merged. */
3562
3563 static void
3564 sortlines (struct line *restrict lines, size_t nthreads,
3565            size_t total_lines, struct merge_node *node,
3566            struct merge_node_queue *queue, FILE *tfp, char const *temp_output)
3567 {
3568   size_t nlines = node->nlo + node->nhi;
3569
3570   /* Calculate thread arguments. */
3571   size_t lo_threads = nthreads / 2;
3572   size_t hi_threads = nthreads - lo_threads;
3573   pthread_t thread;
3574   struct thread_args args = {lines, lo_threads, total_lines,
3575                              node->lo_child, queue, tfp, temp_output};
3576
3577   if (nthreads > 1 && SUBTHREAD_LINES_HEURISTIC <= nlines
3578       && pthread_create (&thread, NULL, sortlines_thread, &args) == 0)
3579     {
3580       sortlines (lines - node->nlo, hi_threads, total_lines,
3581                  node->hi_child, queue, tfp, temp_output);
3582       pthread_join (thread, NULL);
3583     }
3584   else
3585     {
3586       /* Nthreads = 1, this is a leaf NODE, or pthread_create failed.
3587          Sort with 1 thread. */
3588       size_t nlo = node->nlo;
3589       size_t nhi = node->nhi;
3590       struct line *temp = lines - total_lines;
3591       if (1 < nhi)
3592         sequential_sort (lines - nlo, nhi, temp - nlo / 2, false);
3593       if (1 < nlo)
3594         sequential_sort (lines, nlo, temp, false);
3595
3596       /* Update merge NODE. No need to lock yet. */
3597       node->lo = lines;
3598       node->hi = lines - nlo;
3599       node->end_lo = lines - nlo;
3600       node->end_hi = lines - nlo - nhi;
3601
3602       queue_insert (queue, node);
3603       merge_loop (queue, total_lines, tfp, temp_output);
3604     }
3605
3606   pthread_mutex_destroy (&node->lock);
3607 }
3608
3609 /* Scan through FILES[NTEMPS .. NFILES-1] looking for files that are
3610    the same as OUTFILE.  If found, replace each with the same
3611    temporary copy that can be merged into OUTFILE without destroying
3612    OUTFILE before it is completely read.  This temporary copy does not
3613    count as a merge temp, so don't worry about incrementing NTEMPS in
3614    the caller; final cleanup will remove it, not zaptemp.
3615
3616    This test ensures that an otherwise-erroneous use like
3617    "sort -m -o FILE ... FILE ..." copies FILE before writing to it.
3618    It's not clear that POSIX requires this nicety.
3619    Detect common error cases, but don't try to catch obscure cases like
3620    "cat ... FILE ... | sort -m -o FILE"
3621    where traditional "sort" doesn't copy the input and where
3622    people should know that they're getting into trouble anyway.
3623    Catching these obscure cases would slow down performance in
3624    common cases.  */
3625
3626 static void
3627 avoid_trashing_input (struct sortfile *files, size_t ntemps,
3628                       size_t nfiles, char const *outfile)
3629 {
3630   size_t i;
3631   bool got_outstat = false;
3632   struct stat outstat;
3633   struct tempnode *tempcopy = NULL;
3634
3635   for (i = ntemps; i < nfiles; i++)
3636     {
3637       bool is_stdin = STREQ (files[i].name, "-");
3638       bool same;
3639       struct stat instat;
3640
3641       if (outfile && STREQ (outfile, files[i].name) && !is_stdin)
3642         same = true;
3643       else
3644         {
3645           if (! got_outstat)
3646             {
3647               if (fstat (STDOUT_FILENO, &outstat) != 0)
3648                 break;
3649               got_outstat = true;
3650             }
3651
3652           same = (((is_stdin
3653                     ? fstat (STDIN_FILENO, &instat)
3654                     : stat (files[i].name, &instat))
3655                    == 0)
3656                   && SAME_INODE (instat, outstat));
3657         }
3658
3659       if (same)
3660         {
3661           if (! tempcopy)
3662             {
3663               FILE *tftp;
3664               tempcopy = create_temp (&tftp);
3665               mergefiles (&files[i], 0, 1, tftp, tempcopy->name);
3666             }
3667
3668           files[i].name = tempcopy->name;
3669           files[i].temp = tempcopy;
3670         }
3671     }
3672 }
3673
3674 /* Scan the input files to ensure all are accessible.
3675    Otherwise exit with a diagnostic.
3676
3677    Note this will catch common issues with permissions etc.
3678    but will fail to notice issues where you can open() but not read(),
3679    like when a directory is specified on some systems.
3680    Catching these obscure cases could slow down performance in
3681    common cases.  */
3682
3683 static void
3684 check_inputs (char *const *files, size_t nfiles)
3685 {
3686   size_t i;
3687   for (i = 0; i < nfiles; i++)
3688     {
3689       if (STREQ (files[i], "-"))
3690         continue;
3691
3692       if (euidaccess (files[i], R_OK) != 0)
3693         die (_("cannot read"), files[i]);
3694     }
3695 }
3696
3697 /* Ensure a specified output file can be created or written to,
3698    and point stdout to it.  Do not truncate the file.
3699    Exit with a diagnostic on failure.  */
3700
3701 static void
3702 check_output (char const *outfile)
3703 {
3704   if (outfile)
3705     {
3706       int outfd = open (outfile, O_WRONLY | O_CREAT | O_BINARY, MODE_RW_UGO);
3707       if (outfd < 0)
3708         die (_("open failed"), outfile);
3709       move_fd_or_die (outfd, STDOUT_FILENO);
3710     }
3711 }
3712
3713 /* Merge the input FILES.  NTEMPS is the number of files at the
3714    start of FILES that are temporary; it is zero at the top level.
3715    NFILES is the total number of files.  Put the output in
3716    OUTPUT_FILE; a null OUTPUT_FILE stands for standard output.  */
3717
3718 static void
3719 merge (struct sortfile *files, size_t ntemps, size_t nfiles,
3720        char const *output_file)
3721 {
3722   while (nmerge < nfiles)
3723     {
3724       /* Number of input files processed so far.  */
3725       size_t in;
3726
3727       /* Number of output files generated so far.  */
3728       size_t out;
3729
3730       /* nfiles % NMERGE; this counts input files that are left over
3731          after all full-sized merges have been done.  */
3732       size_t remainder;
3733
3734       /* Number of easily-available slots at the next loop iteration.  */
3735       size_t cheap_slots;
3736
3737       /* Do as many NMERGE-size merges as possible. In the case that
3738          nmerge is bogus, increment by the maximum number of file
3739          descriptors allowed.  */
3740       for (out = in = 0; nmerge <= nfiles - in; out++)
3741         {
3742           FILE *tfp;
3743           struct tempnode *temp = create_temp (&tfp);
3744           size_t num_merged = mergefiles (&files[in], MIN (ntemps, nmerge),
3745                                           nmerge, tfp, temp->name);
3746           ntemps -= MIN (ntemps, num_merged);
3747           files[out].name = temp->name;
3748           files[out].temp = temp;
3749           in += num_merged;
3750         }
3751
3752       remainder = nfiles - in;
3753       cheap_slots = nmerge - out % nmerge;
3754
3755       if (cheap_slots < remainder)
3756         {
3757           /* So many files remain that they can't all be put into the last
3758              NMERGE-sized output window.  Do one more merge.  Merge as few
3759              files as possible, to avoid needless I/O.  */
3760           size_t nshortmerge = remainder - cheap_slots + 1;
3761           FILE *tfp;
3762           struct tempnode *temp = create_temp (&tfp);
3763           size_t num_merged = mergefiles (&files[in], MIN (ntemps, nshortmerge),
3764                                           nshortmerge, tfp, temp->name);
3765           ntemps -= MIN (ntemps, num_merged);
3766           files[out].name = temp->name;
3767           files[out++].temp = temp;
3768           in += num_merged;
3769         }
3770
3771       /* Put the remaining input files into the last NMERGE-sized output
3772          window, so they will be merged in the next pass.  */
3773       memmove (&files[out], &files[in], (nfiles - in) * sizeof *files);
3774       ntemps += out;
3775       nfiles -= in - out;
3776     }
3777
3778   avoid_trashing_input (files, ntemps, nfiles, output_file);
3779
3780   /* We aren't guaranteed that this final mergefiles will work, therefore we
3781      try to merge into the output, and then merge as much as we can into a
3782      temp file if we can't. Repeat.  */
3783
3784   while (true)
3785     {
3786       /* Merge directly into the output file if possible.  */
3787       FILE **fps;
3788       size_t nopened = open_input_files (files, nfiles, &fps);
3789
3790       if (nopened == nfiles)
3791         {
3792           FILE *ofp = stream_open (output_file, "w");
3793           if (ofp)
3794             {
3795               mergefps (files, ntemps, nfiles, ofp, output_file, fps);
3796               break;
3797             }
3798           if (errno != EMFILE || nopened <= 2)
3799             die (_("open failed"), output_file);
3800         }
3801       else if (nopened <= 2)
3802         die (_("open failed"), files[nopened].name);
3803
3804       /* We ran out of file descriptors.  Close one of the input
3805          files, to gain a file descriptor.  Then create a temporary
3806          file with our spare file descriptor.  Retry if that failed
3807          (e.g., some other process could open a file between the time
3808          we closed and tried to create).  */
3809       FILE *tfp;
3810       struct tempnode *temp;
3811       do
3812         {
3813           nopened--;
3814           xfclose (fps[nopened], files[nopened].name);
3815           temp = maybe_create_temp (&tfp, ! (nopened <= 2));
3816         }
3817       while (!temp);
3818
3819       /* Merge into the newly allocated temporary.  */
3820       mergefps (&files[0], MIN (ntemps, nopened), nopened, tfp, temp->name,
3821                 fps);
3822       ntemps -= MIN (ntemps, nopened);
3823       files[0].name = temp->name;
3824       files[0].temp = temp;
3825
3826       memmove (&files[1], &files[nopened], (nfiles - nopened) * sizeof *files);
3827       ntemps++;
3828       nfiles -= nopened - 1;
3829     }
3830 }
3831
3832 /* Sort NFILES FILES onto OUTPUT_FILE.  Use at most NTHREADS threads.  */
3833
3834 static void
3835 sort (char *const *files, size_t nfiles, char const *output_file,
3836       size_t nthreads)
3837 {
3838   struct buffer buf;
3839   IF_LINT (buf.buf = NULL);
3840   size_t ntemps = 0;
3841   bool output_file_created = false;
3842
3843   buf.alloc = 0;
3844
3845   while (nfiles)
3846     {
3847       char const *temp_output;
3848       char const *file = *files;
3849       FILE *fp = xfopen (file, "r");
3850       FILE *tfp;
3851
3852       size_t bytes_per_line;
3853       if (nthreads > 1)
3854         {
3855           /* Get log P. */
3856           size_t tmp = 1;
3857           size_t mult = 1;
3858           while (tmp < nthreads)
3859             {
3860               tmp *= 2;
3861               mult++;
3862             }
3863           bytes_per_line = (mult * sizeof (struct line));
3864         }
3865       else
3866         bytes_per_line = sizeof (struct line) * 3 / 2;
3867
3868       if (! buf.alloc)
3869         initbuf (&buf, bytes_per_line,
3870                  sort_buffer_size (&fp, 1, files, nfiles, bytes_per_line));
3871       buf.eof = false;
3872       files++;
3873       nfiles--;
3874
3875       while (fillbuf (&buf, fp, file))
3876         {
3877           struct line *line;
3878
3879           if (buf.eof && nfiles
3880               && (bytes_per_line + 1
3881                   < (buf.alloc - buf.used - bytes_per_line * buf.nlines)))
3882             {
3883               /* End of file, but there is more input and buffer room.
3884                  Concatenate the next input file; this is faster in
3885                  the usual case.  */
3886               buf.left = buf.used;
3887               break;
3888             }
3889
3890           line = buffer_linelim (&buf);
3891           if (buf.eof && !nfiles && !ntemps && !buf.left)
3892             {
3893               xfclose (fp, file);
3894               tfp = xfopen (output_file, "w");
3895               temp_output = output_file;
3896               output_file_created = true;
3897             }
3898           else
3899             {
3900               ++ntemps;
3901               temp_output = create_temp (&tfp)->name;
3902             }
3903           if (1 < buf.nlines)
3904             {
3905               struct merge_node_queue queue;
3906               queue_init (&queue, nthreads);
3907               struct merge_node *merge_tree =
3908                 merge_tree_init (nthreads, buf.nlines, line);
3909               struct merge_node *root = merge_tree + 1;
3910
3911               sortlines (line, nthreads, buf.nlines, root,
3912                          &queue, tfp, temp_output);
3913               queue_destroy (&queue);
3914               pthread_mutex_destroy (&root->lock);
3915               merge_tree_destroy (merge_tree);
3916             }
3917           else
3918             write_unique (line - 1, tfp, temp_output);
3919
3920           xfclose (tfp, temp_output);
3921
3922           if (output_file_created)
3923             goto finish;
3924         }
3925       xfclose (fp, file);
3926     }
3927
3928  finish:
3929   free (buf.buf);
3930
3931   if (! output_file_created)
3932     {
3933       size_t i;
3934       struct tempnode *node = temphead;
3935       struct sortfile *tempfiles = xnmalloc (ntemps, sizeof *tempfiles);
3936       for (i = 0; node; i++)
3937         {
3938           tempfiles[i].name = node->name;
3939           tempfiles[i].temp = node;
3940           node = node->next;
3941         }
3942       merge (tempfiles, ntemps, ntemps, output_file);
3943       free (tempfiles);
3944     }
3945
3946   reap_all ();
3947 }
3948
3949 /* Insert a malloc'd copy of key KEY_ARG at the end of the key list.  */
3950
3951 static void
3952 insertkey (struct keyfield *key_arg)
3953 {
3954   struct keyfield **p;
3955   struct keyfield *key = xmemdup (key_arg, sizeof *key);
3956
3957   for (p = &keylist; *p; p = &(*p)->next)
3958     continue;
3959   *p = key;
3960   key->next = NULL;
3961 }
3962
3963 /* Report a bad field specification SPEC, with extra info MSGID.  */
3964
3965 static void badfieldspec (char const *, char const *)
3966      ATTRIBUTE_NORETURN;
3967 static void
3968 badfieldspec (char const *spec, char const *msgid)
3969 {
3970   error (SORT_FAILURE, 0, _("%s: invalid field specification %s"),
3971          _(msgid), quote (spec));
3972   abort ();
3973 }
3974
3975 /* Report incompatible options.  */
3976
3977 static void incompatible_options (char const *) ATTRIBUTE_NORETURN;
3978 static void
3979 incompatible_options (char const *opts)
3980 {
3981   error (SORT_FAILURE, 0, _("options '-%s' are incompatible"), opts);
3982   abort ();
3983 }
3984
3985 /* Check compatibility of ordering options.  */
3986
3987 static void
3988 check_ordering_compatibility (void)
3989 {
3990   struct keyfield *key;
3991
3992   for (key = keylist; key; key = key->next)
3993     if (1 < (key->numeric + key->general_numeric + key->human_numeric
3994              + key->month + (key->version | key->random | !!key->ignore)))
3995       {
3996         /* The following is too big, but guaranteed to be "big enough".  */
3997         char opts[sizeof short_options];
3998         /* Clear flags we're not interested in.  */
3999         key->skipsblanks = key->skipeblanks = key->reverse = false;
4000         key_to_opts (key, opts);
4001         incompatible_options (opts);
4002       }
4003 }
4004
4005 /* Parse the leading integer in STRING and store the resulting value
4006    (which must fit into size_t) into *VAL.  Return the address of the
4007    suffix after the integer.  If the value is too large, silently
4008    substitute SIZE_MAX.  If MSGID is NULL, return NULL after
4009    failure; otherwise, report MSGID and exit on failure.  */
4010
4011 static char const *
4012 parse_field_count (char const *string, size_t *val, char const *msgid)
4013 {
4014   char *suffix;
4015   uintmax_t n;
4016
4017   switch (xstrtoumax (string, &suffix, 10, &n, ""))
4018     {
4019     case LONGINT_OK:
4020     case LONGINT_INVALID_SUFFIX_CHAR:
4021       *val = n;
4022       if (*val == n)
4023         break;
4024       /* Fall through.  */
4025     case LONGINT_OVERFLOW:
4026     case LONGINT_OVERFLOW | LONGINT_INVALID_SUFFIX_CHAR:
4027       *val = SIZE_MAX;
4028       break;
4029
4030     case LONGINT_INVALID:
4031       if (msgid)
4032         error (SORT_FAILURE, 0, _("%s: invalid count at start of %s"),
4033                _(msgid), quote (string));
4034       return NULL;
4035     }
4036
4037   return suffix;
4038 }
4039
4040 /* Handle interrupts and hangups. */
4041
4042 static void
4043 sighandler (int sig)
4044 {
4045   if (! SA_NOCLDSTOP)
4046     signal (sig, SIG_IGN);
4047
4048   cleanup ();
4049
4050   signal (sig, SIG_DFL);
4051   raise (sig);
4052 }
4053
4054 /* Set the ordering options for KEY specified in S.
4055    Return the address of the first character in S that
4056    is not a valid ordering option.
4057    BLANKTYPE is the kind of blanks that 'b' should skip. */
4058
4059 static char *
4060 set_ordering (char const *s, struct keyfield *key, enum blanktype blanktype)
4061 {
4062   while (*s)
4063     {
4064       switch (*s)
4065         {
4066         case 'b':
4067           if (blanktype == bl_start || blanktype == bl_both)
4068             key->skipsblanks = true;
4069           if (blanktype == bl_end || blanktype == bl_both)
4070             key->skipeblanks = true;
4071           break;
4072         case 'd':
4073           key->ignore = nondictionary;
4074           break;
4075         case 'f':
4076           key->translate = fold_toupper;
4077           break;
4078         case 'g':
4079           key->general_numeric = true;
4080           break;
4081         case 'h':
4082           key->human_numeric = true;
4083           break;
4084         case 'i':
4085           /* Option order should not matter, so don't let -i override
4086              -d.  -d implies -i, but -i does not imply -d.  */
4087           if (! key->ignore)
4088             key->ignore = nonprinting;
4089           break;
4090         case 'M':
4091           key->month = true;
4092           break;
4093         case 'n':
4094           key->numeric = true;
4095           break;
4096         case 'R':
4097           key->random = true;
4098           break;
4099         case 'r':
4100           key->reverse = true;
4101           break;
4102         case 'V':
4103           key->version = true;
4104           break;
4105         default:
4106           return (char *) s;
4107         }
4108       ++s;
4109     }
4110   return (char *) s;
4111 }
4112
4113 /* Initialize KEY.  */
4114
4115 static struct keyfield *
4116 key_init (struct keyfield *key)
4117 {
4118   memset (key, 0, sizeof *key);
4119   key->eword = SIZE_MAX;
4120   return key;
4121 }
4122
4123 int
4124 main (int argc, char **argv)
4125 {
4126   struct keyfield *key;
4127   struct keyfield key_buf;
4128   struct keyfield gkey;
4129   bool gkey_only = false;
4130   char const *s;
4131   int c = 0;
4132   char checkonly = 0;
4133   bool mergeonly = false;
4134   char *random_source = NULL;
4135   bool need_random = false;
4136   size_t nthreads = 0;
4137   size_t nfiles = 0;
4138   bool posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
4139   bool obsolete_usage = (posix2_version () < 200112);
4140   char **files;
4141   char *files_from = NULL;
4142   struct Tokens tok;
4143   char const *outfile = NULL;
4144
4145   initialize_main (&argc, &argv);
4146   set_program_name (argv[0]);
4147   setlocale (LC_ALL, "");
4148   bindtextdomain (PACKAGE, LOCALEDIR);
4149   textdomain (PACKAGE);
4150
4151   initialize_exit_failure (SORT_FAILURE);
4152
4153   hard_LC_COLLATE = hard_locale (LC_COLLATE);
4154 #if HAVE_NL_LANGINFO
4155   hard_LC_TIME = hard_locale (LC_TIME);
4156 #endif
4157
4158   /* Get locale's representation of the decimal point.  */
4159   {
4160     struct lconv const *locale = localeconv ();
4161
4162     /* If the locale doesn't define a decimal point, or if the decimal
4163        point is multibyte, use the C locale's decimal point.  FIXME:
4164        add support for multibyte decimal points.  */
4165     decimal_point = to_uchar (locale->decimal_point[0]);
4166     if (! decimal_point || locale->decimal_point[1])
4167       decimal_point = '.';
4168
4169     /* FIXME: add support for multibyte thousands separators.  */
4170     thousands_sep = to_uchar (*locale->thousands_sep);
4171     if (! thousands_sep || locale->thousands_sep[1])
4172       thousands_sep = -1;
4173   }
4174
4175   have_read_stdin = false;
4176   inittables ();
4177
4178   {
4179     size_t i;
4180     static int const sig[] =
4181       {
4182         /* The usual suspects.  */
4183         SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
4184 #ifdef SIGPOLL
4185         SIGPOLL,
4186 #endif
4187 #ifdef SIGPROF
4188         SIGPROF,
4189 #endif
4190 #ifdef SIGVTALRM
4191         SIGVTALRM,
4192 #endif
4193 #ifdef SIGXCPU
4194         SIGXCPU,
4195 #endif
4196 #ifdef SIGXFSZ
4197         SIGXFSZ,
4198 #endif
4199       };
4200     enum { nsigs = ARRAY_CARDINALITY (sig) };
4201
4202 #if SA_NOCLDSTOP
4203     struct sigaction act;
4204
4205     sigemptyset (&caught_signals);
4206     for (i = 0; i < nsigs; i++)
4207       {
4208         sigaction (sig[i], NULL, &act);
4209         if (act.sa_handler != SIG_IGN)
4210           sigaddset (&caught_signals, sig[i]);
4211       }
4212
4213     act.sa_handler = sighandler;
4214     act.sa_mask = caught_signals;
4215     act.sa_flags = 0;
4216
4217     for (i = 0; i < nsigs; i++)
4218       if (sigismember (&caught_signals, sig[i]))
4219         sigaction (sig[i], &act, NULL);
4220 #else
4221     for (i = 0; i < nsigs; i++)
4222       if (signal (sig[i], SIG_IGN) != SIG_IGN)
4223         {
4224           signal (sig[i], sighandler);
4225           siginterrupt (sig[i], 1);
4226         }
4227 #endif
4228   }
4229   signal (SIGCHLD, SIG_DFL); /* Don't inherit CHLD handling from parent.  */
4230
4231   /* The signal mask is known, so it is safe to invoke exit_cleanup.  */
4232   atexit (exit_cleanup);
4233
4234   key_init (&gkey);
4235   gkey.sword = SIZE_MAX;
4236
4237   files = xnmalloc (argc, sizeof *files);
4238
4239   while (true)
4240     {
4241       /* Parse an operand as a file after "--" was seen; or if
4242          pedantic and a file was seen, unless the POSIX version
4243          predates 1003.1-2001 and -c was not seen and the operand is
4244          "-o FILE" or "-oFILE".  */
4245       int oi = -1;
4246
4247       if (c == -1
4248           || (posixly_correct && nfiles != 0
4249               && ! (obsolete_usage
4250                     && ! checkonly
4251                     && optind != argc
4252                     && argv[optind][0] == '-' && argv[optind][1] == 'o'
4253                     && (argv[optind][2] || optind + 1 != argc)))
4254           || ((c = getopt_long (argc, argv, short_options,
4255                                 long_options, &oi))
4256               == -1))
4257         {
4258           if (argc <= optind)
4259             break;
4260           files[nfiles++] = argv[optind++];
4261         }
4262       else switch (c)
4263         {
4264         case 1:
4265           key = NULL;
4266           if (optarg[0] == '+')
4267             {
4268               bool minus_pos_usage = (optind != argc && argv[optind][0] == '-'
4269                                       && ISDIGIT (argv[optind][1]));
4270               obsolete_usage |= minus_pos_usage && !posixly_correct;
4271               if (obsolete_usage)
4272                 {
4273                   /* Treat +POS1 [-POS2] as a key if possible; but silently
4274                      treat an operand as a file if it is not a valid +POS1.  */
4275                   key = key_init (&key_buf);
4276                   s = parse_field_count (optarg + 1, &key->sword, NULL);
4277                   if (s && *s == '.')
4278                     s = parse_field_count (s + 1, &key->schar, NULL);
4279                   if (! (key->sword || key->schar))
4280                     key->sword = SIZE_MAX;
4281                   if (! s || *set_ordering (s, key, bl_start))
4282                     key = NULL;
4283                   else
4284                     {
4285                       if (minus_pos_usage)
4286                         {
4287                           char const *optarg1 = argv[optind++];
4288                           s = parse_field_count (optarg1 + 1, &key->eword,
4289                                              N_("invalid number after '-'"));
4290                           /* When called with a non-NULL message ID,
4291                              parse_field_count cannot return NULL.  Tell static
4292                              analysis tools that dereferencing S is safe.  */
4293                           assert (s);
4294                           if (*s == '.')
4295                             s = parse_field_count (s + 1, &key->echar,
4296                                                N_("invalid number after '.'"));
4297                           if (!key->echar && key->eword)
4298                             {
4299                               /* obsolescent syntax +A.x -B.y is equivalent to:
4300                                    -k A+1.x+1,B.y   (when y = 0)
4301                                    -k A+1.x+1,B+1.y (when y > 0)
4302                                  So eword is decremented as in the -k case
4303                                  only when the end field (B) is specified and
4304                                  echar (y) is 0.  */
4305                               key->eword--;
4306                             }
4307                           if (*set_ordering (s, key, bl_end))
4308                             badfieldspec (optarg1,
4309                                       N_("stray character in field spec"));
4310                         }
4311                       key->obsolete_used = true;
4312                       insertkey (key);
4313                     }
4314                 }
4315             }
4316           if (! key)
4317             files[nfiles++] = optarg;
4318           break;
4319
4320         case SORT_OPTION:
4321           c = XARGMATCH ("--sort", optarg, sort_args, sort_types);
4322           /* Fall through. */
4323         case 'b':
4324         case 'd':
4325         case 'f':
4326         case 'g':
4327         case 'h':
4328         case 'i':
4329         case 'M':
4330         case 'n':
4331         case 'r':
4332         case 'R':
4333         case 'V':
4334           {
4335             char str[2];
4336             str[0] = c;
4337             str[1] = '\0';
4338             set_ordering (str, &gkey, bl_both);
4339           }
4340           break;
4341
4342         case CHECK_OPTION:
4343           c = (optarg
4344                ? XARGMATCH ("--check", optarg, check_args, check_types)
4345                : 'c');
4346           /* Fall through.  */
4347         case 'c':
4348         case 'C':
4349           if (checkonly && checkonly != c)
4350             incompatible_options ("cC");
4351           checkonly = c;
4352           break;
4353
4354         case COMPRESS_PROGRAM_OPTION:
4355           if (compress_program && !STREQ (compress_program, optarg))
4356             error (SORT_FAILURE, 0, _("multiple compress programs specified"));
4357           compress_program = optarg;
4358           break;
4359
4360         case DEBUG_PROGRAM_OPTION:
4361           debug = true;
4362           break;
4363
4364         case FILES0_FROM_OPTION:
4365           files_from = optarg;
4366           break;
4367
4368         case 'k':
4369           key = key_init (&key_buf);
4370
4371           /* Get POS1. */
4372           s = parse_field_count (optarg, &key->sword,
4373                                  N_("invalid number at field start"));
4374           if (! key->sword--)
4375             {
4376               /* Provoke with 'sort -k0' */
4377               badfieldspec (optarg, N_("field number is zero"));
4378             }
4379           if (*s == '.')
4380             {
4381               s = parse_field_count (s + 1, &key->schar,
4382                                      N_("invalid number after '.'"));
4383               if (! key->schar--)
4384                 {
4385                   /* Provoke with 'sort -k1.0' */
4386                   badfieldspec (optarg, N_("character offset is zero"));
4387                 }
4388             }
4389           if (! (key->sword || key->schar))
4390             key->sword = SIZE_MAX;
4391           s = set_ordering (s, key, bl_start);
4392           if (*s != ',')
4393             {
4394               key->eword = SIZE_MAX;
4395               key->echar = 0;
4396             }
4397           else
4398             {
4399               /* Get POS2. */
4400               s = parse_field_count (s + 1, &key->eword,
4401                                      N_("invalid number after ','"));
4402               if (! key->eword--)
4403                 {
4404                   /* Provoke with 'sort -k1,0' */
4405                   badfieldspec (optarg, N_("field number is zero"));
4406                 }
4407               if (*s == '.')
4408                 {
4409                   s = parse_field_count (s + 1, &key->echar,
4410                                          N_("invalid number after '.'"));
4411                 }
4412               s = set_ordering (s, key, bl_end);
4413             }
4414           if (*s)
4415             badfieldspec (optarg, N_("stray character in field spec"));
4416           insertkey (key);
4417           break;
4418
4419         case 'm':
4420           mergeonly = true;
4421           break;
4422
4423         case NMERGE_OPTION:
4424           specify_nmerge (oi, c, optarg);
4425           break;
4426
4427         case 'o':
4428           if (outfile && !STREQ (outfile, optarg))
4429             error (SORT_FAILURE, 0, _("multiple output files specified"));
4430           outfile = optarg;
4431           break;
4432
4433         case RANDOM_SOURCE_OPTION:
4434           if (random_source && !STREQ (random_source, optarg))
4435             error (SORT_FAILURE, 0, _("multiple random sources specified"));
4436           random_source = optarg;
4437           break;
4438
4439         case 's':
4440           stable = true;
4441           break;
4442
4443         case 'S':
4444           specify_sort_size (oi, c, optarg);
4445           break;
4446
4447         case 't':
4448           {
4449             char newtab = optarg[0];
4450             if (! newtab)
4451               error (SORT_FAILURE, 0, _("empty tab"));
4452             if (optarg[1])
4453               {
4454                 if (STREQ (optarg, "\\0"))
4455                   newtab = '\0';
4456                 else
4457                   {
4458                     /* Provoke with 'sort -txx'.  Complain about
4459                        "multi-character tab" instead of "multibyte tab", so
4460                        that the diagnostic's wording does not need to be
4461                        changed once multibyte characters are supported.  */
4462                     error (SORT_FAILURE, 0, _("multi-character tab %s"),
4463                            quote (optarg));
4464                   }
4465               }
4466             if (tab != TAB_DEFAULT && tab != newtab)
4467               error (SORT_FAILURE, 0, _("incompatible tabs"));
4468             tab = newtab;
4469           }
4470           break;
4471
4472         case 'T':
4473           add_temp_dir (optarg);
4474           break;
4475
4476         case PARALLEL_OPTION:
4477           nthreads = specify_nthreads (oi, c, optarg);
4478           break;
4479
4480         case 'u':
4481           unique = true;
4482           break;
4483
4484         case 'y':
4485           /* Accept and ignore e.g. -y0 for compatibility with Solaris 2.x
4486              through Solaris 7.  It is also accepted by many non-Solaris
4487              "sort" implementations, e.g., AIX 5.2, HP-UX 11i v2, IRIX 6.5.
4488              -y is marked as obsolete starting with Solaris 8 (1999), but is
4489              still accepted as of Solaris 10 prerelease (2004).
4490
4491              Solaris 2.5.1 "sort -y 100" reads the input file "100", but
4492              emulate Solaris 8 and 9 "sort -y 100" which ignores the "100",
4493              and which in general ignores the argument after "-y" if it
4494              consists entirely of digits (it can even be empty).  */
4495           if (optarg == argv[optind - 1])
4496             {
4497               char const *p;
4498               for (p = optarg; ISDIGIT (*p); p++)
4499                 continue;
4500               optind -= (*p != '\0');
4501             }
4502           break;
4503
4504         case 'z':
4505           eolchar = 0;
4506           break;
4507
4508         case_GETOPT_HELP_CHAR;
4509
4510         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
4511
4512         default:
4513           usage (SORT_FAILURE);
4514         }
4515     }
4516
4517   if (files_from)
4518     {
4519       FILE *stream;
4520
4521       /* When using --files0-from=F, you may not specify any files
4522          on the command-line.  */
4523       if (nfiles)
4524         {
4525           error (0, 0, _("extra operand %s"), quote (files[0]));
4526           fprintf (stderr, "%s\n",
4527                    _("file operands cannot be combined with --files0-from"));
4528           usage (SORT_FAILURE);
4529         }
4530
4531       if (STREQ (files_from, "-"))
4532         stream = stdin;
4533       else
4534         {
4535           stream = fopen (files_from, "r");
4536           if (stream == NULL)
4537             error (SORT_FAILURE, errno, _("cannot open %s for reading"),
4538                    quote (files_from));
4539         }
4540
4541       readtokens0_init (&tok);
4542
4543       if (! readtokens0 (stream, &tok) || fclose (stream) != 0)
4544         error (SORT_FAILURE, 0, _("cannot read file names from %s"),
4545                quote (files_from));
4546
4547       if (tok.n_tok)
4548         {
4549           size_t i;
4550           free (files);
4551           files = tok.tok;
4552           nfiles = tok.n_tok;
4553           for (i = 0; i < nfiles; i++)
4554             {
4555               if (STREQ (files[i], "-"))
4556                 error (SORT_FAILURE, 0, _("when reading file names from stdin, "
4557                                           "no file name of %s allowed"),
4558                        quote (files[i]));
4559               else if (files[i][0] == '\0')
4560                 {
4561                   /* Using the standard 'filename:line-number:' prefix here is
4562                      not totally appropriate, since NUL is the separator,
4563                      not NL, but it might be better than nothing.  */
4564                   unsigned long int file_number = i + 1;
4565                   error (SORT_FAILURE, 0,
4566                          _("%s:%lu: invalid zero-length file name"),
4567                          quotearg_colon (files_from), file_number);
4568                 }
4569             }
4570         }
4571       else
4572         error (SORT_FAILURE, 0, _("no input from %s"),
4573                quote (files_from));
4574     }
4575
4576   /* Inheritance of global options to individual keys. */
4577   for (key = keylist; key; key = key->next)
4578     {
4579       if (default_key_compare (key) && !key->reverse)
4580         {
4581           key->ignore = gkey.ignore;
4582           key->translate = gkey.translate;
4583           key->skipsblanks = gkey.skipsblanks;
4584           key->skipeblanks = gkey.skipeblanks;
4585           key->month = gkey.month;
4586           key->numeric = gkey.numeric;
4587           key->general_numeric = gkey.general_numeric;
4588           key->human_numeric = gkey.human_numeric;
4589           key->version = gkey.version;
4590           key->random = gkey.random;
4591           key->reverse = gkey.reverse;
4592         }
4593
4594       need_random |= key->random;
4595     }
4596
4597   if (!keylist && !default_key_compare (&gkey))
4598     {
4599       gkey_only = true;
4600       insertkey (&gkey);
4601       need_random |= gkey.random;
4602     }
4603
4604   check_ordering_compatibility ();
4605
4606   if (debug)
4607     {
4608       if (checkonly || outfile)
4609         {
4610           static char opts[] = "X --debug";
4611           opts[0] = (checkonly ? checkonly : 'o');
4612           incompatible_options (opts);
4613         }
4614
4615       /* Always output the locale in debug mode, since this
4616          is such a common source of confusion.  */
4617       if (hard_LC_COLLATE)
4618         error (0, 0, _("using %s sorting rules"),
4619                quote (setlocale (LC_COLLATE, NULL)));
4620       else
4621         error (0, 0, _("using simple byte comparison"));
4622       key_warnings (&gkey, gkey_only);
4623     }
4624
4625   reverse = gkey.reverse;
4626
4627   if (need_random)
4628     random_md5_state_init (random_source);
4629
4630   if (temp_dir_count == 0)
4631     {
4632       char const *tmp_dir = getenv ("TMPDIR");
4633       add_temp_dir (tmp_dir ? tmp_dir : DEFAULT_TMPDIR);
4634     }
4635
4636   if (nfiles == 0)
4637     {
4638       static char *minus = (char *) "-";
4639       nfiles = 1;
4640       free (files);
4641       files = &minus;
4642     }
4643
4644   /* Need to re-check that we meet the minimum requirement for memory
4645      usage with the final value for NMERGE. */
4646   if (0 < sort_size)
4647     sort_size = MAX (sort_size, MIN_SORT_SIZE);
4648
4649   if (checkonly)
4650     {
4651       if (nfiles > 1)
4652         error (SORT_FAILURE, 0, _("extra operand %s not allowed with -%c"),
4653                quote (files[1]), checkonly);
4654
4655       if (outfile)
4656         {
4657           static char opts[] = {0, 'o', 0};
4658           opts[0] = checkonly;
4659           incompatible_options (opts);
4660         }
4661
4662       /* POSIX requires that sort return 1 IFF invoked with -c or -C and the
4663          input is not properly sorted.  */
4664       exit (check (files[0], checkonly) ? EXIT_SUCCESS : SORT_OUT_OF_ORDER);
4665     }
4666
4667   /* Check all inputs are accessible, or exit immediately.  */
4668   check_inputs (files, nfiles);
4669
4670   /* Check output is writable, or exit immediately.  */
4671   check_output (outfile);
4672
4673   if (mergeonly)
4674     {
4675       struct sortfile *sortfiles = xcalloc (nfiles, sizeof *sortfiles);
4676       size_t i;
4677
4678       for (i = 0; i < nfiles; ++i)
4679         sortfiles[i].name = files[i];
4680
4681       merge (sortfiles, 0, nfiles, outfile);
4682       IF_LINT (free (sortfiles));
4683     }
4684   else
4685     {
4686       if (!nthreads)
4687         {
4688           unsigned long int np = num_processors (NPROC_CURRENT_OVERRIDABLE);
4689           nthreads = MIN (np, DEFAULT_MAX_THREADS);
4690         }
4691
4692       /* Avoid integer overflow later.  */
4693       size_t nthreads_max = SIZE_MAX / (2 * sizeof (struct merge_node));
4694       nthreads = MIN (nthreads, nthreads_max);
4695
4696       sort (files, nfiles, outfile, nthreads);
4697     }
4698
4699   if (have_read_stdin && fclose (stdin) == EOF)
4700     die (_("close failed"), "-");
4701
4702   exit (EXIT_SUCCESS);
4703 }