* NEWS: sort now uses a --compress-program option rather than
[platform/upstream/coreutils.git] / src / sort.c
1 /* sort - sort lines of text (with all kinds of options).
2    Copyright (C) 1988, 1991-2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18    Written December 1988 by Mike Haertel.
19    The author may be reached (Email) at the address mike@gnu.ai.mit.edu,
20    or (US mail) as Mike Haertel c/o Free Software Foundation.
21
22    Ørn E. Hansen added NLS support in 1997.  */
23
24 #include <config.h>
25
26 #include <getopt.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <signal.h>
30 #include "system.h"
31 #include "argmatch.h"
32 #include "error.h"
33 #include "findprog.h"
34 #include "hard-locale.h"
35 #include "hash.h"
36 #include "inttostr.h"
37 #include "md5.h"
38 #include "physmem.h"
39 #include "posixver.h"
40 #include "quote.h"
41 #include "randread.h"
42 #include "stdio--.h"
43 #include "stdlib--.h"
44 #include "strnumcmp.h"
45 #include "xmemcoll.h"
46 #include "xmemxfrm.h"
47 #include "xstrtol.h"
48
49 #if HAVE_SYS_RESOURCE_H
50 # include <sys/resource.h>
51 #endif
52 #ifndef RLIMIT_DATA
53 struct rlimit { size_t rlim_cur; };
54 # define getrlimit(Resource, Rlp) (-1)
55 #endif
56
57 /* The official name of this program (e.g., no `g' prefix).  */
58 #define PROGRAM_NAME "sort"
59
60 #define AUTHORS "Mike Haertel", "Paul Eggert"
61
62 #if HAVE_LANGINFO_CODESET
63 # include <langinfo.h>
64 #endif
65
66 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
67    present.  */
68 #ifndef SA_NOCLDSTOP
69 # define SA_NOCLDSTOP 0
70 /* No sigprocmask.  Always 'return' zero. */
71 # define sigprocmask(How, Set, Oset) (0)
72 # define sigset_t int
73 # if ! HAVE_SIGINTERRUPT
74 #  define siginterrupt(sig, flag) /* empty */
75 # endif
76 #endif
77
78 #ifndef STDC_HEADERS
79 double strtod ();
80 #endif
81
82 #define UCHAR_LIM (UCHAR_MAX + 1)
83
84 #ifndef DEFAULT_TMPDIR
85 # define DEFAULT_TMPDIR "/tmp"
86 #endif
87
88 /* Exit statuses.  */
89 enum
90   {
91     /* POSIX says to exit with status 1 if invoked with -c and the
92        input is not properly sorted.  */
93     SORT_OUT_OF_ORDER = 1,
94
95     /* POSIX says any other irregular exit must exit with a status
96        code greater than 1.  */
97     SORT_FAILURE = 2
98   };
99
100 enum
101   {
102     /* The number of times we should try to fork a compression process
103        (we retry if the fork call fails).  We don't _need_ to compress
104        temp files, this is just to reduce disk access, so this number
105        can be small.  */
106     MAX_FORK_TRIES_COMPRESS = 2,
107
108     /* The number of times we should try to fork a decompression process.
109        If we can't fork a decompression process, we can't sort, so this
110        number should be big.  */
111     MAX_FORK_TRIES_DECOMPRESS = 8
112   };
113
114 /* The representation of the decimal point in the current locale.  */
115 static int decimal_point;
116
117 /* Thousands separator; if -1, then there isn't one.  */
118 static int thousands_sep;
119
120 /* Nonzero if the corresponding locales are hard.  */
121 static bool hard_LC_COLLATE;
122 #if HAVE_NL_LANGINFO
123 static bool hard_LC_TIME;
124 #endif
125
126 #define NONZERO(x) ((x) != 0)
127
128 /* The kind of blanks for '-b' to skip in various options. */
129 enum blanktype { bl_start, bl_end, bl_both };
130
131 /* The character marking end of line. Default to \n. */
132 static char eolchar = '\n';
133
134 /* Lines are held in core as counted strings. */
135 struct line
136 {
137   char *text;                   /* Text of the line. */
138   size_t length;                /* Length including final newline. */
139   char *keybeg;                 /* Start of first key. */
140   char *keylim;                 /* Limit of first key. */
141 };
142
143 /* Input buffers. */
144 struct buffer
145 {
146   char *buf;                    /* Dynamically allocated buffer,
147                                    partitioned into 3 regions:
148                                    - input data;
149                                    - unused area;
150                                    - an array of lines, in reverse order.  */
151   size_t used;                  /* Number of bytes used for input data.  */
152   size_t nlines;                /* Number of lines in the line array.  */
153   size_t alloc;                 /* Number of bytes allocated. */
154   size_t left;                  /* Number of bytes left from previous reads. */
155   size_t line_bytes;            /* Number of bytes to reserve for each line. */
156   bool eof;                     /* An EOF has been read.  */
157 };
158
159 struct keyfield
160 {
161   size_t sword;                 /* Zero-origin 'word' to start at. */
162   size_t schar;                 /* Additional characters to skip. */
163   size_t eword;                 /* Zero-origin first word after field. */
164   size_t echar;                 /* Additional characters in field. */
165   bool const *ignore;           /* Boolean array of characters to ignore. */
166   char const *translate;        /* Translation applied to characters. */
167   bool skipsblanks;             /* Skip leading blanks when finding start.  */
168   bool skipeblanks;             /* Skip leading blanks when finding end.  */
169   bool numeric;                 /* Flag for numeric comparison.  Handle
170                                    strings of digits with optional decimal
171                                    point, but no exponential notation. */
172   bool random;                  /* Sort by random hash of key.  */
173   bool general_numeric;         /* Flag for general, numeric comparison.
174                                    Handle numbers in exponential notation. */
175   bool month;                   /* Flag for comparison by month name. */
176   bool reverse;                 /* Reverse the sense of comparison. */
177   struct keyfield *next;        /* Next keyfield to try. */
178 };
179
180 struct month
181 {
182   char const *name;
183   int val;
184 };
185
186 /* The name this program was run with. */
187 char *program_name;
188
189 /* FIXME: None of these tables work with multibyte character sets.
190    Also, there are many other bugs when handling multibyte characters.
191    One way to fix this is to rewrite `sort' to use wide characters
192    internally, but doing this with good performance is a bit
193    tricky.  */
194
195 /* Table of blanks.  */
196 static bool blanks[UCHAR_LIM];
197
198 /* Table of non-printing characters. */
199 static bool nonprinting[UCHAR_LIM];
200
201 /* Table of non-dictionary characters (not letters, digits, or blanks). */
202 static bool nondictionary[UCHAR_LIM];
203
204 /* Translation table folding lower case to upper.  */
205 static char fold_toupper[UCHAR_LIM];
206
207 #define MONTHS_PER_YEAR 12
208
209 /* Table mapping month names to integers.
210    Alphabetic order allows binary search. */
211 static struct month monthtab[] =
212 {
213   {"APR", 4},
214   {"AUG", 8},
215   {"DEC", 12},
216   {"FEB", 2},
217   {"JAN", 1},
218   {"JUL", 7},
219   {"JUN", 6},
220   {"MAR", 3},
221   {"MAY", 5},
222   {"NOV", 11},
223   {"OCT", 10},
224   {"SEP", 9}
225 };
226
227 /* During the merge phase, the number of files to merge at once. */
228 #define NMERGE 16
229
230 /* Minimum size for a merge or check buffer.  */
231 #define MIN_MERGE_BUFFER_SIZE (2 + sizeof (struct line))
232
233 /* Minimum sort size; the code might not work with smaller sizes.  */
234 #define MIN_SORT_SIZE (NMERGE * MIN_MERGE_BUFFER_SIZE)
235
236 /* The number of bytes needed for a merge or check buffer, which can
237    function relatively efficiently even if it holds only one line.  If
238    a longer line is seen, this value is increased.  */
239 static size_t merge_buffer_size = MAX (MIN_MERGE_BUFFER_SIZE, 256 * 1024);
240
241 /* The approximate maximum number of bytes of main memory to use, as
242    specified by the user.  Zero if the user has not specified a size.  */
243 static size_t sort_size;
244
245 /* The guessed size for non-regular files.  */
246 #define INPUT_FILE_SIZE_GUESS (1024 * 1024)
247
248 /* Array of directory names in which any temporary files are to be created. */
249 static char const **temp_dirs;
250
251 /* Number of temporary directory names used.  */
252 static size_t temp_dir_count;
253
254 /* Number of allocated slots in temp_dirs.  */
255 static size_t temp_dir_alloc;
256
257 /* Flag to reverse the order of all comparisons. */
258 static bool reverse;
259
260 /* Flag for stable sort.  This turns off the last ditch bytewise
261    comparison of lines, and instead leaves lines in the same order
262    they were read if all keys compare equal.  */
263 static bool stable;
264
265 /* If TAB has this value, blanks separate fields.  */
266 enum { TAB_DEFAULT = CHAR_MAX + 1 };
267
268 /* Tab character separating fields.  If TAB_DEFAULT, then fields are
269    separated by the empty string between a non-blank character and a blank
270    character. */
271 static int tab = TAB_DEFAULT;
272
273 /* Flag to remove consecutive duplicate lines from the output.
274    Only the last of a sequence of equal lines will be output. */
275 static bool unique;
276
277 /* Nonzero if any of the input files are the standard input. */
278 static bool have_read_stdin;
279
280 /* List of key field comparisons to be tried.  */
281 static struct keyfield *keylist;
282
283 /* Program used to (de)compress temp files.  Must accept -d.  */
284 static char const *compress_program;
285
286 static void sortlines_temp (struct line *, size_t, struct line *);
287
288 /* Report MESSAGE for FILE, then clean up and exit.
289    If FILE is null, it represents standard output.  */
290
291 static void die (char const *, char const *) ATTRIBUTE_NORETURN;
292 static void
293 die (char const *message, char const *file)
294 {
295   error (0, errno, "%s: %s", message, file ? file : _("standard output"));
296   exit (SORT_FAILURE);
297 }
298
299 void
300 usage (int status)
301 {
302   if (status != EXIT_SUCCESS)
303     fprintf (stderr, _("Try `%s --help' for more information.\n"),
304              program_name);
305   else
306     {
307       printf (_("\
308 Usage: %s [OPTION]... [FILE]...\n\
309 "),
310               program_name);
311       fputs (_("\
312 Write sorted concatenation of all FILE(s) to standard output.\n\
313 \n\
314 "), stdout);
315       fputs (_("\
316 Mandatory arguments to long options are mandatory for short options too.\n\
317 "), stdout);
318       fputs (_("\
319 Ordering options:\n\
320 \n\
321 "), stdout);
322       fputs (_("\
323   -b, --ignore-leading-blanks  ignore leading blanks\n\
324   -d, --dictionary-order      consider only blanks and alphanumeric characters\n\
325   -f, --ignore-case           fold lower case to upper case characters\n\
326 "), stdout);
327       fputs (_("\
328   -g, --general-numeric-sort  compare according to general numerical value\n\
329   -i, --ignore-nonprinting    consider only printable characters\n\
330   -M, --month-sort            compare (unknown) < `JAN' < ... < `DEC'\n\
331   -n, --numeric-sort          compare according to string numerical value\n\
332   -R, --random-sort           sort by random hash of keys\n\
333       --random-source=FILE    get random bytes from FILE (default /dev/urandom)\n\
334   -r, --reverse               reverse the result of comparisons\n\
335 \n\
336 "), stdout);
337       fputs (_("\
338 Other options:\n\
339 \n\
340   -c, --check, --check=diagnose-first  check for sorted input; do not sort\n\
341   -C, --check=quiet, --check=silent  like -c, but do not report first bad line\n\
342       --compress-program=PROG  compress temporaries with PROG;\n\
343                               decompress them with PROG -d\n\
344   -k, --key=POS1[,POS2]     start a key at POS1, end it at POS2 (origin 1)\n\
345   -m, --merge               merge already sorted files; do not sort\n\
346 "), stdout);
347       fputs (_("\
348   -o, --output=FILE         write result to FILE instead of standard output\n\
349   -s, --stable              stabilize sort by disabling last-resort comparison\n\
350   -S, --buffer-size=SIZE    use SIZE for main memory buffer\n\
351 "), stdout);
352       printf (_("\
353   -t, --field-separator=SEP  use SEP instead of non-blank to blank transition\n\
354   -T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or %s;\n\
355                               multiple options specify multiple directories\n\
356   -u, --unique              with -c, check for strict ordering;\n\
357                               without -c, output only the first of an equal run\n\
358 "), DEFAULT_TMPDIR);
359       fputs (_("\
360   -z, --zero-terminated     end lines with 0 byte, not newline\n\
361 "), stdout);
362       fputs (HELP_OPTION_DESCRIPTION, stdout);
363       fputs (VERSION_OPTION_DESCRIPTION, stdout);
364       fputs (_("\
365 \n\
366 POS is F[.C][OPTS], where F is the field number and C the character position\n\
367 in the field; both are origin 1.  If neither -t nor -b is in effect, characters\n\
368 in a field are counted from the beginning of the preceding whitespace.  OPTS is\n\
369 one or more single-letter ordering options, which override global ordering\n\
370 options for that key.  If no key is given, use the entire line as the key.\n\
371 \n\
372 SIZE may be followed by the following multiplicative suffixes:\n\
373 "), stdout);
374       fputs (_("\
375 % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.\n\
376 \n\
377 With no FILE, or when FILE is -, read standard input.\n\
378 \n\
379 *** WARNING ***\n\
380 The locale specified by the environment affects sort order.\n\
381 Set LC_ALL=C to get the traditional sort order that uses\n\
382 native byte values.\n\
383 "), stdout );
384       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
385     }
386
387   exit (status);
388 }
389
390 /* For long options that have no equivalent short option, use a
391    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
392 enum
393 {
394   CHECK_OPTION = CHAR_MAX + 1,
395   COMPRESS_PROGRAM_OPTION,
396   RANDOM_SOURCE_OPTION
397 };
398
399 static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z";
400
401 static struct option const long_options[] =
402 {
403   {"ignore-leading-blanks", no_argument, NULL, 'b'},
404   {"check", optional_argument, NULL, CHECK_OPTION},
405   {"compress-program", required_argument, NULL, COMPRESS_PROGRAM_OPTION},
406   {"dictionary-order", no_argument, NULL, 'd'},
407   {"ignore-case", no_argument, NULL, 'f'},
408   {"general-numeric-sort", no_argument, NULL, 'g'},
409   {"ignore-nonprinting", no_argument, NULL, 'i'},
410   {"key", required_argument, NULL, 'k'},
411   {"merge", no_argument, NULL, 'm'},
412   {"month-sort", no_argument, NULL, 'M'},
413   {"numeric-sort", no_argument, NULL, 'n'},
414   {"random-sort", no_argument, NULL, 'R'},
415   {"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
416   {"output", required_argument, NULL, 'o'},
417   {"reverse", no_argument, NULL, 'r'},
418   {"stable", no_argument, NULL, 's'},
419   {"buffer-size", required_argument, NULL, 'S'},
420   {"field-separator", required_argument, NULL, 't'},
421   {"temporary-directory", required_argument, NULL, 'T'},
422   {"unique", no_argument, NULL, 'u'},
423   {"zero-terminated", no_argument, NULL, 'z'},
424   {GETOPT_HELP_OPTION_DECL},
425   {GETOPT_VERSION_OPTION_DECL},
426   {NULL, 0, NULL, 0},
427 };
428
429 static char const *const check_args[] =
430 {
431   "quiet", "silent", "diagnose-first", NULL
432 };
433 static char const check_types[] =
434 {
435   'C', 'C', 'c'
436 };
437 ARGMATCH_VERIFY (check_args, check_types);
438
439 /* The set of signals that are caught.  */
440 static sigset_t caught_signals;
441
442 /* Critical section status.  */
443 struct cs_status
444 {
445   bool valid;
446   sigset_t sigs;
447 };
448
449 /* Enter a critical section.  */
450 static struct cs_status
451 cs_enter (void)
452 {
453   struct cs_status status;
454   status.valid = (sigprocmask (SIG_BLOCK, &caught_signals, &status.sigs) == 0);
455   return status;
456 }
457
458 /* Leave a critical section.  */
459 static void
460 cs_leave (struct cs_status status)
461 {
462   if (status.valid)
463     {
464       /* Ignore failure when restoring the signal mask. */
465       sigprocmask (SIG_SETMASK, &status.sigs, NULL);
466     }
467 }
468
469 /* The list of temporary files. */
470 struct tempnode
471 {
472   struct tempnode *volatile next;
473   pid_t pid;     /* If compressed, the pid of compressor, else zero */
474   char name[1];  /* Actual size is 1 + file name length.  */
475 };
476 static struct tempnode *volatile temphead;
477 static struct tempnode *volatile *temptail = &temphead;
478
479 struct sortfile
480 {
481   char const *name;
482   pid_t pid;     /* If compressed, the pid of compressor, else zero */
483 };
484
485 /* A table where we store compression process states.  We clean up all
486    processes in a timely manner so as not to exhaust system resources,
487    so we store the info on whether the process is still running, or has
488    been reaped here.  */
489 static Hash_table *proctab;
490
491 enum { INIT_PROCTAB_SIZE = 47 };
492
493 enum procstate { ALIVE, ZOMBIE };
494
495 /* A proctab entry.  The COUNT field is there in case we fork a new
496    compression process that has the same PID as an old zombie process
497    that is still in the table (because the process to decompress the
498    temp file it was associated with hasn't started yet).  */
499 struct procnode
500 {
501   pid_t pid;
502   enum procstate state;
503   size_t count;
504 };
505
506 static size_t
507 proctab_hasher (const void *entry, size_t tabsize)
508 {
509   const struct procnode *node = entry;
510   return node->pid % tabsize;
511 }
512
513 static bool
514 proctab_comparator (const void *e1, const void *e2)
515 {
516   const struct procnode *n1 = e1, *n2 = e2;
517   return n1->pid == n2->pid;
518 }
519
520 /* The total number of forked processes (compressors and decompressors)
521    that have not been reaped yet. */
522 static size_t nprocs;
523
524 /* The number of child processes we'll allow before we try to reap some. */
525 enum { MAX_PROCS_BEFORE_REAP = 2 };
526
527 /* If 0 < PID, wait for the child process with that PID to exit.
528    If PID is -1, clean up a random child process which has finished and
529    return the process ID of that child.  If PID is -1 and no processes
530    have quit yet, return 0 without waiting.  */
531
532 static pid_t
533 reap (pid_t pid)
534 {
535   int status;
536   pid_t cpid = waitpid (pid, &status, pid < 0 ? WNOHANG : 0);
537
538   if (cpid < 0)
539     error (SORT_FAILURE, errno, _("waiting for %s [-d]"),
540            compress_program);
541   else if (0 < cpid)
542     {
543       if (! WIFEXITED (status) || WEXITSTATUS (status))
544         error (SORT_FAILURE, 0, _("%s [-d] terminated abnormally"),
545                compress_program);
546       --nprocs;
547     }
548
549   return cpid;
550 }
551
552 /* Add the PID of a running compression process to proctab, or update
553    the entry COUNT and STATE fields if it's already there.  This also
554    creates the table for us the first time it's called.  */
555
556 static void
557 register_proc (pid_t pid)
558 {
559   struct procnode test, *node;
560
561   if (! proctab)
562     {
563       proctab = hash_initialize (INIT_PROCTAB_SIZE, NULL,
564                                  proctab_hasher,
565                                  proctab_comparator,
566                                  free);
567       if (! proctab)
568         xalloc_die ();
569     }
570
571   test.pid = pid;
572   node = hash_lookup (proctab, &test);
573   if (node)
574     {
575       node->state = ALIVE;
576       ++node->count;
577     }
578   else
579     {
580       node = xmalloc (sizeof *node);
581       node->pid = pid;
582       node->state = ALIVE;
583       node->count = 1;
584       hash_insert (proctab, node);
585     }
586 }
587
588 /* This is called when we reap a random process.  We don't know
589    whether we have reaped a compression process or a decompression
590    process until we look in the table.  If there's an ALIVE entry for
591    it, then we have reaped a compression process, so change the state
592    to ZOMBIE.  Otherwise, it's a decompression processes, so ignore it.  */
593
594 static void
595 update_proc (pid_t pid)
596 {
597   struct procnode test, *node;
598
599   test.pid = pid;
600   node = hash_lookup (proctab, &test);
601   if (node)
602     node->state = ZOMBIE;
603 }
604
605 /* This is for when we need to wait for a compression process to exit.
606    If it has a ZOMBIE entry in the table then it's already dead and has
607    been reaped.  Note that if there's an ALIVE entry for it, it still may
608    already have died and been reaped if a second process was created with
609    the same PID.  This is probably exceedingly rare, but to be on the safe
610    side we will have to wait for any compression process with this PID.  */
611
612 static void
613 wait_proc (pid_t pid)
614 {
615   struct procnode test, *node;
616
617   test.pid = pid;
618   node = hash_lookup (proctab, &test);
619   if (node->state == ALIVE)
620     reap (pid);
621
622   node->state = ZOMBIE;
623   if (! --node->count)
624     {
625       hash_delete (proctab, node);
626       free (node);
627     }
628 }
629
630 /* Keep reaping finished children as long as there are more to reap.
631    This doesn't block waiting for any of them, it only reaps those
632    that are already dead.  */
633
634 static void
635 reap_some (void)
636 {
637   pid_t pid;
638
639   while (0 < nprocs && (pid = reap (-1)))
640     update_proc (pid);
641 }
642
643 /* Clean up any remaining temporary files.  */
644
645 static void
646 cleanup (void)
647 {
648   struct tempnode const *node;
649
650   for (node = temphead; node; node = node->next)
651     unlink (node->name);
652   temphead = NULL;
653 }
654
655 /* Cleanup actions to take when exiting.  */
656
657 static void
658 exit_cleanup (void)
659 {
660   if (temphead)
661     {
662       /* Clean up any remaining temporary files in a critical section so
663          that a signal handler does not try to clean them too.  */
664       struct cs_status cs = cs_enter ();
665       cleanup ();
666       cs_leave (cs);
667     }
668
669   close_stdout ();
670 }
671
672 /* Create a new temporary file, returning its newly allocated tempnode.
673    Store into *PFD the file descriptor open for writing.  */
674
675 static struct tempnode *
676 create_temp_file (int *pfd)
677 {
678   static char const slashbase[] = "/sortXXXXXX";
679   static size_t temp_dir_index;
680   int fd;
681   int saved_errno;
682   char const *temp_dir = temp_dirs[temp_dir_index];
683   size_t len = strlen (temp_dir);
684   struct tempnode *node =
685     xmalloc (offsetof (struct tempnode, name) + len + sizeof slashbase);
686   char *file = node->name;
687   struct cs_status cs;
688
689   memcpy (file, temp_dir, len);
690   memcpy (file + len, slashbase, sizeof slashbase);
691   node->next = NULL;
692   node->pid = 0;
693   if (++temp_dir_index == temp_dir_count)
694     temp_dir_index = 0;
695
696   /* Create the temporary file in a critical section, to avoid races.  */
697   cs = cs_enter ();
698   fd = mkstemp (file);
699   if (0 <= fd)
700     {
701       *temptail = node;
702       temptail = &node->next;
703     }
704   saved_errno = errno;
705   cs_leave (cs);
706   errno = saved_errno;
707
708   if (fd < 0)
709     die (_("cannot create temporary file"), file);
710
711   *pfd = fd;
712   return node;
713 }
714
715 /* Return a stream for FILE, opened with mode HOW.  A null FILE means
716    standard output; HOW should be "w".  When opening for input, "-"
717    means standard input.  To avoid confusion, do not return file
718    descriptors STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO when
719    opening an ordinary FILE.  */
720
721 static FILE *
722 xfopen (const char *file, const char *how)
723 {
724   FILE *fp;
725
726   if (!file)
727     fp = stdout;
728   else if (STREQ (file, "-") && *how == 'r')
729     {
730       have_read_stdin = true;
731       fp = stdin;
732     }
733   else
734     {
735       fp = fopen (file, how);
736       if (! fp)
737         die (_("open failed"), file);
738     }
739
740   return fp;
741 }
742
743 /* Close FP, whose name is FILE, and report any errors.  */
744
745 static void
746 xfclose (FILE *fp, char const *file)
747 {
748   switch (fileno (fp))
749     {
750     case STDIN_FILENO:
751       /* Allow reading stdin from tty more than once.  */
752       if (feof (fp))
753         clearerr (fp);
754       break;
755
756     case STDOUT_FILENO:
757       /* Don't close stdout just yet.  close_stdout does that.  */
758       if (fflush (fp) != 0)
759         die (_("fflush failed"), file);
760       break;
761
762     default:
763       if (fclose (fp) != 0)
764         die (_("close failed"), file);
765       break;
766     }
767 }
768
769 static void
770 dup2_or_die (int oldfd, int newfd)
771 {
772   if (dup2 (oldfd, newfd) < 0)
773     error (SORT_FAILURE, errno, _("dup2 failed"));
774 }
775
776 /* Fork a child process for piping to and do common cleanup.  The
777    TRIES parameter tells us how many times to try to fork before
778    giving up.  Return the PID of the child or -1 if fork failed.  */
779
780 static pid_t
781 pipe_fork (int pipefds[2], size_t tries)
782 {
783 #if HAVE_WORKING_FORK
784   struct tempnode *saved_temphead;
785   int saved_errno;
786   unsigned int wait_retry = 1;
787   pid_t pid IF_LINT (= -1);
788   struct cs_status cs;
789
790   if (pipe (pipefds) < 0)
791     return -1;
792
793   while (tries--)
794     {
795       /* This is so the child process won't delete our temp files
796          if it receives a signal before exec-ing.  */
797       cs = cs_enter ();
798       saved_temphead = temphead;
799       temphead = NULL;
800
801       pid = fork ();
802       saved_errno = errno;
803       if (pid)
804         temphead = saved_temphead;
805
806       cs_leave (cs);
807       errno = saved_errno;
808
809       if (0 <= pid || errno != EAGAIN)
810         break;
811       else
812         {
813           sleep (wait_retry);
814           wait_retry *= 2;
815           reap_some ();
816         }
817     }
818
819   if (pid < 0)
820     {
821       close (pipefds[0]);
822       close (pipefds[1]);
823     }
824   else if (pid == 0)
825     {
826       close (STDIN_FILENO);
827       close (STDOUT_FILENO);
828     }
829   else
830     ++nprocs;
831
832   return pid;
833
834 #else  /* ! HAVE_WORKING_FORK */
835   return -1;
836 #endif
837 }
838
839 /* Create a temporary file and start a compression program to filter output
840    to that file.  Set *PFP to the file handle and if *PPID is non-NULL,
841    set it to the PID of the newly-created process.  */
842
843 static char *
844 create_temp (FILE **pfp, pid_t *ppid)
845 {
846   int tempfd;
847   struct tempnode *node = create_temp_file (&tempfd);
848   char *name = node->name;
849
850   if (! compress_program)
851     {
852       static char const default_compress_program[] = "gzip";
853       char const *prog = find_in_path (default_compress_program);
854       compress_program = (prog == default_compress_program ? "" : prog);
855     }
856
857   if (*compress_program)
858     {
859       int pipefds[2];
860
861       node->pid = pipe_fork (pipefds, MAX_FORK_TRIES_COMPRESS);
862       if (0 < node->pid)
863         {
864           close (tempfd);
865           close (pipefds[0]);
866           tempfd = pipefds[1];
867
868           register_proc (node->pid);
869         }
870       else if (node->pid == 0)
871         {
872           close (pipefds[1]);
873           dup2_or_die (tempfd, STDOUT_FILENO);
874           close (tempfd);
875           dup2_or_die (pipefds[0], STDIN_FILENO);
876           close (pipefds[0]);
877
878           if (execlp (compress_program, compress_program,
879                       (char *) NULL) < 0)
880             error (SORT_FAILURE, errno, _("couldn't execute %s"),
881                    compress_program);
882         }
883       else
884         node->pid = 0;
885     }
886
887   *pfp = fdopen (tempfd, "w");
888   if (! *pfp)
889     die (_("couldn't create temporary file"), name);
890
891   if (ppid)
892     *ppid = node->pid;
893
894   return name;
895 }
896
897 /* Open a compressed temp file and start a decompression process through
898    which to filter the input.  PID must be the valid processes ID of the
899    process used to compress the file.  */
900
901 static FILE *
902 open_temp (const char *name, pid_t pid)
903 {
904   int tempfd, pipefds[2];
905   pid_t child_pid;
906   FILE *fp;
907
908   wait_proc (pid);
909
910   tempfd = open (name, O_RDONLY);
911   if (tempfd < 0)
912     die (_("couldn't open temporary file"), name);
913
914   child_pid = pipe_fork (pipefds, MAX_FORK_TRIES_DECOMPRESS);
915   if (0 < child_pid)
916     {
917       close (tempfd);
918       close (pipefds[1]);
919     }
920   else if (child_pid == 0)
921     {
922       close (pipefds[0]);
923       dup2_or_die (tempfd, STDIN_FILENO);
924       close (tempfd);
925       dup2_or_die (pipefds[1], STDOUT_FILENO);
926       close (pipefds[1]);
927
928       if (execlp (compress_program, compress_program,
929                   "-d", (char *) NULL) < 0)
930         error (SORT_FAILURE, errno, _("couldn't execute %s -d"),
931                compress_program);
932     }
933   else
934     error (SORT_FAILURE, errno, _("couldn't create process for %s -d"),
935            compress_program);
936
937   fp = fdopen (pipefds[0], "r");
938   if (! fp)
939     die (_("couldn't create temporary file"), name);
940
941   return fp;
942 }
943
944 static void
945 write_bytes (const char *buf, size_t n_bytes, FILE *fp, const char *output_file)
946 {
947   if (fwrite (buf, 1, n_bytes, fp) != n_bytes)
948     die (_("write failed"), output_file);
949 }
950
951 /* Append DIR to the array of temporary directory names.  */
952 static void
953 add_temp_dir (char const *dir)
954 {
955   if (temp_dir_count == temp_dir_alloc)
956     temp_dirs = X2NREALLOC (temp_dirs, &temp_dir_alloc);
957
958   temp_dirs[temp_dir_count++] = dir;
959 }
960
961 /* Remove NAME from the list of temporary files.  */
962
963 static void
964 zaptemp (const char *name)
965 {
966   struct tempnode *volatile *pnode;
967   struct tempnode *node;
968   struct tempnode *next;
969   int unlink_status;
970   int unlink_errno = 0;
971   struct cs_status cs;
972
973   for (pnode = &temphead; (node = *pnode)->name != name; pnode = &node->next)
974     continue;
975
976   /* Unlink the temporary file in a critical section to avoid races.  */
977   next = node->next;
978   cs = cs_enter ();
979   unlink_status = unlink (name);
980   unlink_errno = errno;
981   *pnode = next;
982   cs_leave (cs);
983
984   if (unlink_status != 0)
985     error (0, unlink_errno, _("warning: cannot remove: %s"), name);
986   if (! next)
987     temptail = pnode;
988   free (node);
989 }
990
991 #if HAVE_NL_LANGINFO
992
993 static int
994 struct_month_cmp (const void *m1, const void *m2)
995 {
996   struct month const *month1 = m1;
997   struct month const *month2 = m2;
998   return strcmp (month1->name, month2->name);
999 }
1000
1001 #endif
1002
1003 /* Initialize the character class tables. */
1004
1005 static void
1006 inittables (void)
1007 {
1008   size_t i;
1009
1010   for (i = 0; i < UCHAR_LIM; ++i)
1011     {
1012       blanks[i] = !! isblank (i);
1013       nonprinting[i] = ! isprint (i);
1014       nondictionary[i] = ! isalnum (i) && ! isblank (i);
1015       fold_toupper[i] = toupper (i);
1016     }
1017
1018 #if HAVE_NL_LANGINFO
1019   /* If we're not in the "C" locale, read different names for months.  */
1020   if (hard_LC_TIME)
1021     {
1022       for (i = 0; i < MONTHS_PER_YEAR; i++)
1023         {
1024           char const *s;
1025           size_t s_len;
1026           size_t j;
1027           char *name;
1028
1029           s = (char *) nl_langinfo (ABMON_1 + i);
1030           s_len = strlen (s);
1031           monthtab[i].name = name = xmalloc (s_len + 1);
1032           monthtab[i].val = i + 1;
1033
1034           for (j = 0; j < s_len; j++)
1035             name[j] = fold_toupper[to_uchar (s[j])];
1036           name[j] = '\0';
1037         }
1038       qsort ((void *) monthtab, MONTHS_PER_YEAR,
1039              sizeof *monthtab, struct_month_cmp);
1040     }
1041 #endif
1042 }
1043
1044 /* Specify the amount of main memory to use when sorting.  */
1045 static void
1046 specify_sort_size (char const *s)
1047 {
1048   uintmax_t n;
1049   char *suffix;
1050   enum strtol_error e = xstrtoumax (s, &suffix, 10, &n, "EgGkKmMPtTYZ");
1051
1052   /* The default unit is KiB.  */
1053   if (e == LONGINT_OK && ISDIGIT (suffix[-1]))
1054     {
1055       if (n <= UINTMAX_MAX / 1024)
1056         n *= 1024;
1057       else
1058         e = LONGINT_OVERFLOW;
1059     }
1060
1061   /* A 'b' suffix means bytes; a '%' suffix means percent of memory.  */
1062   if (e == LONGINT_INVALID_SUFFIX_CHAR && ISDIGIT (suffix[-1]) && ! suffix[1])
1063     switch (suffix[0])
1064       {
1065       case 'b':
1066         e = LONGINT_OK;
1067         break;
1068
1069       case '%':
1070         {
1071           double mem = physmem_total () * n / 100;
1072
1073           /* Use "<", not "<=", to avoid problems with rounding.  */
1074           if (mem < UINTMAX_MAX)
1075             {
1076               n = mem;
1077               e = LONGINT_OK;
1078             }
1079           else
1080             e = LONGINT_OVERFLOW;
1081         }
1082         break;
1083       }
1084
1085   if (e == LONGINT_OK)
1086     {
1087       /* If multiple sort sizes are specified, take the maximum, so
1088          that option order does not matter.  */
1089       if (n < sort_size)
1090         return;
1091
1092       sort_size = n;
1093       if (sort_size == n)
1094         {
1095           sort_size = MAX (sort_size, MIN_SORT_SIZE);
1096           return;
1097         }
1098
1099       e = LONGINT_OVERFLOW;
1100     }
1101
1102   STRTOL_FATAL_ERROR (s, _("sort size"), e);
1103 }
1104
1105 /* Return the default sort size.  */
1106 static size_t
1107 default_sort_size (void)
1108 {
1109   /* Let MEM be available memory or 1/8 of total memory, whichever
1110      is greater.  */
1111   double avail = physmem_available ();
1112   double total = physmem_total ();
1113   double mem = MAX (avail, total / 8);
1114   struct rlimit rlimit;
1115
1116   /* Let SIZE be MEM, but no more than the maximum object size or
1117      system resource limits.  Avoid the MIN macro here, as it is not
1118      quite right when only one argument is floating point.  Don't
1119      bother to check for values like RLIM_INFINITY since in practice
1120      they are not much less than SIZE_MAX.  */
1121   size_t size = SIZE_MAX;
1122   if (mem < size)
1123     size = mem;
1124   if (getrlimit (RLIMIT_DATA, &rlimit) == 0 && rlimit.rlim_cur < size)
1125     size = rlimit.rlim_cur;
1126 #ifdef RLIMIT_AS
1127   if (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur < size)
1128     size = rlimit.rlim_cur;
1129 #endif
1130
1131   /* Leave a large safety margin for the above limits, as failure can
1132      occur when they are exceeded.  */
1133   size /= 2;
1134
1135 #ifdef RLIMIT_RSS
1136   /* Leave a 1/16 margin for RSS to leave room for code, stack, etc.
1137      Exceeding RSS is not fatal, but can be quite slow.  */
1138   if (getrlimit (RLIMIT_RSS, &rlimit) == 0 && rlimit.rlim_cur / 16 * 15 < size)
1139     size = rlimit.rlim_cur / 16 * 15;
1140 #endif
1141
1142   /* Use no less than the minimum.  */
1143   return MAX (size, MIN_SORT_SIZE);
1144 }
1145
1146 /* Return the sort buffer size to use with the input files identified
1147    by FPS and FILES, which are alternate names of the same files.
1148    NFILES gives the number of input files; NFPS may be less.  Assume
1149    that each input line requires LINE_BYTES extra bytes' worth of line
1150    information.  Do not exceed the size bound specified by the user
1151    (or a default size bound, if the user does not specify one).  */
1152
1153 static size_t
1154 sort_buffer_size (FILE *const *fps, size_t nfps,
1155                   char *const *files, size_t nfiles,
1156                   size_t line_bytes)
1157 {
1158   /* A bound on the input size.  If zero, the bound hasn't been
1159      determined yet.  */
1160   static size_t size_bound;
1161
1162   /* In the worst case, each input byte is a newline.  */
1163   size_t worst_case_per_input_byte = line_bytes + 1;
1164
1165   /* Keep enough room for one extra input line and an extra byte.
1166      This extra room might be needed when preparing to read EOF.  */
1167   size_t size = worst_case_per_input_byte + 1;
1168
1169   size_t i;
1170
1171   for (i = 0; i < nfiles; i++)
1172     {
1173       struct stat st;
1174       off_t file_size;
1175       size_t worst_case;
1176
1177       if ((i < nfps ? fstat (fileno (fps[i]), &st)
1178            : STREQ (files[i], "-") ? fstat (STDIN_FILENO, &st)
1179            : stat (files[i], &st))
1180           != 0)
1181         die (_("stat failed"), files[i]);
1182
1183       if (S_ISREG (st.st_mode))
1184         file_size = st.st_size;
1185       else
1186         {
1187           /* The file has unknown size.  If the user specified a sort
1188              buffer size, use that; otherwise, guess the size.  */
1189           if (sort_size)
1190             return sort_size;
1191           file_size = INPUT_FILE_SIZE_GUESS;
1192         }
1193
1194       if (! size_bound)
1195         {
1196           size_bound = sort_size;
1197           if (! size_bound)
1198             size_bound = default_sort_size ();
1199         }
1200
1201       /* Add the amount of memory needed to represent the worst case
1202          where the input consists entirely of newlines followed by a
1203          single non-newline.  Check for overflow.  */
1204       worst_case = file_size * worst_case_per_input_byte + 1;
1205       if (file_size != worst_case / worst_case_per_input_byte
1206           || size_bound - size <= worst_case)
1207         return size_bound;
1208       size += worst_case;
1209     }
1210
1211   return size;
1212 }
1213
1214 /* Initialize BUF.  Reserve LINE_BYTES bytes for each line; LINE_BYTES
1215    must be at least sizeof (struct line).  Allocate ALLOC bytes
1216    initially.  */
1217
1218 static void
1219 initbuf (struct buffer *buf, size_t line_bytes, size_t alloc)
1220 {
1221   /* Ensure that the line array is properly aligned.  If the desired
1222      size cannot be allocated, repeatedly halve it until allocation
1223      succeeds.  The smaller allocation may hurt overall performance,
1224      but that's better than failing.  */
1225   for (;;)
1226     {
1227       alloc += sizeof (struct line) - alloc % sizeof (struct line);
1228       buf->buf = malloc (alloc);
1229       if (buf->buf)
1230         break;
1231       alloc /= 2;
1232       if (alloc <= line_bytes + 1)
1233         xalloc_die ();
1234     }
1235
1236   buf->line_bytes = line_bytes;
1237   buf->alloc = alloc;
1238   buf->used = buf->left = buf->nlines = 0;
1239   buf->eof = false;
1240 }
1241
1242 /* Return one past the limit of the line array.  */
1243
1244 static inline struct line *
1245 buffer_linelim (struct buffer const *buf)
1246 {
1247   return (struct line *) (buf->buf + buf->alloc);
1248 }
1249
1250 /* Return a pointer to the first character of the field specified
1251    by KEY in LINE. */
1252
1253 static char *
1254 begfield (const struct line *line, const struct keyfield *key)
1255 {
1256   char *ptr = line->text, *lim = ptr + line->length - 1;
1257   size_t sword = key->sword;
1258   size_t schar = key->schar;
1259   size_t remaining_bytes;
1260
1261   /* The leading field separator itself is included in a field when -t
1262      is absent.  */
1263
1264   if (tab != TAB_DEFAULT)
1265     while (ptr < lim && sword--)
1266       {
1267         while (ptr < lim && *ptr != tab)
1268           ++ptr;
1269         if (ptr < lim)
1270           ++ptr;
1271       }
1272   else
1273     while (ptr < lim && sword--)
1274       {
1275         while (ptr < lim && blanks[to_uchar (*ptr)])
1276           ++ptr;
1277         while (ptr < lim && !blanks[to_uchar (*ptr)])
1278           ++ptr;
1279       }
1280
1281   if (key->skipsblanks)
1282     while (ptr < lim && blanks[to_uchar (*ptr)])
1283       ++ptr;
1284
1285   /* Advance PTR by SCHAR (if possible), but no further than LIM.  */
1286   remaining_bytes = lim - ptr;
1287   if (schar < remaining_bytes)
1288     ptr += schar;
1289   else
1290     ptr = lim;
1291
1292   return ptr;
1293 }
1294
1295 /* Return the limit of (a pointer to the first character after) the field
1296    in LINE specified by KEY. */
1297
1298 static char *
1299 limfield (const struct line *line, const struct keyfield *key)
1300 {
1301   char *ptr = line->text, *lim = ptr + line->length - 1;
1302   size_t eword = key->eword, echar = key->echar;
1303   size_t remaining_bytes;
1304
1305   /* Move PTR past EWORD fields or to one past the last byte on LINE,
1306      whichever comes first.  If there are more than EWORD fields, leave
1307      PTR pointing at the beginning of the field having zero-based index,
1308      EWORD.  If a delimiter character was specified (via -t), then that
1309      `beginning' is the first character following the delimiting TAB.
1310      Otherwise, leave PTR pointing at the first `blank' character after
1311      the preceding field.  */
1312   if (tab != TAB_DEFAULT)
1313     while (ptr < lim && eword--)
1314       {
1315         while (ptr < lim && *ptr != tab)
1316           ++ptr;
1317         if (ptr < lim && (eword | echar))
1318           ++ptr;
1319       }
1320   else
1321     while (ptr < lim && eword--)
1322       {
1323         while (ptr < lim && blanks[to_uchar (*ptr)])
1324           ++ptr;
1325         while (ptr < lim && !blanks[to_uchar (*ptr)])
1326           ++ptr;
1327       }
1328
1329 #ifdef POSIX_UNSPECIFIED
1330   /* The following block of code makes GNU sort incompatible with
1331      standard Unix sort, so it's ifdef'd out for now.
1332      The POSIX spec isn't clear on how to interpret this.
1333      FIXME: request clarification.
1334
1335      From: kwzh@gnu.ai.mit.edu (Karl Heuer)
1336      Date: Thu, 30 May 96 12:20:41 -0400
1337      [Translated to POSIX 1003.1-2001 terminology by Paul Eggert.]
1338
1339      [...]I believe I've found another bug in `sort'.
1340
1341      $ cat /tmp/sort.in
1342      a b c 2 d
1343      pq rs 1 t
1344      $ textutils-1.15/src/sort -k1.7,1.7 </tmp/sort.in
1345      a b c 2 d
1346      pq rs 1 t
1347      $ /bin/sort -k1.7,1.7 </tmp/sort.in
1348      pq rs 1 t
1349      a b c 2 d
1350
1351      Unix sort produced the answer I expected: sort on the single character
1352      in column 7.  GNU sort produced different results, because it disagrees
1353      on the interpretation of the key-end spec "M.N".  Unix sort reads this
1354      as "skip M-1 fields, then N-1 characters"; but GNU sort wants it to mean
1355      "skip M-1 fields, then either N-1 characters or the rest of the current
1356      field, whichever comes first".  This extra clause applies only to
1357      key-ends, not key-starts.
1358      */
1359
1360   /* Make LIM point to the end of (one byte past) the current field.  */
1361   if (tab != TAB_DEFAULT)
1362     {
1363       char *newlim;
1364       newlim = memchr (ptr, tab, lim - ptr);
1365       if (newlim)
1366         lim = newlim;
1367     }
1368   else
1369     {
1370       char *newlim;
1371       newlim = ptr;
1372       while (newlim < lim && blanks[to_uchar (*newlim)])
1373         ++newlim;
1374       while (newlim < lim && !blanks[to_uchar (*newlim)])
1375         ++newlim;
1376       lim = newlim;
1377     }
1378 #endif
1379
1380   /* If we're ignoring leading blanks when computing the End
1381      of the field, don't start counting bytes until after skipping
1382      past any leading blanks. */
1383   if (key->skipeblanks)
1384     while (ptr < lim && blanks[to_uchar (*ptr)])
1385       ++ptr;
1386
1387   /* Advance PTR by ECHAR (if possible), but no further than LIM.  */
1388   remaining_bytes = lim - ptr;
1389   if (echar < remaining_bytes)
1390     ptr += echar;
1391   else
1392     ptr = lim;
1393
1394   return ptr;
1395 }
1396
1397 /* Fill BUF reading from FP, moving buf->left bytes from the end
1398    of buf->buf to the beginning first.  If EOF is reached and the
1399    file wasn't terminated by a newline, supply one.  Set up BUF's line
1400    table too.  FILE is the name of the file corresponding to FP.
1401    Return true if some input was read.  */
1402
1403 static bool
1404 fillbuf (struct buffer *buf, FILE *fp, char const *file)
1405 {
1406   struct keyfield const *key = keylist;
1407   char eol = eolchar;
1408   size_t line_bytes = buf->line_bytes;
1409   size_t mergesize = merge_buffer_size - MIN_MERGE_BUFFER_SIZE;
1410
1411   if (buf->eof)
1412     return false;
1413
1414   if (buf->used != buf->left)
1415     {
1416       memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
1417       buf->used = buf->left;
1418       buf->nlines = 0;
1419     }
1420
1421   for (;;)
1422     {
1423       char *ptr = buf->buf + buf->used;
1424       struct line *linelim = buffer_linelim (buf);
1425       struct line *line = linelim - buf->nlines;
1426       size_t avail = (char *) linelim - buf->nlines * line_bytes - ptr;
1427       char *line_start = buf->nlines ? line->text + line->length : buf->buf;
1428
1429       while (line_bytes + 1 < avail)
1430         {
1431           /* Read as many bytes as possible, but do not read so many
1432              bytes that there might not be enough room for the
1433              corresponding line array.  The worst case is when the
1434              rest of the input file consists entirely of newlines,
1435              except that the last byte is not a newline.  */
1436           size_t readsize = (avail - 1) / (line_bytes + 1);
1437           size_t bytes_read = fread (ptr, 1, readsize, fp);
1438           char *ptrlim = ptr + bytes_read;
1439           char *p;
1440           avail -= bytes_read;
1441
1442           if (bytes_read != readsize)
1443             {
1444               if (ferror (fp))
1445                 die (_("read failed"), file);
1446               if (feof (fp))
1447                 {
1448                   buf->eof = true;
1449                   if (buf->buf == ptrlim)
1450                     return false;
1451                   if (ptrlim[-1] != eol)
1452                     *ptrlim++ = eol;
1453                 }
1454             }
1455
1456           /* Find and record each line in the just-read input.  */
1457           while ((p = memchr (ptr, eol, ptrlim - ptr)))
1458             {
1459               ptr = p + 1;
1460               line--;
1461               line->text = line_start;
1462               line->length = ptr - line_start;
1463               mergesize = MAX (mergesize, line->length);
1464               avail -= line_bytes;
1465
1466               if (key)
1467                 {
1468                   /* Precompute the position of the first key for
1469                      efficiency.  */
1470                   line->keylim = (key->eword == SIZE_MAX
1471                                   ? p
1472                                   : limfield (line, key));
1473
1474                   if (key->sword != SIZE_MAX)
1475                     line->keybeg = begfield (line, key);
1476                   else
1477                     {
1478                       if (key->skipsblanks)
1479                         while (blanks[to_uchar (*line_start)])
1480                           line_start++;
1481                       line->keybeg = line_start;
1482                     }
1483                 }
1484
1485               line_start = ptr;
1486             }
1487
1488           ptr = ptrlim;
1489           if (buf->eof)
1490             break;
1491         }
1492
1493       buf->used = ptr - buf->buf;
1494       buf->nlines = buffer_linelim (buf) - line;
1495       if (buf->nlines != 0)
1496         {
1497           buf->left = ptr - line_start;
1498           merge_buffer_size = mergesize + MIN_MERGE_BUFFER_SIZE;
1499           return true;
1500         }
1501
1502       /* The current input line is too long to fit in the buffer.
1503          Double the buffer size and try again.  */
1504       buf->buf = X2REALLOC (buf->buf, &buf->alloc);
1505     }
1506 }
1507
1508 /* Compare strings A and B as numbers without explicitly converting them to
1509    machine numbers.  Comparatively slow for short strings, but asymptotically
1510    hideously fast. */
1511
1512 static int
1513 numcompare (const char *a, const char *b)
1514 {
1515   while (blanks[to_uchar (*a)])
1516     a++;
1517   while (blanks[to_uchar (*b)])
1518     b++;
1519
1520   return strnumcmp (a, b, decimal_point, thousands_sep);
1521 }
1522
1523 static int
1524 general_numcompare (const char *sa, const char *sb)
1525 {
1526   /* FIXME: add option to warn about failed conversions.  */
1527   /* FIXME: maybe add option to try expensive FP conversion
1528      only if A and B can't be compared more cheaply/accurately.  */
1529
1530   char *ea;
1531   char *eb;
1532   double a = strtod (sa, &ea);
1533   double b = strtod (sb, &eb);
1534
1535   /* Put conversion errors at the start of the collating sequence.  */
1536   if (sa == ea)
1537     return sb == eb ? 0 : -1;
1538   if (sb == eb)
1539     return 1;
1540
1541   /* Sort numbers in the usual way, where -0 == +0.  Put NaNs after
1542      conversion errors but before numbers; sort them by internal
1543      bit-pattern, for lack of a more portable alternative.  */
1544   return (a < b ? -1
1545           : a > b ? 1
1546           : a == b ? 0
1547           : b == b ? -1
1548           : a == a ? 1
1549           : memcmp ((char *) &a, (char *) &b, sizeof a));
1550 }
1551
1552 /* Return an integer in 1..12 of the month name MONTH with length LEN.
1553    Return 0 if the name in S is not recognized.  */
1554
1555 static int
1556 getmonth (char const *month, size_t len)
1557 {
1558   size_t lo = 0;
1559   size_t hi = MONTHS_PER_YEAR;
1560   char const *monthlim = month + len;
1561
1562   for (;;)
1563     {
1564       if (month == monthlim)
1565         return 0;
1566       if (!blanks[to_uchar (*month)])
1567         break;
1568       ++month;
1569     }
1570
1571   do
1572     {
1573       size_t ix = (lo + hi) / 2;
1574       char const *m = month;
1575       char const *n = monthtab[ix].name;
1576
1577       for (;; m++, n++)
1578         {
1579           if (!*n)
1580             return monthtab[ix].val;
1581           if (m == monthlim || fold_toupper[to_uchar (*m)] < to_uchar (*n))
1582             {
1583               hi = ix;
1584               break;
1585             }
1586           else if (fold_toupper[to_uchar (*m)] > to_uchar (*n))
1587             {
1588               lo = ix + 1;
1589               break;
1590             }
1591         }
1592     }
1593   while (lo < hi);
1594
1595   return 0;
1596 }
1597
1598 /* A source of random data.  */
1599 static struct randread_source *randread_source;
1600
1601 /* Return the Ith randomly-generated state.  The caller must invoke
1602    random_state (H) for all H less than I before invoking random_state
1603    (I).  */
1604
1605 static struct md5_ctx
1606 random_state (size_t i)
1607 {
1608   /* An array of states resulting from the random data, and counts of
1609      its used and allocated members.  */
1610   static struct md5_ctx *state;
1611   static size_t used;
1612   static size_t allocated;
1613
1614   struct md5_ctx *s = &state[i];
1615
1616   if (used <= i)
1617     {
1618       unsigned char buf[MD5_DIGEST_SIZE];
1619
1620       used++;
1621
1622       if (allocated <= i)
1623         {
1624           state = X2NREALLOC (state, &allocated);
1625           s = &state[i];
1626         }
1627
1628       randread (randread_source, buf, sizeof buf);
1629       md5_init_ctx (s);
1630       md5_process_bytes (buf, sizeof buf, s);
1631     }
1632
1633   return *s;
1634 }
1635
1636 /* Compare the hashes of TEXTA with length LENGTHA to those of TEXTB
1637    with length LENGTHB.  Return negative if less, zero if equal,
1638    positive if greater.  */
1639
1640 static int
1641 cmp_hashes (char const *texta, size_t lena,
1642             char const *textb, size_t lenb)
1643 {
1644   /* Try random hashes until a pair of hashes disagree.  But if the
1645      first pair of random hashes agree, check whether the keys are
1646      identical and if so report no difference.  */
1647   int diff;
1648   size_t i;
1649   for (i = 0; ; i++)
1650     {
1651       uint32_t dig[2][MD5_DIGEST_SIZE / sizeof (uint32_t)];
1652       struct md5_ctx s[2];
1653       s[0] = s[1] = random_state (i);
1654       md5_process_bytes (texta, lena, &s[0]);  md5_finish_ctx (&s[0], dig[0]);
1655       md5_process_bytes (textb, lenb, &s[1]);  md5_finish_ctx (&s[1], dig[1]);
1656       diff = memcmp (dig[0], dig[1], sizeof dig[0]);
1657       if (diff != 0)
1658         break;
1659       if (i == 0 && lena == lenb && memcmp (texta, textb, lena) == 0)
1660         break;
1661     }
1662
1663   return diff;
1664 }
1665
1666 /* Compare the keys TEXTA (of length LENA) and TEXTB (of length LENB)
1667    using one or more random hash functions.  */
1668
1669 static int
1670 compare_random (char *restrict texta, size_t lena,
1671                 char *restrict textb, size_t lenb)
1672 {
1673   int diff;
1674
1675   if (! hard_LC_COLLATE)
1676     diff = cmp_hashes (texta, lena, textb, lenb);
1677   else
1678     {
1679       /* Transform the text into the basis of comparison, so that byte
1680          strings that would otherwise considered to be equal are
1681          considered equal here even if their bytes differ.  */
1682
1683       char *buf = NULL;
1684       char stackbuf[4000];
1685       size_t tlena = xmemxfrm (stackbuf, sizeof stackbuf, texta, lena);
1686       bool a_fits = tlena <= sizeof stackbuf;
1687       size_t tlenb = xmemxfrm ((a_fits ? stackbuf + tlena : NULL),
1688                                (a_fits ? sizeof stackbuf - tlena : 0),
1689                                textb, lenb);
1690
1691       if (a_fits && tlena + tlenb <= sizeof stackbuf)
1692         buf = stackbuf;
1693       else
1694         {
1695           /* Adding 1 to the buffer size lets xmemxfrm run a bit
1696              faster by avoiding the need for an extra buffer copy.  */
1697           buf = xmalloc (tlena + tlenb + 1);
1698           xmemxfrm (buf, tlena + 1, texta, lena);
1699           xmemxfrm (buf + tlena, tlenb + 1, textb, lenb);
1700         }
1701
1702       diff = cmp_hashes (buf, tlena, buf + tlena, tlenb);
1703
1704       if (buf != stackbuf)
1705         free (buf);
1706     }
1707
1708   return diff;
1709 }
1710
1711 /* Compare two lines A and B trying every key in sequence until there
1712    are no more keys or a difference is found. */
1713
1714 static int
1715 keycompare (const struct line *a, const struct line *b)
1716 {
1717   struct keyfield const *key = keylist;
1718
1719   /* For the first iteration only, the key positions have been
1720      precomputed for us. */
1721   char *texta = a->keybeg;
1722   char *textb = b->keybeg;
1723   char *lima = a->keylim;
1724   char *limb = b->keylim;
1725
1726   int diff;
1727
1728   for (;;)
1729     {
1730       char const *translate = key->translate;
1731       bool const *ignore = key->ignore;
1732
1733       /* Find the lengths. */
1734       size_t lena = lima <= texta ? 0 : lima - texta;
1735       size_t lenb = limb <= textb ? 0 : limb - textb;
1736
1737       /* Actually compare the fields. */
1738
1739       if (key->random)
1740         diff = compare_random (texta, lena, textb, lenb);
1741       else if (key->numeric | key->general_numeric)
1742         {
1743           char savea = *lima, saveb = *limb;
1744
1745           *lima = *limb = '\0';
1746           diff = ((key->numeric ? numcompare : general_numcompare)
1747                   (texta, textb));
1748           *lima = savea, *limb = saveb;
1749         }
1750       else if (key->month)
1751         diff = getmonth (texta, lena) - getmonth (textb, lenb);
1752       /* Sorting like this may become slow, so in a simple locale the user
1753          can select a faster sort that is similar to ascii sort.  */
1754       else if (hard_LC_COLLATE)
1755         {
1756           if (ignore || translate)
1757             {
1758               char buf[4000];
1759               size_t size = lena + 1 + lenb + 1;
1760               char *copy_a = (size <= sizeof buf ? buf : xmalloc (size));
1761               char *copy_b = copy_a + lena + 1;
1762               size_t new_len_a, new_len_b, i;
1763
1764               /* Ignore and/or translate chars before comparing.  */
1765               for (new_len_a = new_len_b = i = 0; i < MAX (lena, lenb); i++)
1766                 {
1767                   if (i < lena)
1768                     {
1769                       copy_a[new_len_a] = (translate
1770                                            ? translate[to_uchar (texta[i])]
1771                                            : texta[i]);
1772                       if (!ignore || !ignore[to_uchar (texta[i])])
1773                         ++new_len_a;
1774                     }
1775                   if (i < lenb)
1776                     {
1777                       copy_b[new_len_b] = (translate
1778                                            ? translate[to_uchar (textb[i])]
1779                                            : textb [i]);
1780                       if (!ignore || !ignore[to_uchar (textb[i])])
1781                         ++new_len_b;
1782                     }
1783                 }
1784
1785               diff = xmemcoll (copy_a, new_len_a, copy_b, new_len_b);
1786
1787               if (sizeof buf < size)
1788                 free (copy_a);
1789             }
1790           else if (lena == 0)
1791             diff = - NONZERO (lenb);
1792           else if (lenb == 0)
1793             goto greater;
1794           else
1795             diff = xmemcoll (texta, lena, textb, lenb);
1796         }
1797       else if (ignore)
1798         {
1799 #define CMP_WITH_IGNORE(A, B)                                           \
1800   do                                                                    \
1801     {                                                                   \
1802           for (;;)                                                      \
1803             {                                                           \
1804               while (texta < lima && ignore[to_uchar (*texta)])         \
1805                 ++texta;                                                \
1806               while (textb < limb && ignore[to_uchar (*textb)])         \
1807                 ++textb;                                                \
1808               if (! (texta < lima && textb < limb))                     \
1809                 break;                                                  \
1810               diff = to_uchar (A) - to_uchar (B);                       \
1811               if (diff)                                                 \
1812                 goto not_equal;                                         \
1813               ++texta;                                                  \
1814               ++textb;                                                  \
1815             }                                                           \
1816                                                                         \
1817           diff = (texta < lima) - (textb < limb);                       \
1818     }                                                                   \
1819   while (0)
1820
1821           if (translate)
1822             CMP_WITH_IGNORE (translate[to_uchar (*texta)],
1823                              translate[to_uchar (*textb)]);
1824           else
1825             CMP_WITH_IGNORE (*texta, *textb);
1826         }
1827       else if (lena == 0)
1828         diff = - NONZERO (lenb);
1829       else if (lenb == 0)
1830         goto greater;
1831       else
1832         {
1833           if (translate)
1834             {
1835               while (texta < lima && textb < limb)
1836                 {
1837                   diff = (to_uchar (translate[to_uchar (*texta++)])
1838                           - to_uchar (translate[to_uchar (*textb++)]));
1839                   if (diff)
1840                     goto not_equal;
1841                 }
1842             }
1843           else
1844             {
1845               diff = memcmp (texta, textb, MIN (lena, lenb));
1846               if (diff)
1847                 goto not_equal;
1848             }
1849           diff = lena < lenb ? -1 : lena != lenb;
1850         }
1851
1852       if (diff)
1853         goto not_equal;
1854
1855       key = key->next;
1856       if (! key)
1857         break;
1858
1859       /* Find the beginning and limit of the next field.  */
1860       if (key->eword != SIZE_MAX)
1861         lima = limfield (a, key), limb = limfield (b, key);
1862       else
1863         lima = a->text + a->length - 1, limb = b->text + b->length - 1;
1864
1865       if (key->sword != SIZE_MAX)
1866         texta = begfield (a, key), textb = begfield (b, key);
1867       else
1868         {
1869           texta = a->text, textb = b->text;
1870           if (key->skipsblanks)
1871             {
1872               while (texta < lima && blanks[to_uchar (*texta)])
1873                 ++texta;
1874               while (textb < limb && blanks[to_uchar (*textb)])
1875                 ++textb;
1876             }
1877         }
1878     }
1879
1880   return 0;
1881
1882  greater:
1883   diff = 1;
1884  not_equal:
1885   return key->reverse ? -diff : diff;
1886 }
1887
1888 /* Compare two lines A and B, returning negative, zero, or positive
1889    depending on whether A compares less than, equal to, or greater than B. */
1890
1891 static int
1892 compare (const struct line *a, const struct line *b)
1893 {
1894   int diff;
1895   size_t alen, blen;
1896
1897   /* First try to compare on the specified keys (if any).
1898      The only two cases with no key at all are unadorned sort,
1899      and unadorned sort -r. */
1900   if (keylist)
1901     {
1902       diff = keycompare (a, b);
1903       if (diff | unique | stable)
1904         return diff;
1905     }
1906
1907   /* If the keys all compare equal (or no keys were specified)
1908      fall through to the default comparison.  */
1909   alen = a->length - 1, blen = b->length - 1;
1910
1911   if (alen == 0)
1912     diff = - NONZERO (blen);
1913   else if (blen == 0)
1914     diff = 1;
1915   else if (hard_LC_COLLATE)
1916     diff = xmemcoll (a->text, alen, b->text, blen);
1917   else if (! (diff = memcmp (a->text, b->text, MIN (alen, blen))))
1918     diff = alen < blen ? -1 : alen != blen;
1919
1920   return reverse ? -diff : diff;
1921 }
1922
1923 /* Check that the lines read from FILE_NAME come in order.  Return
1924    true if they are in order.  If CHECKONLY == 'c', also print a
1925    diagnostic (FILE_NAME, line number, contents of line) to stderr if
1926    they are not in order.  */
1927
1928 static bool
1929 check (char const *file_name, char checkonly)
1930 {
1931   FILE *fp = xfopen (file_name, "r");
1932   struct buffer buf;            /* Input buffer. */
1933   struct line temp;             /* Copy of previous line. */
1934   size_t alloc = 0;
1935   uintmax_t line_number = 0;
1936   struct keyfield const *key = keylist;
1937   bool nonunique = ! unique;
1938   bool ordered = true;
1939
1940   initbuf (&buf, sizeof (struct line),
1941            MAX (merge_buffer_size, sort_size));
1942   temp.text = NULL;
1943
1944   while (fillbuf (&buf, fp, file_name))
1945     {
1946       struct line const *line = buffer_linelim (&buf);
1947       struct line const *linebase = line - buf.nlines;
1948
1949       /* Make sure the line saved from the old buffer contents is
1950          less than or equal to the first line of the new buffer. */
1951       if (alloc && nonunique <= compare (&temp, line - 1))
1952         {
1953         found_disorder:
1954           {
1955             if (checkonly == 'c')
1956               {
1957                 struct line const *disorder_line = line - 1;
1958                 uintmax_t disorder_line_number =
1959                   buffer_linelim (&buf) - disorder_line + line_number;
1960                 char hr_buf[INT_BUFSIZE_BOUND (uintmax_t)];
1961                 fprintf (stderr, _("%s: %s:%s: disorder: "),
1962                          program_name, file_name,
1963                          umaxtostr (disorder_line_number, hr_buf));
1964                 write_bytes (disorder_line->text, disorder_line->length,
1965                              stderr, _("standard error"));
1966               }
1967
1968             ordered = false;
1969             break;
1970           }
1971         }
1972
1973       /* Compare each line in the buffer with its successor.  */
1974       while (linebase < --line)
1975         if (nonunique <= compare (line, line - 1))
1976           goto found_disorder;
1977
1978       line_number += buf.nlines;
1979
1980       /* Save the last line of the buffer.  */
1981       if (alloc < line->length)
1982         {
1983           do
1984             {
1985               alloc *= 2;
1986               if (! alloc)
1987                 {
1988                   alloc = line->length;
1989                   break;
1990                 }
1991             }
1992           while (alloc < line->length);
1993
1994           temp.text = xrealloc (temp.text, alloc);
1995         }
1996       memcpy (temp.text, line->text, line->length);
1997       temp.length = line->length;
1998       if (key)
1999         {
2000           temp.keybeg = temp.text + (line->keybeg - line->text);
2001           temp.keylim = temp.text + (line->keylim - line->text);
2002         }
2003     }
2004
2005   xfclose (fp, file_name);
2006   free (buf.buf);
2007   free (temp.text);
2008   return ordered;
2009 }
2010
2011 /* Merge lines from FILES onto OFP.  NTEMPS is the number of temporary
2012    files (all of which are at the start of the FILES array), and
2013    NFILES is the number of files; 0 <= NTEMPS <= NFILES <= NMERGE.
2014    Close input and output files before returning.
2015    OUTPUT_FILE gives the name of the output file.  If it is NULL,
2016    the output file is standard output.  If OFP is NULL, the output
2017    file has not been opened yet (or written to, if standard output).  */
2018
2019 static void
2020 mergefps (struct sortfile *files, size_t ntemps, size_t nfiles,
2021           FILE *ofp, char const *output_file)
2022 {
2023   FILE *fps[NMERGE];            /* Input streams for each file.  */
2024   struct buffer buffer[NMERGE]; /* Input buffers for each file. */
2025   struct line saved;            /* Saved line storage for unique check. */
2026   struct line const *savedline = NULL;
2027                                 /* &saved if there is a saved line. */
2028   size_t savealloc = 0;         /* Size allocated for the saved line. */
2029   struct line const *cur[NMERGE]; /* Current line in each line table. */
2030   struct line const *base[NMERGE]; /* Base of each line table.  */
2031   size_t ord[NMERGE];           /* Table representing a permutation of fps,
2032                                    such that cur[ord[0]] is the smallest line
2033                                    and will be next output. */
2034   size_t i;
2035   size_t j;
2036   size_t t;
2037   struct keyfield const *key = keylist;
2038   saved.text = NULL;
2039
2040   /* Read initial lines from each input file. */
2041   for (i = 0; i < nfiles; )
2042     {
2043       fps[i] = (files[i].pid
2044                 ? open_temp (files[i].name, files[i].pid)
2045                 : xfopen (files[i].name, "r"));
2046       initbuf (&buffer[i], sizeof (struct line),
2047                MAX (merge_buffer_size, sort_size / nfiles));
2048       if (fillbuf (&buffer[i], fps[i], files[i].name))
2049         {
2050           struct line const *linelim = buffer_linelim (&buffer[i]);
2051           cur[i] = linelim - 1;
2052           base[i] = linelim - buffer[i].nlines;
2053           i++;
2054         }
2055       else
2056         {
2057           /* fps[i] is empty; eliminate it from future consideration.  */
2058           xfclose (fps[i], files[i].name);
2059           if (i < ntemps)
2060             {
2061               ntemps--;
2062               zaptemp (files[i].name);
2063             }
2064           free (buffer[i].buf);
2065           --nfiles;
2066           for (j = i; j < nfiles; ++j)
2067             files[j] = files[j + 1];
2068         }
2069     }
2070
2071   if (! ofp)
2072     ofp = xfopen (output_file, "w");
2073
2074   /* Set up the ord table according to comparisons among input lines.
2075      Since this only reorders two items if one is strictly greater than
2076      the other, it is stable. */
2077   for (i = 0; i < nfiles; ++i)
2078     ord[i] = i;
2079   for (i = 1; i < nfiles; ++i)
2080     if (0 < compare (cur[ord[i - 1]], cur[ord[i]]))
2081       t = ord[i - 1], ord[i - 1] = ord[i], ord[i] = t, i = 0;
2082
2083   /* Repeatedly output the smallest line until no input remains. */
2084   while (nfiles)
2085     {
2086       struct line const *smallest = cur[ord[0]];
2087
2088       /* If uniquified output is turned on, output only the first of
2089          an identical series of lines. */
2090       if (unique)
2091         {
2092           if (savedline && compare (savedline, smallest))
2093             {
2094               savedline = NULL;
2095               write_bytes (saved.text, saved.length, ofp, output_file);
2096             }
2097           if (!savedline)
2098             {
2099               savedline = &saved;
2100               if (savealloc < smallest->length)
2101                 {
2102                   do
2103                     if (! savealloc)
2104                       {
2105                         savealloc = smallest->length;
2106                         break;
2107                       }
2108                   while ((savealloc *= 2) < smallest->length);
2109
2110                   saved.text = xrealloc (saved.text, savealloc);
2111                 }
2112               saved.length = smallest->length;
2113               memcpy (saved.text, smallest->text, saved.length);
2114               if (key)
2115                 {
2116                   saved.keybeg =
2117                     saved.text + (smallest->keybeg - smallest->text);
2118                   saved.keylim =
2119                     saved.text + (smallest->keylim - smallest->text);
2120                 }
2121             }
2122         }
2123       else
2124         write_bytes (smallest->text, smallest->length, ofp, output_file);
2125
2126       /* Check if we need to read more lines into core. */
2127       if (base[ord[0]] < smallest)
2128         cur[ord[0]] = smallest - 1;
2129       else
2130         {
2131           if (fillbuf (&buffer[ord[0]], fps[ord[0]], files[ord[0]].name))
2132             {
2133               struct line const *linelim = buffer_linelim (&buffer[ord[0]]);
2134               cur[ord[0]] = linelim - 1;
2135               base[ord[0]] = linelim - buffer[ord[0]].nlines;
2136             }
2137           else
2138             {
2139               /* We reached EOF on fps[ord[0]].  */
2140               for (i = 1; i < nfiles; ++i)
2141                 if (ord[i] > ord[0])
2142                   --ord[i];
2143               --nfiles;
2144               xfclose (fps[ord[0]], files[ord[0]].name);
2145               if (ord[0] < ntemps)
2146                 {
2147                   ntemps--;
2148                   zaptemp (files[ord[0]].name);
2149                 }
2150               free (buffer[ord[0]].buf);
2151               for (i = ord[0]; i < nfiles; ++i)
2152                 {
2153                   fps[i] = fps[i + 1];
2154                   files[i] = files[i + 1];
2155                   buffer[i] = buffer[i + 1];
2156                   cur[i] = cur[i + 1];
2157                   base[i] = base[i + 1];
2158                 }
2159               for (i = 0; i < nfiles; ++i)
2160                 ord[i] = ord[i + 1];
2161               continue;
2162             }
2163         }
2164
2165       /* The new line just read in may be larger than other lines
2166          already in main memory; push it back in the queue until we
2167          encounter a line larger than it.  Optimize for the common
2168          case where the new line is smallest.  */
2169       {
2170         size_t lo = 1;
2171         size_t hi = nfiles;
2172         size_t probe = lo;
2173         size_t ord0 = ord[0];
2174         size_t count_of_smaller_lines;
2175
2176         while (lo < hi)
2177           {
2178             int cmp = compare (cur[ord0], cur[ord[probe]]);
2179             if (cmp < 0 || (cmp == 0 && ord0 < ord[probe]))
2180               hi = probe;
2181             else
2182               lo = probe + 1;
2183             probe = (lo + hi) / 2;
2184           }
2185
2186         count_of_smaller_lines = lo - 1;
2187         for (j = 0; j < count_of_smaller_lines; j++)
2188           ord[j] = ord[j + 1];
2189         ord[count_of_smaller_lines] = ord0;
2190       }
2191
2192       /* Free up some resources every once in a while.  */
2193       if (MAX_PROCS_BEFORE_REAP < nprocs)
2194         reap_some ();
2195     }
2196
2197   if (unique && savedline)
2198     {
2199       write_bytes (saved.text, saved.length, ofp, output_file);
2200       free (saved.text);
2201     }
2202
2203   xfclose (ofp, output_file);
2204 }
2205
2206 /* Merge into T the two sorted arrays of lines LO (with NLO members)
2207    and HI (with NHI members).  T, LO, and HI point just past their
2208    respective arrays, and the arrays are in reverse order.  NLO and
2209    NHI must be positive, and HI - NHI must equal T - (NLO + NHI).  */
2210
2211 static inline void
2212 mergelines (struct line *t,
2213             struct line const *lo, size_t nlo,
2214             struct line const *hi, size_t nhi)
2215 {
2216   for (;;)
2217     if (compare (lo - 1, hi - 1) <= 0)
2218       {
2219         *--t = *--lo;
2220         if (! --nlo)
2221           {
2222             /* HI - NHI equalled T - (NLO + NHI) when this function
2223                began.  Therefore HI must equal T now, and there is no
2224                need to copy from HI to T.  */
2225             return;
2226           }
2227       }
2228     else
2229       {
2230         *--t = *--hi;
2231         if (! --nhi)
2232           {
2233             do
2234               *--t = *--lo;
2235             while (--nlo);
2236
2237             return;
2238           }
2239       }
2240 }
2241
2242 /* Sort the array LINES with NLINES members, using TEMP for temporary space.
2243    NLINES must be at least 2.
2244    The input and output arrays are in reverse order, and LINES and
2245    TEMP point just past the end of their respective arrays.
2246
2247    Use a recursive divide-and-conquer algorithm, in the style
2248    suggested by Knuth volume 3 (2nd edition), exercise 5.2.4-23.  Use
2249    the optimization suggested by exercise 5.2.4-10; this requires room
2250    for only 1.5*N lines, rather than the usual 2*N lines.  Knuth
2251    writes that this memory optimization was originally published by
2252    D. A. Bell, Comp J. 1 (1958), 75.  */
2253
2254 static void
2255 sortlines (struct line *lines, size_t nlines, struct line *temp)
2256 {
2257   if (nlines == 2)
2258     {
2259       if (0 < compare (&lines[-1], &lines[-2]))
2260         {
2261           struct line tmp = lines[-1];
2262           lines[-1] = lines[-2];
2263           lines[-2] = tmp;
2264         }
2265     }
2266   else
2267     {
2268       size_t nlo = nlines / 2;
2269       size_t nhi = nlines - nlo;
2270       struct line *lo = lines;
2271       struct line *hi = lines - nlo;
2272       struct line *sorted_lo = temp;
2273
2274       sortlines (hi, nhi, temp);
2275       if (1 < nlo)
2276         sortlines_temp (lo, nlo, sorted_lo);
2277       else
2278         sorted_lo[-1] = lo[-1];
2279
2280       mergelines (lines, sorted_lo, nlo, hi, nhi);
2281     }
2282 }
2283
2284 /* Like sortlines (LINES, NLINES, TEMP), except output into TEMP
2285    rather than sorting in place.  */
2286
2287 static void
2288 sortlines_temp (struct line *lines, size_t nlines, struct line *temp)
2289 {
2290   if (nlines == 2)
2291     {
2292       /* Declare `swap' as int, not bool, to work around a bug
2293          <http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html>
2294          in the IBM xlc 6.0.0.0 compiler in 64-bit mode.  */
2295       int swap = (0 < compare (&lines[-1], &lines[-2]));
2296       temp[-1] = lines[-1 - swap];
2297       temp[-2] = lines[-2 + swap];
2298     }
2299   else
2300     {
2301       size_t nlo = nlines / 2;
2302       size_t nhi = nlines - nlo;
2303       struct line *lo = lines;
2304       struct line *hi = lines - nlo;
2305       struct line *sorted_hi = temp - nlo;
2306
2307       sortlines_temp (hi, nhi, sorted_hi);
2308       if (1 < nlo)
2309         sortlines (lo, nlo, temp);
2310
2311       mergelines (temp, lo, nlo, sorted_hi, nhi);
2312     }
2313 }
2314
2315 /* Scan through FILES[NTEMPS .. NFILES-1] looking for a file that is
2316    the same as OUTFILE.  If found, merge the found instances (and perhaps
2317    some other files) into a temporary file so that it can in turn be
2318    merged into OUTFILE without destroying OUTFILE before it is completely
2319    read.  Return the new value of NFILES, which differs from the old if
2320    some merging occurred.
2321
2322    This test ensures that an otherwise-erroneous use like
2323    "sort -m -o FILE ... FILE ..." copies FILE before writing to it.
2324    It's not clear that POSIX requires this nicety.
2325    Detect common error cases, but don't try to catch obscure cases like
2326    "cat ... FILE ... | sort -m -o FILE"
2327    where traditional "sort" doesn't copy the input and where
2328    people should know that they're getting into trouble anyway.
2329    Catching these obscure cases would slow down performance in
2330    common cases.  */
2331
2332 static size_t
2333 avoid_trashing_input (struct sortfile *files, size_t ntemps,
2334                       size_t nfiles, char const *outfile)
2335 {
2336   size_t i;
2337   bool got_outstat = false;
2338   struct stat outstat;
2339
2340   for (i = ntemps; i < nfiles; i++)
2341     {
2342       bool is_stdin = STREQ (files[i].name, "-");
2343       bool same;
2344       struct stat instat;
2345
2346       if (outfile && STREQ (outfile, files[i].name) && !is_stdin)
2347         same = true;
2348       else
2349         {
2350           if (! got_outstat)
2351             {
2352               if ((outfile
2353                    ? stat (outfile, &outstat)
2354                    : fstat (STDOUT_FILENO, &outstat))
2355                   != 0)
2356                 break;
2357               got_outstat = true;
2358             }
2359
2360           same = (((is_stdin
2361                     ? fstat (STDIN_FILENO, &instat)
2362                     : stat (files[i].name, &instat))
2363                    == 0)
2364                   && SAME_INODE (instat, outstat));
2365         }
2366
2367       if (same)
2368         {
2369           FILE *tftp;
2370           pid_t pid;
2371           char *temp = create_temp (&tftp, &pid);
2372           mergefps (&files[i],0, nfiles - i, tftp, temp);
2373           files[i].name = temp;
2374           files[i].pid = pid;
2375           return i + 1;
2376         }
2377     }
2378
2379   return nfiles;
2380 }
2381
2382 /* Merge the input FILES.  NTEMPS is the number of files at the
2383    start of FILES that are temporary; it is zero at the top level.
2384    NFILES is the total number of files.  Put the output in
2385    OUTPUT_FILE; a null OUTPUT_FILE stands for standard output.  */
2386
2387 static void
2388 merge (struct sortfile *files, size_t ntemps, size_t nfiles,
2389        char const *output_file)
2390 {
2391   while (NMERGE < nfiles)
2392     {
2393       /* Number of input files processed so far.  */
2394       size_t in;
2395
2396       /* Number of output files generated so far.  */
2397       size_t out;
2398
2399       /* nfiles % NMERGE; this counts input files that are left over
2400          after all full-sized merges have been done.  */
2401       size_t remainder;
2402
2403       /* Number of easily-available slots at the next loop iteration.  */
2404       size_t cheap_slots;
2405
2406       /* Do as many NMERGE-size merges as possible.  */
2407       for (out = in = 0; out < nfiles / NMERGE; out++, in += NMERGE)
2408         {
2409           FILE *tfp;
2410           pid_t pid;
2411           char *temp = create_temp (&tfp, &pid);
2412           size_t nt = MIN (ntemps, NMERGE);
2413           ntemps -= nt;
2414           mergefps (&files[in], nt, NMERGE, tfp, temp);
2415           files[out].name = temp;
2416           files[out].pid = pid;
2417         }
2418
2419       remainder = nfiles - in;
2420       cheap_slots = NMERGE - out % NMERGE;
2421
2422       if (cheap_slots < remainder)
2423         {
2424           /* So many files remain that they can't all be put into the last
2425              NMERGE-sized output window.  Do one more merge.  Merge as few
2426              files as possible, to avoid needless I/O.  */
2427           size_t nshortmerge = remainder - cheap_slots + 1;
2428           FILE *tfp;
2429           pid_t pid;
2430           char *temp = create_temp (&tfp, &pid);
2431           size_t nt = MIN (ntemps, nshortmerge);
2432           ntemps -= nt;
2433           mergefps (&files[in], nt, nshortmerge, tfp, temp);
2434           files[out].name = temp;
2435           files[out++].pid = pid;
2436           in += nshortmerge;
2437         }
2438
2439       /* Put the remaining input files into the last NMERGE-sized output
2440          window, so they will be merged in the next pass.  */
2441       memmove(&files[out], &files[in], (nfiles - in) * sizeof *files);
2442       ntemps += out;
2443       nfiles -= in - out;
2444     }
2445
2446   nfiles = avoid_trashing_input (files, ntemps, nfiles, output_file);
2447   mergefps (files, ntemps, nfiles, NULL, output_file);
2448 }
2449
2450 /* Sort NFILES FILES onto OUTPUT_FILE. */
2451
2452 static void
2453 sort (char * const *files, size_t nfiles, char const *output_file)
2454 {
2455   struct buffer buf;
2456   size_t ntemps = 0;
2457   bool output_file_created = false;
2458
2459   buf.alloc = 0;
2460
2461   while (nfiles)
2462     {
2463       char const *temp_output;
2464       char const *file = *files;
2465       FILE *fp = xfopen (file, "r");
2466       FILE *tfp;
2467       size_t bytes_per_line = (2 * sizeof (struct line)
2468                                - sizeof (struct line) / 2);
2469
2470       if (! buf.alloc)
2471         initbuf (&buf, bytes_per_line,
2472                  sort_buffer_size (&fp, 1, files, nfiles, bytes_per_line));
2473       buf.eof = false;
2474       files++;
2475       nfiles--;
2476
2477       while (fillbuf (&buf, fp, file))
2478         {
2479           struct line *line;
2480           struct line *linebase;
2481
2482           if (buf.eof && nfiles
2483               && (bytes_per_line + 1
2484                   < (buf.alloc - buf.used - bytes_per_line * buf.nlines)))
2485             {
2486               /* End of file, but there is more input and buffer room.
2487                  Concatenate the next input file; this is faster in
2488                  the usual case.  */
2489               buf.left = buf.used;
2490               break;
2491             }
2492
2493           line = buffer_linelim (&buf);
2494           linebase = line - buf.nlines;
2495           if (1 < buf.nlines)
2496             sortlines (line, buf.nlines, linebase);
2497           if (buf.eof && !nfiles && !ntemps && !buf.left)
2498             {
2499               xfclose (fp, file);
2500               tfp = xfopen (output_file, "w");
2501               temp_output = output_file;
2502               output_file_created = true;
2503             }
2504           else
2505             {
2506               ++ntemps;
2507               temp_output = create_temp (&tfp, NULL);
2508             }
2509
2510           do
2511             {
2512               line--;
2513               write_bytes (line->text, line->length, tfp, temp_output);
2514               if (unique)
2515                 while (linebase < line && compare (line, line - 1) == 0)
2516                   line--;
2517             }
2518           while (linebase < line);
2519
2520           xfclose (tfp, temp_output);
2521
2522           /* Free up some resources every once in a while.  */
2523           if (MAX_PROCS_BEFORE_REAP < nprocs)
2524             reap_some ();
2525
2526           if (output_file_created)
2527             goto finish;
2528         }
2529       xfclose (fp, file);
2530     }
2531
2532  finish:
2533   free (buf.buf);
2534
2535   if (! output_file_created)
2536     {
2537       size_t i;
2538       struct tempnode *node = temphead;
2539       struct sortfile *tempfiles = xnmalloc (ntemps, sizeof *tempfiles);
2540       for (i = 0; node; i++)
2541         {
2542           tempfiles[i].name = node->name;
2543           tempfiles[i].pid = node->pid;
2544           node = node->next;
2545         }
2546       merge (tempfiles, ntemps, ntemps, output_file);
2547       free (tempfiles);
2548     }
2549 }
2550
2551 /* Insert a malloc'd copy of key KEY_ARG at the end of the key list.  */
2552
2553 static void
2554 insertkey (struct keyfield *key_arg)
2555 {
2556   struct keyfield **p;
2557   struct keyfield *key = xmemdup (key_arg, sizeof *key);
2558
2559   for (p = &keylist; *p; p = &(*p)->next)
2560     continue;
2561   *p = key;
2562   key->next = NULL;
2563 }
2564
2565 /* Report a bad field specification SPEC, with extra info MSGID.  */
2566
2567 static void badfieldspec (char const *, char const *)
2568      ATTRIBUTE_NORETURN;
2569 static void
2570 badfieldspec (char const *spec, char const *msgid)
2571 {
2572   error (SORT_FAILURE, 0, _("%s: invalid field specification %s"),
2573          _(msgid), quote (spec));
2574   abort ();
2575 }
2576
2577 /* Report incompatible options.  */
2578
2579 static void incompatible_options (char const *) ATTRIBUTE_NORETURN;
2580 static void
2581 incompatible_options (char const *opts)
2582 {
2583   error (SORT_FAILURE, 0, _("options `-%s' are incompatible"), opts);
2584   abort ();
2585 }
2586
2587 /* Check compatibility of ordering options.  */
2588
2589 static void
2590 check_ordering_compatibility (void)
2591 {
2592   struct keyfield const *key;
2593
2594   for (key = keylist; key; key = key->next)
2595     if ((1 < (key->random + key->numeric + key->general_numeric + key->month
2596               + !!key->ignore))
2597         || (key->random && key->translate))
2598       {
2599         char opts[7];
2600         char *p = opts;
2601         if (key->ignore == nondictionary)
2602           *p++ = 'd';
2603         if (key->translate)
2604           *p++ = 'f';
2605         if (key->general_numeric)
2606           *p++ = 'g';
2607         if (key->ignore == nonprinting)
2608           *p++ = 'i';
2609         if (key->month)
2610           *p++ = 'M';
2611         if (key->numeric)
2612           *p++ = 'n';
2613         if (key->random)
2614           *p++ = 'R';
2615         *p = '\0';
2616         incompatible_options (opts);
2617       }
2618 }
2619
2620 /* Parse the leading integer in STRING and store the resulting value
2621    (which must fit into size_t) into *VAL.  Return the address of the
2622    suffix after the integer.  If the value is too large, silently
2623    substitute SIZE_MAX.  If MSGID is NULL, return NULL after
2624    failure; otherwise, report MSGID and exit on failure.  */
2625
2626 static char const *
2627 parse_field_count (char const *string, size_t *val, char const *msgid)
2628 {
2629   char *suffix;
2630   uintmax_t n;
2631
2632   switch (xstrtoumax (string, &suffix, 10, &n, ""))
2633     {
2634     case LONGINT_OK:
2635     case LONGINT_INVALID_SUFFIX_CHAR:
2636       *val = n;
2637       if (*val == n)
2638         break;
2639       /* Fall through.  */
2640     case LONGINT_OVERFLOW:
2641     case LONGINT_OVERFLOW | LONGINT_INVALID_SUFFIX_CHAR:
2642       *val = SIZE_MAX;
2643       break;
2644
2645     case LONGINT_INVALID:
2646       if (msgid)
2647         error (SORT_FAILURE, 0, _("%s: invalid count at start of %s"),
2648                _(msgid), quote (string));
2649       return NULL;
2650     }
2651
2652   return suffix;
2653 }
2654
2655 /* Handle interrupts and hangups. */
2656
2657 static void
2658 sighandler (int sig)
2659 {
2660   if (! SA_NOCLDSTOP)
2661     signal (sig, SIG_IGN);
2662
2663   cleanup ();
2664
2665   signal (sig, SIG_DFL);
2666   raise (sig);
2667 }
2668
2669 /* Set the ordering options for KEY specified in S.
2670    Return the address of the first character in S that
2671    is not a valid ordering option.
2672    BLANKTYPE is the kind of blanks that 'b' should skip. */
2673
2674 static char *
2675 set_ordering (const char *s, struct keyfield *key, enum blanktype blanktype)
2676 {
2677   while (*s)
2678     {
2679       switch (*s)
2680         {
2681         case 'b':
2682           if (blanktype == bl_start || blanktype == bl_both)
2683             key->skipsblanks = true;
2684           if (blanktype == bl_end || blanktype == bl_both)
2685             key->skipeblanks = true;
2686           break;
2687         case 'd':
2688           key->ignore = nondictionary;
2689           break;
2690         case 'f':
2691           key->translate = fold_toupper;
2692           break;
2693         case 'g':
2694           key->general_numeric = true;
2695           break;
2696         case 'i':
2697           /* Option order should not matter, so don't let -i override
2698              -d.  -d implies -i, but -i does not imply -d.  */
2699           if (! key->ignore)
2700             key->ignore = nonprinting;
2701           break;
2702         case 'M':
2703           key->month = true;
2704           break;
2705         case 'n':
2706           key->numeric = true;
2707           break;
2708         case 'R':
2709           key->random = true;
2710           break;
2711         case 'r':
2712           key->reverse = true;
2713           break;
2714         default:
2715           return (char *) s;
2716         }
2717       ++s;
2718     }
2719   return (char *) s;
2720 }
2721
2722 static struct keyfield *
2723 key_init (struct keyfield *key)
2724 {
2725   memset (key, 0, sizeof *key);
2726   key->eword = SIZE_MAX;
2727   return key;
2728 }
2729
2730 int
2731 main (int argc, char **argv)
2732 {
2733   struct keyfield *key;
2734   struct keyfield key_buf;
2735   struct keyfield gkey;
2736   char const *s;
2737   int c = 0;
2738   char checkonly = 0;
2739   bool mergeonly = false;
2740   char *random_source = NULL;
2741   bool need_random = false;
2742   size_t nfiles = 0;
2743   bool posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
2744   bool obsolete_usage = (posix2_version () < 200112);
2745   char **files;
2746   char const *outfile = NULL;
2747
2748   initialize_main (&argc, &argv);
2749   program_name = argv[0];
2750   setlocale (LC_ALL, "");
2751   bindtextdomain (PACKAGE, LOCALEDIR);
2752   textdomain (PACKAGE);
2753
2754   initialize_exit_failure (SORT_FAILURE);
2755
2756   hard_LC_COLLATE = hard_locale (LC_COLLATE);
2757 #if HAVE_NL_LANGINFO
2758   hard_LC_TIME = hard_locale (LC_TIME);
2759 #endif
2760
2761   /* Get locale's representation of the decimal point.  */
2762   {
2763     struct lconv const *locale = localeconv ();
2764
2765     /* If the locale doesn't define a decimal point, or if the decimal
2766        point is multibyte, use the C locale's decimal point.  FIXME:
2767        add support for multibyte decimal points.  */
2768     decimal_point = to_uchar (locale->decimal_point[0]);
2769     if (! decimal_point || locale->decimal_point[1])
2770       decimal_point = '.';
2771
2772     /* FIXME: add support for multibyte thousands separators.  */
2773     thousands_sep = to_uchar (*locale->thousands_sep);
2774     if (! thousands_sep || locale->thousands_sep[1])
2775       thousands_sep = -1;
2776   }
2777
2778   have_read_stdin = false;
2779   inittables ();
2780
2781   {
2782     size_t i;
2783     static int const sig[] =
2784       {
2785         /* The usual suspects.  */
2786         SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
2787 #ifdef SIGPOLL
2788         SIGPOLL,
2789 #endif
2790 #ifdef SIGPROF
2791         SIGPROF,
2792 #endif
2793 #ifdef SIGVTALRM
2794         SIGVTALRM,
2795 #endif
2796 #ifdef SIGXCPU
2797         SIGXCPU,
2798 #endif
2799 #ifdef SIGXFSZ
2800         SIGXFSZ,
2801 #endif
2802       };
2803     enum { nsigs = sizeof sig / sizeof sig[0] };
2804
2805 #if SA_NOCLDSTOP
2806     struct sigaction act;
2807
2808     sigemptyset (&caught_signals);
2809     for (i = 0; i < nsigs; i++)
2810       {
2811         sigaction (sig[i], NULL, &act);
2812         if (act.sa_handler != SIG_IGN)
2813           sigaddset (&caught_signals, sig[i]);
2814       }
2815
2816     act.sa_handler = sighandler;
2817     act.sa_mask = caught_signals;
2818     act.sa_flags = 0;
2819
2820     for (i = 0; i < nsigs; i++)
2821       if (sigismember (&caught_signals, sig[i]))
2822         sigaction (sig[i], &act, NULL);
2823 #else
2824     for (i = 0; i < nsigs; i++)
2825       if (signal (sig[i], SIG_IGN) != SIG_IGN)
2826         {
2827           signal (sig[i], sighandler);
2828           siginterrupt (sig[i], 1);
2829         }
2830 #endif
2831   }
2832
2833   /* The signal mask is known, so it is safe to invoke exit_cleanup.  */
2834   atexit (exit_cleanup);
2835
2836   gkey.sword = gkey.eword = SIZE_MAX;
2837   gkey.ignore = NULL;
2838   gkey.translate = NULL;
2839   gkey.numeric = gkey.general_numeric = gkey.random = false;
2840   gkey.month = gkey.reverse = false;
2841   gkey.skipsblanks = gkey.skipeblanks = false;
2842
2843   files = xnmalloc (argc, sizeof *files);
2844
2845   for (;;)
2846     {
2847       /* Parse an operand as a file after "--" was seen; or if
2848          pedantic and a file was seen, unless the POSIX version
2849          predates 1003.1-2001 and -c was not seen and the operand is
2850          "-o FILE" or "-oFILE".  */
2851
2852       if (c == -1
2853           || (posixly_correct && nfiles != 0
2854               && ! (obsolete_usage
2855                     && ! checkonly
2856                     && optind != argc
2857                     && argv[optind][0] == '-' && argv[optind][1] == 'o'
2858                     && (argv[optind][2] || optind + 1 != argc)))
2859           || ((c = getopt_long (argc, argv, short_options,
2860                                 long_options, NULL))
2861               == -1))
2862         {
2863           if (argc <= optind)
2864             break;
2865           files[nfiles++] = argv[optind++];
2866         }
2867       else switch (c)
2868         {
2869         case 1:
2870           key = NULL;
2871           if (optarg[0] == '+')
2872             {
2873               bool minus_pos_usage = (optind != argc && argv[optind][0] == '-'
2874                                       && ISDIGIT (argv[optind][1]));
2875               obsolete_usage |= minus_pos_usage & ~posixly_correct;
2876               if (obsolete_usage)
2877                 {
2878                   /* Treat +POS1 [-POS2] as a key if possible; but silently
2879                      treat an operand as a file if it is not a valid +POS1.  */
2880                   key = key_init (&key_buf);
2881                   s = parse_field_count (optarg + 1, &key->sword, NULL);
2882                   if (s && *s == '.')
2883                     s = parse_field_count (s + 1, &key->schar, NULL);
2884                   if (! (key->sword | key->schar))
2885                     key->sword = SIZE_MAX;
2886                   if (! s || *set_ordering (s, key, bl_start))
2887                     {
2888                       free (key);
2889                       key = NULL;
2890                     }
2891                   else
2892                     {
2893                       if (minus_pos_usage)
2894                         {
2895                           char const *optarg1 = argv[optind++];
2896                           s = parse_field_count (optarg1 + 1, &key->eword,
2897                                              N_("invalid number after `-'"));
2898                           if (*s == '.')
2899                             s = parse_field_count (s + 1, &key->echar,
2900                                                N_("invalid number after `.'"));
2901                           if (*set_ordering (s, key, bl_end))
2902                             badfieldspec (optarg1,
2903                                       N_("stray character in field spec"));
2904                         }
2905                       insertkey (key);
2906                     }
2907                 }
2908             }
2909           if (! key)
2910             files[nfiles++] = optarg;
2911           break;
2912
2913         case 'b':
2914         case 'd':
2915         case 'f':
2916         case 'g':
2917         case 'i':
2918         case 'M':
2919         case 'n':
2920         case 'r':
2921         case 'R':
2922           {
2923             char str[2];
2924             str[0] = c;
2925             str[1] = '\0';
2926             set_ordering (str, &gkey, bl_both);
2927           }
2928           break;
2929
2930         case CHECK_OPTION:
2931           c = (optarg
2932                ? XARGMATCH ("--check", optarg, check_args, check_types)
2933                : 'c');
2934           /* Fall through.  */
2935         case 'c':
2936         case 'C':
2937           if (checkonly && checkonly != c)
2938             incompatible_options ("cC");
2939           checkonly = c;
2940           break;
2941
2942         case COMPRESS_PROGRAM_OPTION:
2943           if (compress_program && strcmp (compress_program, optarg) != 0)
2944             error (SORT_FAILURE, 0, _("multiple compress programs specified"));
2945           compress_program = optarg;
2946           break;
2947
2948         case 'k':
2949           key = key_init (&key_buf);
2950
2951           /* Get POS1. */
2952           s = parse_field_count (optarg, &key->sword,
2953                                  N_("invalid number at field start"));
2954           if (! key->sword--)
2955             {
2956               /* Provoke with `sort -k0' */
2957               badfieldspec (optarg, N_("field number is zero"));
2958             }
2959           if (*s == '.')
2960             {
2961               s = parse_field_count (s + 1, &key->schar,
2962                                      N_("invalid number after `.'"));
2963               if (! key->schar--)
2964                 {
2965                   /* Provoke with `sort -k1.0' */
2966                   badfieldspec (optarg, N_("character offset is zero"));
2967                 }
2968             }
2969           if (! (key->sword | key->schar))
2970             key->sword = SIZE_MAX;
2971           s = set_ordering (s, key, bl_start);
2972           if (*s != ',')
2973             {
2974               key->eword = SIZE_MAX;
2975               key->echar = 0;
2976             }
2977           else
2978             {
2979               /* Get POS2. */
2980               s = parse_field_count (s + 1, &key->eword,
2981                                      N_("invalid number after `,'"));
2982               if (! key->eword--)
2983                 {
2984                   /* Provoke with `sort -k1,0' */
2985                   badfieldspec (optarg, N_("field number is zero"));
2986                 }
2987               if (*s == '.')
2988                 s = parse_field_count (s + 1, &key->echar,
2989                                        N_("invalid number after `.'"));
2990               else
2991                 {
2992                   /* `-k 2,3' is equivalent to `+1 -3'.  */
2993                   key->eword++;
2994                 }
2995               s = set_ordering (s, key, bl_end);
2996             }
2997           if (*s)
2998             badfieldspec (optarg, N_("stray character in field spec"));
2999           insertkey (key);
3000           break;
3001
3002         case 'm':
3003           mergeonly = true;
3004           break;
3005
3006         case 'o':
3007           if (outfile && !STREQ (outfile, optarg))
3008             error (SORT_FAILURE, 0, _("multiple output files specified"));
3009           outfile = optarg;
3010           break;
3011
3012         case RANDOM_SOURCE_OPTION:
3013           if (random_source && !STREQ (random_source, optarg))
3014             error (SORT_FAILURE, 0, _("multiple random sources specified"));
3015           random_source = optarg;
3016           break;
3017
3018         case 's':
3019           stable = true;
3020           break;
3021
3022         case 'S':
3023           specify_sort_size (optarg);
3024           break;
3025
3026         case 't':
3027           {
3028             char newtab = optarg[0];
3029             if (! newtab)
3030               error (SORT_FAILURE, 0, _("empty tab"));
3031             if (optarg[1])
3032               {
3033                 if (STREQ (optarg, "\\0"))
3034                   newtab = '\0';
3035                 else
3036                   {
3037                     /* Provoke with `sort -txx'.  Complain about
3038                        "multi-character tab" instead of "multibyte tab", so
3039                        that the diagnostic's wording does not need to be
3040                        changed once multibyte characters are supported.  */
3041                     error (SORT_FAILURE, 0, _("multi-character tab %s"),
3042                            quote (optarg));
3043                   }
3044               }
3045             if (tab != TAB_DEFAULT && tab != newtab)
3046               error (SORT_FAILURE, 0, _("incompatible tabs"));
3047             tab = newtab;
3048           }
3049           break;
3050
3051         case 'T':
3052           add_temp_dir (optarg);
3053           break;
3054
3055         case 'u':
3056           unique = true;
3057           break;
3058
3059         case 'y':
3060           /* Accept and ignore e.g. -y0 for compatibility with Solaris 2.x
3061              through Solaris 7.  It is also accepted by many non-Solaris
3062              "sort" implementations, e.g., AIX 5.2, HP-UX 11i v2, IRIX 6.5.
3063              -y is marked as obsolete starting with Solaris 8 (1999), but is
3064              still accepted as of Solaris 10 prerelease (2004).
3065
3066              Solaris 2.5.1 "sort -y 100" reads the input file "100", but
3067              emulate Solaris 8 and 9 "sort -y 100" which ignores the "100",
3068              and which in general ignores the argument after "-y" if it
3069              consists entirely of digits (it can even be empty).  */
3070           if (optarg == argv[optind - 1])
3071             {
3072               char const *p;
3073               for (p = optarg; ISDIGIT (*p); p++)
3074                 continue;
3075               optind -= (*p != '\0');
3076             }
3077           break;
3078
3079         case 'z':
3080           eolchar = 0;
3081           break;
3082
3083         case_GETOPT_HELP_CHAR;
3084
3085         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
3086
3087         default:
3088           usage (SORT_FAILURE);
3089         }
3090     }
3091
3092   /* Inheritance of global options to individual keys. */
3093   for (key = keylist; key; key = key->next)
3094     {
3095       if (! (key->ignore || key->translate
3096              || (key->skipsblanks | key->reverse
3097                  | key->skipeblanks | key->month | key->numeric
3098                  | key->general_numeric
3099                  | key->random)))
3100         {
3101           key->ignore = gkey.ignore;
3102           key->translate = gkey.translate;
3103           key->skipsblanks = gkey.skipsblanks;
3104           key->skipeblanks = gkey.skipeblanks;
3105           key->month = gkey.month;
3106           key->numeric = gkey.numeric;
3107           key->general_numeric = gkey.general_numeric;
3108           key->random = gkey.random;
3109           key->reverse = gkey.reverse;
3110         }
3111
3112       need_random |= key->random;
3113     }
3114
3115   if (!keylist && (gkey.ignore || gkey.translate
3116                    || (gkey.skipsblanks | gkey.skipeblanks | gkey.month
3117                        | gkey.numeric | gkey.general_numeric
3118                        | gkey.random)))
3119     {
3120       insertkey (&gkey);
3121       need_random |= gkey.random;
3122     }
3123
3124   check_ordering_compatibility ();
3125
3126   reverse = gkey.reverse;
3127
3128   if (need_random)
3129     {
3130       randread_source = randread_new (random_source, MD5_DIGEST_SIZE);
3131       if (! randread_source)
3132         die (_("open failed"), random_source);
3133     }
3134
3135   if (temp_dir_count == 0)
3136     {
3137       char const *tmp_dir = getenv ("TMPDIR");
3138       add_temp_dir (tmp_dir ? tmp_dir : DEFAULT_TMPDIR);
3139     }
3140
3141   if (nfiles == 0)
3142     {
3143       static char *minus = "-";
3144       nfiles = 1;
3145       free (files);
3146       files = &minus;
3147     }
3148
3149   if (checkonly)
3150     {
3151       if (nfiles > 1)
3152         error (SORT_FAILURE, 0, _("extra operand %s not allowed with -%c"),
3153                quote (files[1]), checkonly);
3154
3155       if (outfile)
3156         {
3157           static char opts[] = {0, 'o', 0};
3158           opts[0] = checkonly;
3159           incompatible_options (opts);
3160         }
3161
3162       /* POSIX requires that sort return 1 IFF invoked with -c or -C and the
3163          input is not properly sorted.  */
3164       exit (check (files[0], checkonly) ? EXIT_SUCCESS : SORT_OUT_OF_ORDER);
3165     }
3166
3167   if (mergeonly)
3168     {
3169       struct sortfile *sortfiles = xcalloc (nfiles, sizeof *sortfiles);
3170       size_t i;
3171
3172       for (i = 0; i < nfiles; ++i)
3173         sortfiles[i].name = files[i];
3174
3175       merge (sortfiles, 0, nfiles, outfile);
3176     }
3177   else
3178     sort (files, nfiles, outfile);
3179
3180   if (have_read_stdin && fclose (stdin) == EOF)
3181     die (_("close failed"), "-");
3182
3183   exit (EXIT_SUCCESS);
3184 }