(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / sort.c
blob4bfa853c2e3efeed5e8417004244aeb4d404416b
1 /* sort - sort lines of text (with all kinds of options).
2 Copyright (C) 88, 1991-1999 Free Software Foundation, Inc.
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.
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.
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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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.
22 Ørn E. Hansen added NLS support in 1997. */
24 #include <config.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include "system.h"
31 #include "long-options.h"
32 #include "error.h"
33 #include "hard-locale.h"
34 #include "memcoll.h"
35 #include "xalloc.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "sort"
40 #define AUTHORS "Mike Haertel"
42 #if defined ENABLE_NLS && HAVE_LANGINFO_H
43 # include <langinfo.h>
44 #endif
46 #if HAVE_PATHCONF && defined _PC_NAME_MAX
47 # define NAME_MAX_IN_DIR(Dir) pathconf (Dir, _PC_NAME_MAX)
48 #else
49 # define NAME_MAX_IN_DIR(Dir) 255
50 #endif
52 #ifndef STDC_HEADERS
53 double strtod ();
54 #endif
56 /* Undefine, to avoid warning about redefinition on some systems. */
57 #undef min
58 #define min(a, b) ((a) < (b) ? (a) : (b))
59 #undef max
60 #define max(a, b) ((a) > (b) ? (a) : (b))
62 #define UCHAR_LIM (UCHAR_MAX + 1)
63 #define UCHAR(c) ((unsigned char) (c))
65 #ifndef DEFAULT_TMPDIR
66 # define DEFAULT_TMPDIR "/tmp"
67 #endif
69 /* Use this as exit status in case of error, not EXIT_FAILURE. This
70 is necessary because EXIT_FAILURE is usually 1 and POSIX requires
71 that sort exit with status 1 IFF invoked with -c and the input is
72 not properly sorted. Any other irregular exit must exit with a
73 status code greater than 1. */
74 #define SORT_FAILURE 2
76 #define C_DECIMAL_POINT '.'
77 #define NEGATION_SIGN '-'
78 #define NUMERIC_ZERO '0'
80 #ifdef ENABLE_NLS
82 static char decimal_point;
83 static int th_sep; /* if CHAR_MAX + 1, then there is no thousands separator */
85 /* Nonzero if the corresponding locales are hard. */
86 static int hard_LC_COLLATE;
87 static int hard_LC_CTYPE;
88 # if HAVE_NL_LANGINFO
89 static int hard_LC_TIME;
90 # endif
92 # define IS_THOUSANDS_SEP(x) ((x) == th_sep)
94 #else
96 # define decimal_point C_DECIMAL_POINT
97 # define IS_THOUSANDS_SEP(x) 0
99 #endif
101 /* The kind of blanks for '-b' to skip in various options. */
102 enum blanktype { bl_start, bl_end, bl_both };
104 /* The character marking end of line. Default to \n. */
105 int eolchar = '\n';
107 /* Lines are held in core as counted strings. */
108 struct line
110 char *text; /* Text of the line. */
111 int length; /* Length including final newline. */
112 char *keybeg; /* Start of first key. */
113 char *keylim; /* Limit of first key. */
116 /* Arrays of lines. */
117 struct lines
119 struct line *lines; /* Dynamically allocated array of lines. */
120 int used; /* Number of slots used. */
121 int alloc; /* Number of slots allocated. */
122 int limit; /* Max number of slots to allocate. */
125 /* Input buffers. */
126 struct buffer
128 char *buf; /* Dynamically allocated buffer. */
129 int used; /* Number of bytes used. */
130 int alloc; /* Number of bytes allocated. */
131 int left; /* Number of bytes left after line parsing. */
134 struct keyfield
136 int sword; /* Zero-origin 'word' to start at. */
137 int schar; /* Additional characters to skip. */
138 int skipsblanks; /* Skip leading white space at start. */
139 int eword; /* Zero-origin first word after field. */
140 int echar; /* Additional characters in field. */
141 int skipeblanks; /* Skip trailing white space at finish. */
142 int *ignore; /* Boolean array of characters to ignore. */
143 char *translate; /* Translation applied to characters. */
144 int numeric; /* Flag for numeric comparison. Handle
145 strings of digits with optional decimal
146 point, but no exponential notation. */
147 int general_numeric; /* Flag for general, numeric comparison.
148 Handle numbers in exponential notation. */
149 int month; /* Flag for comparison by month name. */
150 int reverse; /* Reverse the sense of comparison. */
151 struct keyfield *next; /* Next keyfield to try. */
154 struct month
156 char *name;
157 int val;
160 /* The name this program was run with. */
161 char *program_name;
163 /* Table of white space. */
164 static int blanks[UCHAR_LIM];
166 /* Table of non-printing characters. */
167 static int nonprinting[UCHAR_LIM];
169 /* Table of non-dictionary characters (not letters, digits, or blanks). */
170 static int nondictionary[UCHAR_LIM];
172 /* Translation table folding lower case to upper.
173 FIXME: This doesn't work with multibyte character sets. */
174 static char fold_toupper[UCHAR_LIM];
176 #define MONTHS_PER_YEAR 12
178 #if defined ENABLE_NLS && HAVE_NL_LANGINFO
179 # define MONTHTAB_CONST /* empty */
180 #else
181 # define MONTHTAB_CONST const
182 #endif
184 /* Table mapping month names to integers.
185 Alphabetic order allows binary search. */
186 static MONTHTAB_CONST struct month monthtab[] =
188 {"APR", 4},
189 {"AUG", 8},
190 {"DEC", 12},
191 {"FEB", 2},
192 {"JAN", 1},
193 {"JUL", 7},
194 {"JUN", 6},
195 {"MAR", 3},
196 {"MAY", 5},
197 {"NOV", 11},
198 {"OCT", 10},
199 {"SEP", 9}
202 /* During the merge phase, the number of files to merge at once. */
203 #define NMERGE 16
205 /* Initial buffer size for in-core sorting. The buffer will grow only
206 if a line longer than this is seen. */
207 #define SORTALLOC (8 * 1024 * 1024)
208 static int const sortalloc = SORTALLOC;
210 /* Initial buffer size for in core merge buffers. Bear in mind that
211 up to NMERGE * mergealloc bytes may be allocated for merge buffers. */
212 static int const mergealloc = SORTALLOC / NMERGE / 2;
214 /* Guess of average line length. */
215 static int const linelength = 30;
217 /* Maximum number of elements for the array(s) of struct line's, in bytes. */
218 #define LINEALLOC (SORTALLOC / 2)
220 /* Directory in which any temporary files are to be created. */
221 static char *temp_dir;
223 /* Flag to reverse the order of all comparisons. */
224 static int reverse;
226 /* Flag for stable sort. This turns off the last ditch bytewise
227 comparison of lines, and instead leaves lines in the same order
228 they were read if all keys compare equal. */
229 static int stable;
231 /* Tab character separating fields. If NUL, then fields are separated
232 by the empty string between a non-whitespace character and a whitespace
233 character. */
234 static char tab;
236 /* Flag to remove consecutive duplicate lines from the output.
237 Only the last of a sequence of equal lines will be output. */
238 static int unique;
240 /* Nonzero if any of the input files are the standard input. */
241 static int have_read_stdin;
243 /* Lists of key field comparisons to be tried. */
244 static struct keyfield keyhead;
246 void
247 usage (int status)
249 if (status != 0)
250 fprintf (stderr, _("Try `%s --help' for more information.\n"),
251 program_name);
252 else
254 printf (_("\
255 Usage: %s [OPTION]... [FILE]...\n\
257 program_name);
258 printf (_("\
259 Write sorted concatenation of all FILE(s) to standard output.\n\
261 +POS1 [-POS2] start a key at POS1, end it *before* POS2 (obsolescent)\n\
262 field numbers and character offsets are numbered\n\
263 starting with zero (contrast with the -k option)\n\
264 -b ignore leading blanks in sort fields or keys\n\
265 -c check if given files already sorted, do not sort\n\
266 -d consider only [a-zA-Z0-9 ] characters in keys\n\
267 -f fold lower case to upper case characters in keys\n\
268 -g compare according to general numerical value, imply -b\n\
269 -i consider only [\\040-\\0176] characters in keys\n\
270 -k POS1[,POS2] start a key at POS1, end it *at* POS2\n\
271 field numbers and character offsets are numbered\n\
272 starting with one (contrast with zero-based +POS form)\n\
273 -m merge already sorted files, do not sort\n\
274 -M compare (unknown) < `JAN' < ... < `DEC', imply -b\n\
275 -n compare according to string numerical value, imply -b\n\
276 -o FILE write result on FILE instead of standard output\n\
277 -r reverse the result of comparisons\n\
278 -s stabilize sort by disabling last resort comparison\n\
279 -t SEP use SEParator instead of non- to whitespace transition\n\
280 -T DIRECTORY use DIRECTORY for temporary files, not $TMPDIR or %s\n\
281 -u with -c, check for strict ordering;\n\
282 with -m, only output the first of an equal sequence\n\
283 -z end lines with 0 byte, not newline, for find -print0\n\
284 --help display this help and exit\n\
285 --version output version information and exit\n\
288 DEFAULT_TMPDIR);
289 printf (_("\
290 POS is F[.C][OPTS], where F is the field number and C the character position\n\
291 in the field, both counted from one with -k, from zero with the obsolescent\n\
292 form. OPTS is made up of one or more of Mbdfinr; this effectively disables\n\
293 global -Mbdfinr settings for that key. If no key is given, use the entire\n\
294 line as the key. With no FILE, or when FILE is -, read standard input.\n\
297 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
299 /* Don't use EXIT_FAILURE here in case it is defined to be 1.
300 POSIX requires that sort return 1 IFF invoked with -c and
301 the input is not properly sorted. */
302 assert (status == 0 || status == SORT_FAILURE);
303 exit (status);
306 /* The list of temporary files. */
307 static struct tempnode
309 char *name;
310 struct tempnode *next;
311 } temphead;
313 /* Clean up any remaining temporary files. */
315 static void
316 cleanup (void)
318 struct tempnode *node;
320 for (node = temphead.next; node; node = node->next)
321 unlink (node->name);
324 static FILE *
325 xtmpfopen (const char *file)
327 FILE *fp;
328 int fd;
330 /* Open temporary file exclusively, to foil a common
331 denial-of-service attack. */
332 fd = open (file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
333 if (fd < 0 || (fp = fdopen (fd, "w")) == NULL)
335 error (0, errno, "%s", file);
336 cleanup ();
337 exit (SORT_FAILURE);
340 return fp;
343 static FILE *
344 xfopen (const char *file, const char *how)
346 FILE *fp;
348 if (STREQ (file, "-"))
350 fp = stdin;
352 else
354 if ((fp = fopen (file, how)) == NULL)
356 error (0, errno, "%s", file);
357 cleanup ();
358 exit (SORT_FAILURE);
362 if (fp == stdin)
363 have_read_stdin = 1;
364 return fp;
367 static void
368 xfclose (FILE *fp)
370 if (fp == stdin)
372 /* Allow reading stdin from tty more than once. */
373 if (feof (fp))
374 clearerr (fp);
376 else if (fp == stdout)
378 if (fflush (fp) != 0)
380 error (0, errno, _("flushing file"));
381 cleanup ();
382 exit (SORT_FAILURE);
385 else
387 if (fclose (fp) != 0)
389 error (0, errno, _("error closing file"));
390 cleanup ();
391 exit (SORT_FAILURE);
396 static void
397 write_bytes (const char *buf, size_t n_bytes, FILE *fp, const char *output_file)
399 if (fwrite (buf, 1, n_bytes, fp) != n_bytes)
401 error (0, errno, _("%s: write error"), output_file);
402 cleanup ();
403 exit (SORT_FAILURE);
407 /* Return a name for a temporary file. */
409 static char *
410 tempname (void)
412 static unsigned int seq;
413 int len = strlen (temp_dir);
414 char *name = xmalloc (len + 1 + sizeof ("sort") - 1 + 5 + 5 + 1);
415 int long_file_names = NAME_MAX_IN_DIR (temp_dir) > 12;
416 struct tempnode *node;
418 /* If long filenames aren't supported, we cannot use filenames
419 longer than 8+3 and still assume they are unique. */
420 if (long_file_names)
421 sprintf (name,
422 "%s%ssort%5.5d%5.5d",
423 temp_dir,
424 (len && temp_dir[len - 1] != '/') ? "/" : "",
425 (unsigned int) getpid () & 0xffff, seq);
426 else
427 sprintf (name, "%s%ss%5.5d%2.2d.%3.3d",
428 temp_dir,
429 (len && temp_dir[len - 1] != '/') ? "/" : "",
430 (unsigned int) getpid () & 0xffff, seq / 1000, seq % 1000);
432 ++seq;
434 /* Make sure that SEQ's value fits in 5 digits if temp_dir is on
435 an 8.3 filesystem. */
436 if (!long_file_names && seq >= 100000)
437 seq = 0;
439 node = (struct tempnode *) xmalloc (sizeof (struct tempnode));
440 node->name = name;
441 node->next = temphead.next;
442 temphead.next = node;
443 return name;
446 /* Search through the list of temporary files for NAME;
447 remove it if it is found on the list. */
449 static void
450 zaptemp (const char *name)
452 struct tempnode *node, *temp;
454 for (node = &temphead; node->next; node = node->next)
455 if (STREQ (name, node->next->name))
456 break;
457 if (node->next)
459 temp = node->next;
460 unlink (temp->name);
461 free (temp->name);
462 node->next = temp->next;
463 free ((char *) temp);
467 #ifdef ENABLE_NLS
469 static int
470 struct_month_cmp (const void *m1, const void *m2)
472 return strcmp (((const struct month *) m1)->name,
473 ((const struct month *) m2)->name);
476 #endif /* NLS */
478 /* Initialize the character class tables. */
480 static void
481 inittables (void)
483 int i;
485 for (i = 0; i < UCHAR_LIM; ++i)
487 if (ISBLANK (i))
488 blanks[i] = 1;
489 if (!ISPRINT (i))
490 nonprinting[i] = 1;
491 if (!ISALNUM (i) && !ISBLANK (i))
492 nondictionary[i] = 1;
493 if (ISLOWER (i))
494 fold_toupper[i] = toupper (i);
495 else
496 fold_toupper[i] = i;
499 #if defined ENABLE_NLS && HAVE_NL_LANGINFO
500 /* If we're not in the "C" locale, read different names for months. */
501 if (hard_LC_TIME)
503 for (i = 0; i < MONTHS_PER_YEAR; i++)
505 char *s;
506 size_t s_len;
507 size_t j;
508 char *name;
510 s = (char *) nl_langinfo (ABMON_1 + i);
511 s_len = strlen (s);
512 monthtab[i].name = name = (char *) xmalloc (s_len + 1);
513 monthtab[i].val = i + 1;
515 for (j = 0; j < s_len; j++)
516 name[j] = fold_toupper[UCHAR (s[j])];
517 name[j] = '\0';
519 qsort ((void *) monthtab, MONTHS_PER_YEAR,
520 sizeof (struct month), struct_month_cmp);
522 #endif /* NLS */
525 /* Initialize BUF, allocating ALLOC bytes initially. */
527 static void
528 initbuf (struct buffer *buf, int alloc)
530 buf->alloc = alloc;
531 buf->buf = xmalloc (buf->alloc);
532 buf->used = buf->left = 0;
535 /* Fill BUF reading from FP, moving buf->left bytes from the end
536 of buf->buf to the beginning first. If EOF is reached and the
537 file wasn't terminated by a newline, supply one. Return a count
538 of bytes buffered. */
540 static int
541 fillbuf (struct buffer *buf, FILE *fp)
543 int cc;
545 memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
546 buf->used = buf->left;
548 while (!feof (fp) && (buf->used == 0
549 || !memchr (buf->buf, eolchar, buf->used)))
551 if (buf->used == buf->alloc)
553 buf->alloc *= 2;
554 buf->buf = xrealloc (buf->buf, buf->alloc);
556 cc = fread (buf->buf + buf->used, 1, buf->alloc - buf->used, fp);
557 if (ferror (fp))
559 error (0, errno, _("read error"));
560 cleanup ();
561 exit (SORT_FAILURE);
563 buf->used += cc;
566 if (feof (fp) && buf->used && buf->buf[buf->used - 1] != eolchar)
568 if (buf->used == buf->alloc)
570 buf->alloc *= 2;
571 buf->buf = xrealloc (buf->buf, buf->alloc);
573 buf->buf[buf->used++] = eolchar;
576 return buf->used;
579 /* Initialize LINES, allocating space for ALLOC lines initially.
580 LIMIT is the maximum possible number of lines to allocate space
581 for, ever. */
583 static void
584 initlines (struct lines *lines, int alloc, int limit)
586 lines->alloc = alloc;
587 lines->lines = (struct line *) xmalloc (lines->alloc * sizeof (struct line));
588 lines->used = 0;
589 lines->limit = limit;
592 /* Return a pointer to the first character of the field specified
593 by KEY in LINE. */
595 static char *
596 begfield (const struct line *line, const struct keyfield *key)
598 register char *ptr = line->text, *lim = ptr + line->length - 1;
599 register int sword = key->sword, schar = key->schar;
601 if (tab)
602 while (ptr < lim && sword--)
604 while (ptr < lim && *ptr != tab)
605 ++ptr;
606 if (ptr < lim)
607 ++ptr;
609 else
610 while (ptr < lim && sword--)
612 while (ptr < lim && blanks[UCHAR (*ptr)])
613 ++ptr;
614 while (ptr < lim && !blanks[UCHAR (*ptr)])
615 ++ptr;
618 if (key->skipsblanks)
619 while (ptr < lim && blanks[UCHAR (*ptr)])
620 ++ptr;
622 if (ptr + schar <= lim)
623 ptr += schar;
624 else
625 ptr = lim;
627 return ptr;
630 /* Return the limit of (a pointer to the first character after) the field
631 in LINE specified by KEY. */
633 static char *
634 limfield (const struct line *line, const struct keyfield *key)
636 register char *ptr = line->text, *lim = ptr + line->length - 1;
637 register int eword = key->eword, echar = key->echar;
639 /* Note: from the POSIX spec:
640 The leading field separator itself is included in
641 a field when -t is not used. FIXME: move this comment up... */
643 /* Move PTR past EWORD fields or to one past the last byte on LINE,
644 whichever comes first. If there are more than EWORD fields, leave
645 PTR pointing at the beginning of the field having zero-based index,
646 EWORD. If a delimiter character was specified (via -t), then that
647 `beginning' is the first character following the delimiting TAB.
648 Otherwise, leave PTR pointing at the first `blank' character after
649 the preceding field. */
650 if (tab)
651 while (ptr < lim && eword--)
653 while (ptr < lim && *ptr != tab)
654 ++ptr;
655 if (ptr < lim && (eword || echar > 0))
656 ++ptr;
658 else
659 while (ptr < lim && eword--)
661 while (ptr < lim && blanks[UCHAR (*ptr)])
662 ++ptr;
663 while (ptr < lim && !blanks[UCHAR (*ptr)])
664 ++ptr;
667 #ifdef POSIX_UNSPECIFIED
668 /* The following block of code makes GNU sort incompatible with
669 standard Unix sort, so it's ifdef'd out for now.
670 The POSIX spec isn't clear on how to interpret this.
671 FIXME: request clarification.
673 From: kwzh@gnu.ai.mit.edu (Karl Heuer)
674 Date: Thu, 30 May 96 12:20:41 -0400
676 [...]I believe I've found another bug in `sort'.
678 $ cat /tmp/sort.in
679 a b c 2 d
680 pq rs 1 t
681 $ textutils-1.15/src/sort +0.6 -0.7 </tmp/sort.in
682 a b c 2 d
683 pq rs 1 t
684 $ /bin/sort +0.6 -0.7 </tmp/sort.in
685 pq rs 1 t
686 a b c 2 d
688 Unix sort produced the answer I expected: sort on the single character
689 in column 6. GNU sort produced different results, because it disagrees
690 on the interpretation of the key-end spec "-M.N". Unix sort reads this
691 as "skip M fields, then N characters"; but GNU sort wants it to mean
692 "skip M fields, then either N characters or the rest of the current
693 field, whichever comes first". This extra clause applies only to
694 key-ends, not key-starts.
697 /* Make LIM point to the end of (one byte past) the current field. */
698 if (tab)
700 char *newlim;
701 newlim = memchr (ptr, tab, lim - ptr);
702 if (newlim)
703 lim = newlim;
705 else
707 char *newlim;
708 newlim = ptr;
709 while (newlim < lim && blanks[UCHAR (*newlim)])
710 ++newlim;
711 while (newlim < lim && !blanks[UCHAR (*newlim)])
712 ++newlim;
713 lim = newlim;
715 #endif
717 /* If we're skipping leading blanks, don't start counting characters
718 until after skipping past any leading blanks. */
719 if (key->skipsblanks)
720 while (ptr < lim && blanks[UCHAR (*ptr)])
721 ++ptr;
723 /* Advance PTR by ECHAR (if possible), but no further than LIM. */
724 if (ptr + echar <= lim)
725 ptr += echar;
726 else
727 ptr = lim;
729 return ptr;
732 /* FIXME */
734 void
735 trim_trailing_blanks (const char *a_start, char **a_end)
737 while (*a_end > a_start && blanks[UCHAR (*(*a_end - 1))])
738 --(*a_end);
741 /* Find the lines in BUF, storing pointers and lengths in LINES. */
743 static void
744 findlines (struct buffer *buf, struct lines *lines)
746 register char *beg = buf->buf, *lim = buf->buf + buf->used, *ptr;
747 struct keyfield *key = keyhead.next;
749 lines->used = 0;
751 while (beg < lim && (ptr = memchr (beg, eolchar, lim - beg))
752 && lines->used < lines->limit)
754 if (lines->used == lines->alloc)
756 lines->alloc *= 2;
757 lines->lines = (struct line *)
758 xrealloc ((char *) lines->lines,
759 lines->alloc * sizeof (struct line));
762 lines->lines[lines->used].text = beg;
763 lines->lines[lines->used].length = ptr + 1 - beg;
765 /* Precompute the position of the first key for efficiency. */
766 if (key)
768 if (key->eword >= 0)
769 lines->lines[lines->used].keylim =
770 limfield (&lines->lines[lines->used], key);
771 else
772 lines->lines[lines->used].keylim = ptr;
774 if (key->sword >= 0)
775 lines->lines[lines->used].keybeg =
776 begfield (&lines->lines[lines->used], key);
777 else
779 if (key->skipsblanks)
780 while (blanks[UCHAR (*beg)])
781 ++beg;
782 lines->lines[lines->used].keybeg = beg;
784 if (key->skipeblanks)
786 trim_trailing_blanks (lines->lines[lines->used].keybeg,
787 &lines->lines[lines->used].keylim);
790 else
792 lines->lines[lines->used].keybeg = 0;
793 lines->lines[lines->used].keylim = 0;
796 ++lines->used;
797 beg = ptr + 1;
800 buf->left = lim - beg;
803 /* Compare strings A and B containing decimal fractions < 1. Each string
804 should begin with a decimal point followed immediately by the digits
805 of the fraction. Strings not of this form are considered to be zero. */
807 /* The goal here, is to take two numbers a and b... compare these
808 in parallel. Instead of converting each, and then comparing the
809 outcome. Most likely stopping the comparison before the conversion
810 is complete. The algorithm used, in the old sort:
812 Algorithm: fraccompare
813 Action : compare two decimal fractions
814 accepts : char *a, char *b
815 returns : -1 if a<b, 0 if a=b, 1 if a>b.
816 implement:
818 if *a == decimal_point AND *b == decimal_point
819 find first character different in a and b.
820 if both are digits, return the difference *a - *b.
821 if *a is a digit
822 skip past zeros
823 if digit return 1, else 0
824 if *b is a digit
825 skip past zeros
826 if digit return -1, else 0
827 if *a is a decimal_point
828 skip past decimal_point and zeros
829 if digit return 1, else 0
830 if *b is a decimal_point
831 skip past decimal_point and zeros
832 if digit return -1, else 0
833 return 0 */
835 static int
836 fraccompare (register const char *a, register const char *b)
838 if (*a == decimal_point && *b == decimal_point)
840 while (*++a == *++b)
841 if (! ISDIGIT (*a))
842 return 0;
843 if (ISDIGIT (*a) && ISDIGIT (*b))
844 return *a - *b;
845 if (ISDIGIT (*a))
846 goto a_trailing_nonzero;
847 if (ISDIGIT (*b))
848 goto b_trailing_nonzero;
849 return 0;
851 else if (*a++ == decimal_point)
853 a_trailing_nonzero:
854 while (*a == NUMERIC_ZERO)
855 a++;
856 return ISDIGIT (*a);
858 else if (*b++ == decimal_point)
860 b_trailing_nonzero:
861 while (*b == NUMERIC_ZERO)
862 b++;
863 return - ISDIGIT (*b);
865 return 0;
868 /* Compare strings A and B as numbers without explicitly converting them to
869 machine numbers. Comparatively slow for short strings, but asymptotically
870 hideously fast. */
872 static int
873 numcompare (register const char *a, register const char *b)
875 register int tmpa, tmpb, loga, logb, tmp;
877 tmpa = *a;
878 tmpb = *b;
880 while (blanks[UCHAR (tmpa)])
881 tmpa = *++a;
882 while (blanks[UCHAR (tmpb)])
883 tmpb = *++b;
885 if (tmpa == NEGATION_SIGN)
888 tmpa = *++a;
889 while (tmpa == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpa));
890 if (tmpb != NEGATION_SIGN)
892 if (tmpa == decimal_point)
894 tmpa = *++a;
895 while (tmpa == NUMERIC_ZERO);
896 if (ISDIGIT (tmpa))
897 return -1;
898 while (tmpb == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpb))
899 tmpb = *++b;
900 if (tmpb == decimal_point)
902 tmpb = *++b;
903 while (tmpb == NUMERIC_ZERO);
904 if (ISDIGIT (tmpb))
905 return -1;
906 return 0;
909 tmpb = *++b;
910 while (tmpb == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpb));
912 while (tmpa == tmpb && ISDIGIT (tmpa))
915 tmpa = *++a;
916 while (IS_THOUSANDS_SEP (tmpa));
918 tmpb = *++b;
919 while (IS_THOUSANDS_SEP (tmpb));
922 if ((tmpa == decimal_point && !ISDIGIT (tmpb))
923 || (tmpb == decimal_point && !ISDIGIT (tmpa)))
924 return -fraccompare (a, b);
926 tmp = tmpb - tmpa;
928 for (loga = 0; ISDIGIT (tmpa); ++loga)
930 tmpa = *++a;
931 while (IS_THOUSANDS_SEP (tmpa));
933 for (logb = 0; ISDIGIT (tmpb); ++logb)
935 tmpb = *++b;
936 while (IS_THOUSANDS_SEP (tmpb));
938 if (logb - loga != 0)
939 return logb - loga;
941 if (!loga)
942 return 0;
944 return tmp;
946 else if (tmpb == NEGATION_SIGN)
949 tmpb = *++b;
950 while (tmpb == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpb));
951 if (tmpb == decimal_point)
953 tmpb = *++b;
954 while (tmpb == NUMERIC_ZERO);
955 if (ISDIGIT (tmpb))
956 return 1;
957 while (tmpa == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpa))
958 tmpa = *++a;
959 if (tmpa == decimal_point)
961 tmpa = *++a;
962 while (tmpa == NUMERIC_ZERO);
963 if (ISDIGIT (tmpa))
964 return 1;
965 return 0;
967 else
969 while (tmpa == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpa))
970 tmpa = *++a;
971 while (tmpb == NUMERIC_ZERO || IS_THOUSANDS_SEP (tmpb))
972 tmpb = *++b;
974 while (tmpa == tmpb && ISDIGIT (tmpa))
977 tmpa = *++a;
978 while (IS_THOUSANDS_SEP (tmpa));
980 tmpb = *++b;
981 while (IS_THOUSANDS_SEP (tmpb));
984 if ((tmpa == decimal_point && !ISDIGIT (tmpb))
985 || (tmpb == decimal_point && !ISDIGIT (tmpa)))
986 return fraccompare (a, b);
988 tmp = tmpa - tmpb;
990 for (loga = 0; ISDIGIT (tmpa); ++loga)
992 tmpa = *++a;
993 while (IS_THOUSANDS_SEP (tmpa));
995 for (logb = 0; ISDIGIT (tmpb); ++logb)
997 tmpb = *++b;
998 while (IS_THOUSANDS_SEP (tmpb));
1000 if (loga - logb != 0)
1001 return loga - logb;
1003 if (!loga)
1004 return 0;
1006 return tmp;
1010 static int
1011 general_numcompare (const char *sa, const char *sb)
1013 /* FIXME: add option to warn about failed conversions. */
1014 /* FIXME: maybe add option to try expensive FP conversion
1015 only if A and B can't be compared more cheaply/accurately. */
1017 char *ea;
1018 char *eb;
1019 double a = strtod (sa, &ea);
1020 double b = strtod (sb, &eb);
1022 /* Put conversion errors at the start of the collating sequence. */
1023 if (sa == ea)
1024 return sb == eb ? 0 : -1;
1025 if (sb == eb)
1026 return 1;
1028 /* Sort numbers in the usual way, where -0 == +0. Put NaNs after
1029 conversion errors but before numbers; sort them by internal
1030 bit-pattern, for lack of a more portable alternative. */
1031 return (a < b ? -1
1032 : a > b ? 1
1033 : a == b ? 0
1034 : b == b ? -1
1035 : a == a ? 1
1036 : memcmp ((char *) &a, (char *) &b, sizeof a));
1039 /* Return an integer in 1..12 of the month name S with length LEN.
1040 Return 0 if the name in S is not recognized. */
1042 static int
1043 getmonth (const char *s, int len)
1045 char *month;
1046 register int i, lo = 0, hi = MONTHS_PER_YEAR, result;
1048 while (len > 0 && blanks[UCHAR (*s)])
1050 ++s;
1051 --len;
1054 if (len == 0)
1055 return 0;
1057 month = (char *) alloca (len + 1);
1058 for (i = 0; i < len; ++i)
1059 month[i] = fold_toupper[UCHAR (s[i])];
1060 while (blanks[UCHAR (month[i - 1])])
1061 --i;
1062 month[i] = '\0';
1066 int ix = (lo + hi) / 2;
1068 if (strncmp (month, monthtab[ix].name, strlen (monthtab[ix].name)) < 0)
1069 hi = ix;
1070 else
1071 lo = ix;
1073 while (hi - lo > 1);
1075 result = (!strncmp (month, monthtab[lo].name, strlen (monthtab[lo].name))
1076 ? monthtab[lo].val : 0);
1078 return result;
1081 /* Compare two lines A and B trying every key in sequence until there
1082 are no more keys or a difference is found. */
1084 static int
1085 keycompare (const struct line *a, const struct line *b)
1087 register char *texta, *textb, *lima, *limb;
1088 register unsigned char *translate;
1089 register int *ignore;
1090 struct keyfield *key;
1091 int diff = 0, iter = 0, lena, lenb;
1093 for (key = keyhead.next; key; key = key->next, ++iter)
1095 int comparable_lengths = 1;
1097 ignore = key->ignore;
1098 translate = (unsigned char *) key->translate;
1100 /* Find the beginning and limit of each field. */
1101 if (iter || a->keybeg == NULL || b->keybeg == NULL)
1103 if (key->eword >= 0)
1104 lima = limfield (a, key), limb = limfield (b, key);
1105 else
1106 lima = a->text + a->length - 1, limb = b->text + b->length - 1;
1108 if (key->sword >= 0)
1109 texta = begfield (a, key), textb = begfield (b, key);
1110 else
1112 texta = a->text, textb = b->text;
1113 if (key->skipsblanks)
1115 while (texta < lima && blanks[UCHAR (*texta)])
1116 ++texta;
1117 while (textb < limb && blanks[UCHAR (*textb)])
1118 ++textb;
1122 else
1124 /* For the first iteration only, the key positions have
1125 been precomputed for us. */
1126 texta = a->keybeg, lima = a->keylim;
1127 textb = b->keybeg, limb = b->keylim;
1130 /* Find the lengths. */
1131 lena = lima - texta, lenb = limb - textb;
1132 if (lena < 0)
1133 lena = 0;
1134 if (lenb < 0)
1135 lenb = 0;
1137 if (key->skipeblanks)
1139 char *a_end = texta + lena;
1140 char *b_end = textb + lenb;
1141 trim_trailing_blanks (texta, &a_end);
1142 trim_trailing_blanks (textb, &b_end);
1143 lena = a_end - texta;
1144 lenb = b_end - textb;
1147 /* Actually compare the fields. */
1148 if (key->numeric | key->general_numeric)
1150 char savea = *lima, saveb = *limb;
1152 *lima = *limb = '\0';
1153 diff = ((key->numeric ? numcompare : general_numcompare)
1154 (texta, textb));
1155 *lima = savea, *limb = saveb;
1156 if (diff)
1157 return key->reverse ? -diff : diff;
1158 continue;
1160 else if (key->month)
1162 diff = getmonth (texta, lena) - getmonth (textb, lenb);
1163 if (diff)
1164 return key->reverse ? -diff : diff;
1165 continue;
1167 #ifdef ENABLE_NLS
1169 /* Sorting like this may become slow, so in a simple locale the user
1170 can select a faster sort that is similar to ascii sort */
1171 else if (hard_LC_COLLATE | hard_LC_CTYPE)
1173 if (ignore || translate)
1175 char *copy_a = (char *) alloca (lena + 1);
1176 char *copy_b = (char *) alloca (lenb + 1);
1177 int new_len_a, new_len_b, i;
1179 /* Ignore and/or translate chars before comparing. */
1180 for (new_len_a = new_len_b = i = 0; i < max (lena, lenb); i++)
1182 if (i < lena)
1184 copy_a[new_len_a] = (translate
1185 ? translate[UCHAR (texta[i])]
1186 : texta[i]);
1187 if (!ignore || !ignore[UCHAR (texta[i])])
1188 ++new_len_a;
1190 if (i < lenb)
1192 copy_b[new_len_b] = (translate
1193 ? translate[UCHAR (textb[i])]
1194 : textb [i]);
1195 if (!ignore || !ignore[UCHAR (textb[i])])
1196 ++new_len_b;
1200 diff = memcoll (copy_a, new_len_a, copy_b, new_len_b);
1202 else
1204 diff = memcoll (texta, lena, textb, lenb);
1207 if (diff)
1208 return key->reverse ? -diff : diff;
1210 continue;
1212 #endif
1213 else if (ignore && translate)
1215 #define CMP_WITH_IGNORE(A, B) \
1216 do \
1218 while (texta < lima && textb < limb) \
1220 while (texta < lima && ignore[UCHAR (*texta)]) \
1221 ++texta; \
1222 while (textb < limb && ignore[UCHAR (*textb)]) \
1223 ++textb; \
1224 if (texta < lima && textb < limb) \
1226 if ((A) != (B)) \
1228 diff = UCHAR (A) - UCHAR (B); \
1229 break; \
1231 ++texta; \
1232 ++textb; \
1235 if (texta == lima && textb < limb && !ignore[UCHAR (*textb)]) \
1236 diff = -1; \
1237 else if (texta < lima && textb == limb \
1238 && !ignore[UCHAR (*texta)]) \
1239 diff = 1; \
1242 if (diff == 0) \
1244 while (texta < lima && ignore[UCHAR (*texta)]) \
1245 ++texta; \
1246 while (textb < limb && ignore[UCHAR (*textb)]) \
1247 ++textb; \
1249 if (texta == lima && textb < limb) \
1250 diff = -1; \
1251 else if (texta < lima && textb == limb) \
1252 diff = 1; \
1254 /* Relative lengths are meaningless if characters were ignored. \
1255 Handling this case here avoids what might be an invalid length \
1256 comparison below. */ \
1257 if (diff == 0 && texta == lima && textb == limb) \
1258 comparable_lengths = 0; \
1260 while (0)
1262 CMP_WITH_IGNORE (translate[UCHAR (*texta)], translate[UCHAR (*textb)]);
1263 else if (ignore)
1264 CMP_WITH_IGNORE (UCHAR (*texta), UCHAR (*textb));
1265 else if (translate)
1266 while (texta < lima && textb < limb)
1268 if (translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
1270 diff = (UCHAR (translate[UCHAR (*--texta)])
1271 - UCHAR (translate[UCHAR (*--textb)]));
1272 break;
1275 else
1277 #ifdef ENABLE_NLS
1278 if (hard_LC_COLLATE)
1280 /* Ignore any length difference if the localized comparison
1281 says the strings are equal. */
1282 comparable_lengths = 0;
1283 diff = memcoll (texta, lena, textb, lenb);
1285 else
1286 #endif
1288 diff = memcmp (texta, textb, min (lena, lenb));
1292 if (diff)
1293 return key->reverse ? -diff : diff;
1294 if (comparable_lengths && (diff = lena - lenb) != 0)
1295 return key->reverse ? -diff : diff;
1298 return 0;
1301 /* Compare two lines A and B, returning negative, zero, or positive
1302 depending on whether A compares less than, equal to, or greater than B. */
1304 static int
1305 compare (register const struct line *a, register const struct line *b)
1307 int diff, alen, blen, minlen;
1309 /* First try to compare on the specified keys (if any).
1310 The only two cases with no key at all are unadorned sort,
1311 and unadorned sort -r. */
1312 if (keyhead.next)
1314 diff = keycompare (a, b);
1315 if (diff != 0 || unique || stable)
1317 alloca (0);
1318 return diff;
1322 /* If the keys all compare equal (or no keys were specified)
1323 fall through to the default byte-by-byte comparison. */
1324 alen = a->length - 1, blen = b->length - 1;
1326 #ifdef ENABLE_NLS
1327 if (hard_LC_COLLATE)
1329 diff = memcoll (a->text, alen, b->text, blen);
1330 alloca (0);
1331 return reverse ? -diff : diff;
1333 #endif
1335 minlen = min (alen, blen);
1336 if (minlen == 0
1337 || (! (diff = UCHAR (a->text[0]) - UCHAR (b->text[0]))
1338 && ! (diff = memcmp (a->text, b->text, minlen))))
1339 diff = alen - blen;
1341 return reverse ? -diff : diff;
1344 /* Check that the lines read from the given FP come in order. Print a
1345 diagnostic (FILE_NAME, line number, contents of line) to stderr and return
1346 the line number of the first out-of-order line (counting from 1) if they
1347 are not in order. Otherwise, print no diagnostic and return zero. */
1349 static int
1350 checkfp (FILE *fp, const char *file_name)
1352 struct buffer buf; /* Input buffer. */
1353 struct lines lines; /* Lines scanned from the buffer. */
1354 struct line temp; /* Copy of previous line. */
1355 int cc; /* Character count. */
1356 int alloc;
1357 int line_number = 1;
1358 struct line *disorder_line IF_LINT (= NULL);
1359 int disorder_line_number = 0;
1361 initbuf (&buf, mergealloc);
1362 initlines (&lines, mergealloc / linelength + 1,
1363 LINEALLOC / ((NMERGE + NMERGE) * sizeof (struct line)));
1364 alloc = linelength;
1365 temp.text = xmalloc (alloc);
1367 cc = fillbuf (&buf, fp);
1368 if (cc == 0)
1369 goto finish;
1371 findlines (&buf, &lines);
1373 while (1)
1375 struct line *prev_line; /* Pointer to previous line. */
1376 int cmp; /* Result of calling compare. */
1377 int i;
1379 /* Compare each line in the buffer with its successor. */
1380 for (i = 0; i < lines.used - 1; ++i)
1382 cmp = compare (&lines.lines[i], &lines.lines[i + 1]);
1383 if ((unique && cmp >= 0) || (cmp > 0))
1385 disorder_line = &lines.lines[i + 1];
1386 disorder_line_number = line_number + i + 1;
1387 goto finish;
1391 line_number += lines.used;
1393 /* Save the last line of the buffer and refill the buffer. */
1394 prev_line = lines.lines + (lines.used - 1);
1395 if (alloc < prev_line->length)
1399 alloc *= 2;
1401 while (alloc < prev_line->length);
1402 temp.text = xrealloc (temp.text, alloc);
1404 assert (prev_line->length <= alloc);
1405 memcpy (temp.text, prev_line->text, prev_line->length);
1406 temp.length = prev_line->length;
1407 temp.keybeg = temp.text + (prev_line->keybeg - prev_line->text);
1408 temp.keylim = temp.text + (prev_line->keylim - prev_line->text);
1410 cc = fillbuf (&buf, fp);
1411 if (cc == 0)
1412 break;
1414 findlines (&buf, &lines);
1415 /* Make sure the line saved from the old buffer contents is
1416 less than or equal to the first line of the new buffer. */
1417 cmp = compare (&temp, &lines.lines[0]);
1418 if ((unique && cmp >= 0) || (cmp > 0))
1420 disorder_line = &lines.lines[0];
1421 disorder_line_number = line_number;
1422 break;
1426 finish:
1427 xfclose (fp);
1429 if (disorder_line_number)
1431 fprintf (stderr, _("%s: %s:%d: disorder: "), program_name, file_name,
1432 disorder_line_number);
1433 write_bytes (disorder_line->text, disorder_line->length, stderr,
1434 _("standard error"));
1437 free (buf.buf);
1438 free ((char *) lines.lines);
1439 free (temp.text);
1440 return disorder_line_number;
1443 /* Merge lines from FPS onto OFP. NFPS cannot be greater than NMERGE.
1444 Close FPS before returning. */
1446 static void
1447 mergefps (FILE **fps, register int nfps, FILE *ofp, const char *output_file)
1449 struct buffer buffer[NMERGE]; /* Input buffers for each file. */
1450 struct lines lines[NMERGE]; /* Line tables for each buffer. */
1451 struct line saved; /* Saved line for unique check. */
1452 int savedflag = 0; /* True if there is a saved line. */
1453 int savealloc IF_LINT (= 0); /* Size allocated for the saved line. */
1454 int cur[NMERGE]; /* Current line in each line table. */
1455 int ord[NMERGE]; /* Table representing a permutation of fps,
1456 such that lines[ord[0]].lines[cur[ord[0]]]
1457 is the smallest line and will be next
1458 output. */
1459 register int i, j, t;
1461 /* Allocate space for a saved line if necessary. */
1462 if (unique)
1464 savealloc = linelength;
1465 saved.text = xmalloc (savealloc);
1468 /* Read initial lines from each input file. */
1469 for (i = 0; i < nfps; ++i)
1471 initbuf (&buffer[i], mergealloc);
1472 /* If a file is empty, eliminate it from future consideration. */
1473 while (i < nfps && !fillbuf (&buffer[i], fps[i]))
1475 xfclose (fps[i]);
1476 --nfps;
1477 for (j = i; j < nfps; ++j)
1478 fps[j] = fps[j + 1];
1480 if (i == nfps)
1481 free (buffer[i].buf);
1482 else
1484 initlines (&lines[i], mergealloc / linelength + 1,
1485 LINEALLOC / ((NMERGE + NMERGE) * sizeof (struct line)));
1486 findlines (&buffer[i], &lines[i]);
1487 cur[i] = 0;
1491 /* Set up the ord table according to comparisons among input lines.
1492 Since this only reorders two items if one is strictly greater than
1493 the other, it is stable. */
1494 for (i = 0; i < nfps; ++i)
1495 ord[i] = i;
1496 for (i = 1; i < nfps; ++i)
1497 if (compare (&lines[ord[i - 1]].lines[cur[ord[i - 1]]],
1498 &lines[ord[i]].lines[cur[ord[i]]]) > 0)
1499 t = ord[i - 1], ord[i - 1] = ord[i], ord[i] = t, i = 0;
1501 /* Repeatedly output the smallest line until no input remains. */
1502 while (nfps)
1504 /* If uniquified output is turned on, output only the first of
1505 an identical series of lines. */
1506 if (unique)
1508 if (savedflag && compare (&saved, &lines[ord[0]].lines[cur[ord[0]]]))
1510 write_bytes (saved.text, saved.length, ofp, output_file);
1511 savedflag = 0;
1513 if (!savedflag)
1515 if (savealloc < lines[ord[0]].lines[cur[ord[0]]].length)
1517 while (savealloc < lines[ord[0]].lines[cur[ord[0]]].length)
1518 savealloc *= 2;
1519 saved.text = xrealloc (saved.text, savealloc);
1521 saved.length = lines[ord[0]].lines[cur[ord[0]]].length;
1522 memcpy (saved.text, lines[ord[0]].lines[cur[ord[0]]].text,
1523 saved.length);
1524 if (lines[ord[0]].lines[cur[ord[0]]].keybeg != NULL)
1526 saved.keybeg = saved.text +
1527 (lines[ord[0]].lines[cur[ord[0]]].keybeg
1528 - lines[ord[0]].lines[cur[ord[0]]].text);
1530 if (lines[ord[0]].lines[cur[ord[0]]].keylim != NULL)
1532 saved.keylim = saved.text +
1533 (lines[ord[0]].lines[cur[ord[0]]].keylim
1534 - lines[ord[0]].lines[cur[ord[0]]].text);
1536 savedflag = 1;
1539 else
1540 write_bytes (lines[ord[0]].lines[cur[ord[0]]].text,
1541 lines[ord[0]].lines[cur[ord[0]]].length, ofp, output_file);
1543 /* Check if we need to read more lines into core. */
1544 if (++cur[ord[0]] == lines[ord[0]].used)
1546 if (fillbuf (&buffer[ord[0]], fps[ord[0]]))
1548 findlines (&buffer[ord[0]], &lines[ord[0]]);
1549 cur[ord[0]] = 0;
1551 else
1553 /* We reached EOF on fps[ord[0]]. */
1554 for (i = 1; i < nfps; ++i)
1555 if (ord[i] > ord[0])
1556 --ord[i];
1557 --nfps;
1558 xfclose (fps[ord[0]]);
1559 free (buffer[ord[0]].buf);
1560 free ((char *) lines[ord[0]].lines);
1561 for (i = ord[0]; i < nfps; ++i)
1563 fps[i] = fps[i + 1];
1564 buffer[i] = buffer[i + 1];
1565 lines[i] = lines[i + 1];
1566 cur[i] = cur[i + 1];
1568 for (i = 0; i < nfps; ++i)
1569 ord[i] = ord[i + 1];
1570 continue;
1574 /* The new line just read in may be larger than other lines
1575 already in core; push it back in the queue until we encounter
1576 a line larger than it. */
1577 for (i = 1; i < nfps; ++i)
1579 t = compare (&lines[ord[0]].lines[cur[ord[0]]],
1580 &lines[ord[i]].lines[cur[ord[i]]]);
1581 if (!t)
1582 t = ord[0] - ord[i];
1583 if (t < 0)
1584 break;
1586 t = ord[0];
1587 for (j = 1; j < i; ++j)
1588 ord[j - 1] = ord[j];
1589 ord[i - 1] = t;
1592 if (unique && savedflag)
1594 write_bytes (saved.text, saved.length, ofp, output_file);
1595 free (saved.text);
1599 /* Sort the array LINES with NLINES members, using TEMP for temporary space. */
1601 static void
1602 sortlines (struct line *lines, int nlines, struct line *temp)
1604 register struct line *lo, *hi, *t;
1605 register int nlo, nhi;
1607 if (nlines == 2)
1609 if (compare (&lines[0], &lines[1]) > 0)
1611 *temp = lines[0];
1612 lines[0] = lines[1];
1613 lines[1] = *temp;
1615 return;
1618 nlo = nlines / 2;
1619 lo = lines;
1620 nhi = nlines - nlo;
1621 hi = lines + nlo;
1623 if (nlo > 1)
1624 sortlines (lo, nlo, temp);
1626 if (nhi > 1)
1627 sortlines (hi, nhi, temp);
1629 t = temp;
1631 while (nlo && nhi)
1632 if (compare (lo, hi) <= 0)
1633 *t++ = *lo++, --nlo;
1634 else
1635 *t++ = *hi++, --nhi;
1636 while (nlo--)
1637 *t++ = *lo++;
1639 for (lo = lines, nlo = nlines - nhi, t = temp; nlo; --nlo)
1640 *lo++ = *t++;
1643 /* Check that each of the NFILES FILES is ordered.
1644 Return a count of disordered files. */
1646 static int
1647 check (char **files, int nfiles)
1649 int i, disorders = 0;
1650 FILE *fp;
1652 for (i = 0; i < nfiles; ++i)
1654 fp = xfopen (files[i], "r");
1655 if (checkfp (fp, files[i]))
1657 ++disorders;
1660 return disorders;
1663 /* Merge NFILES FILES onto OFP. */
1665 static void
1666 merge (char **files, int nfiles, FILE *ofp, const char *output_file)
1668 int i, j, t;
1669 char *temp;
1670 FILE *fps[NMERGE], *tfp;
1672 while (nfiles > NMERGE)
1674 t = 0;
1675 for (i = 0; i < nfiles / NMERGE; ++i)
1677 for (j = 0; j < NMERGE; ++j)
1678 fps[j] = xfopen (files[i * NMERGE + j], "r");
1679 tfp = xtmpfopen (temp = tempname ());
1680 mergefps (fps, NMERGE, tfp, temp);
1681 xfclose (tfp);
1682 for (j = 0; j < NMERGE; ++j)
1683 zaptemp (files[i * NMERGE + j]);
1684 files[t++] = temp;
1686 for (j = 0; j < nfiles % NMERGE; ++j)
1687 fps[j] = xfopen (files[i * NMERGE + j], "r");
1688 tfp = xtmpfopen (temp = tempname ());
1689 mergefps (fps, nfiles % NMERGE, tfp, temp);
1690 xfclose (tfp);
1691 for (j = 0; j < nfiles % NMERGE; ++j)
1692 zaptemp (files[i * NMERGE + j]);
1693 files[t++] = temp;
1694 nfiles = t;
1697 for (i = 0; i < nfiles; ++i)
1698 fps[i] = xfopen (files[i], "r");
1699 mergefps (fps, i, ofp, output_file);
1700 for (i = 0; i < nfiles; ++i)
1701 zaptemp (files[i]);
1704 /* Sort NFILES FILES onto OFP. */
1706 static void
1707 sort (char **files, int nfiles, FILE *ofp, const char *output_file)
1709 struct buffer buf;
1710 struct lines lines;
1711 struct line *tmp;
1712 int i, ntmp;
1713 FILE *fp, *tfp;
1714 struct tempnode *node;
1715 int n_temp_files = 0;
1716 char **tempfiles;
1718 initbuf (&buf, sortalloc);
1719 initlines (&lines, sortalloc / linelength + 1,
1720 LINEALLOC / sizeof (struct line));
1721 ntmp = lines.alloc;
1722 tmp = (struct line *) xmalloc (ntmp * sizeof (struct line));
1724 while (nfiles--)
1726 const char *temp_output;
1728 fp = xfopen (*files++, "r");
1729 while (fillbuf (&buf, fp))
1731 findlines (&buf, &lines);
1732 if (lines.used > ntmp)
1734 while (lines.used > ntmp)
1735 ntmp *= 2;
1736 tmp = (struct line *)
1737 xrealloc ((char *) tmp, ntmp * sizeof (struct line));
1739 sortlines (lines.lines, lines.used, tmp);
1740 if (feof (fp) && !nfiles && !n_temp_files && !buf.left)
1742 tfp = ofp;
1743 temp_output = output_file;
1745 else
1747 ++n_temp_files;
1748 tfp = xtmpfopen (temp_output = tempname ());
1750 for (i = 0; i < lines.used; ++i)
1751 if (!unique || i == 0
1752 || compare (&lines.lines[i], &lines.lines[i - 1]))
1753 write_bytes (lines.lines[i].text, lines.lines[i].length, tfp,
1754 temp_output);
1755 if (tfp != ofp)
1756 xfclose (tfp);
1758 xfclose (fp);
1761 free (buf.buf);
1762 free ((char *) lines.lines);
1763 free ((char *) tmp);
1765 if (n_temp_files)
1767 tempfiles = (char **) xmalloc (n_temp_files * sizeof (char *));
1768 i = n_temp_files;
1769 for (node = temphead.next; i > 0; node = node->next)
1770 tempfiles[--i] = node->name;
1771 merge (tempfiles, n_temp_files, ofp, output_file);
1772 free ((char *) tempfiles);
1776 /* Insert key KEY at the end of the list (`keyhead'). */
1778 static void
1779 insertkey (struct keyfield *key)
1781 struct keyfield *k = &keyhead;
1783 while (k->next)
1784 k = k->next;
1785 k->next = key;
1786 key->next = NULL;
1789 static void
1790 badfieldspec (const char *s)
1792 error (SORT_FAILURE, 0, _("invalid field specification `%s'"), s);
1795 /* Handle interrupts and hangups. */
1797 static void
1798 sighandler (int sig)
1800 #ifdef SA_INTERRUPT
1801 struct sigaction sigact;
1803 sigact.sa_handler = SIG_DFL;
1804 sigemptyset (&sigact.sa_mask);
1805 sigact.sa_flags = 0;
1806 sigaction (sig, &sigact, NULL);
1807 #else /* !SA_INTERRUPT */
1808 signal (sig, SIG_DFL);
1809 #endif /* SA_INTERRUPT */
1810 cleanup ();
1811 kill (getpid (), sig);
1814 /* Set the ordering options for KEY specified in S.
1815 Return the address of the first character in S that
1816 is not a valid ordering option.
1817 BLANKTYPE is the kind of blanks that 'b' should skip. */
1819 static char *
1820 set_ordering (register const char *s, struct keyfield *key,
1821 enum blanktype blanktype)
1823 while (*s)
1825 switch (*s)
1827 case 'b':
1828 if (blanktype == bl_start || blanktype == bl_both)
1829 key->skipsblanks = 1;
1830 if (blanktype == bl_end || blanktype == bl_both)
1831 key->skipeblanks = 1;
1832 break;
1833 case 'd':
1834 key->ignore = nondictionary;
1835 break;
1836 case 'f':
1837 key->translate = fold_toupper;
1838 break;
1839 case 'g':
1840 key->general_numeric = 1;
1841 break;
1842 case 'i':
1843 key->ignore = nonprinting;
1844 break;
1845 case 'M':
1846 key->month = 1;
1847 break;
1848 case 'n':
1849 key->numeric = 1;
1850 break;
1851 case 'r':
1852 key->reverse = 1;
1853 break;
1854 default:
1855 return (char *) s;
1857 ++s;
1859 return (char *) s;
1862 static void
1863 key_init (struct keyfield *key)
1865 memset (key, 0, sizeof (*key));
1866 key->eword = -1;
1870 main (int argc, char **argv)
1872 struct keyfield *key = NULL, gkey;
1873 char *s;
1874 int i, t, t2;
1875 int checkonly = 0, mergeonly = 0, nfiles = 0;
1876 char *minus = "-", *outfile = minus, **files, *tmp;
1877 FILE *ofp;
1878 #ifdef SA_INTERRUPT
1879 struct sigaction oldact, newact;
1880 #endif /* SA_INTERRUPT */
1882 program_name = argv[0];
1883 setlocale (LC_ALL, "");
1884 bindtextdomain (PACKAGE, LOCALEDIR);
1885 textdomain (PACKAGE);
1887 #ifdef ENABLE_NLS
1889 hard_LC_COLLATE = hard_locale (LC_COLLATE);
1890 hard_LC_CTYPE = hard_locale (LC_CTYPE);
1891 # if HAVE_NL_LANGINFO
1892 hard_LC_TIME = hard_locale (LC_TIME);
1893 # endif
1895 /* Let's get locale's representation of the decimal point */
1897 struct lconv *lconvp = localeconv ();
1899 /* If the locale doesn't define a decimal point, or if the decimal
1900 point is multibyte, use the C decimal point. We don't support
1901 multibyte decimal points yet. */
1902 decimal_point = *lconvp->decimal_point;
1903 if (! decimal_point || lconvp->decimal_point[1])
1904 decimal_point = C_DECIMAL_POINT;
1906 /* We don't support multibyte thousands separators yet. */
1907 th_sep = *lconvp->thousands_sep;
1908 if (! th_sep || lconvp->thousands_sep[1])
1909 th_sep = CHAR_MAX + 1;
1912 #endif /* NLS */
1914 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1915 AUTHORS, usage);
1917 have_read_stdin = 0;
1918 inittables ();
1920 temp_dir = getenv ("TMPDIR");
1921 if (temp_dir == NULL)
1922 temp_dir = DEFAULT_TMPDIR;
1924 /* Change the way xmalloc and xrealloc fail. */
1925 xalloc_exit_failure = SORT_FAILURE;
1926 xalloc_fail_func = cleanup;
1928 #ifdef SA_INTERRUPT
1929 newact.sa_handler = sighandler;
1930 sigemptyset (&newact.sa_mask);
1931 newact.sa_flags = 0;
1933 sigaction (SIGINT, NULL, &oldact);
1934 if (oldact.sa_handler != SIG_IGN)
1935 sigaction (SIGINT, &newact, NULL);
1936 sigaction (SIGHUP, NULL, &oldact);
1937 if (oldact.sa_handler != SIG_IGN)
1938 sigaction (SIGHUP, &newact, NULL);
1939 sigaction (SIGPIPE, NULL, &oldact);
1940 if (oldact.sa_handler != SIG_IGN)
1941 sigaction (SIGPIPE, &newact, NULL);
1942 sigaction (SIGTERM, NULL, &oldact);
1943 if (oldact.sa_handler != SIG_IGN)
1944 sigaction (SIGTERM, &newact, NULL);
1945 #else /* !SA_INTERRUPT */
1946 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1947 signal (SIGINT, sighandler);
1948 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1949 signal (SIGHUP, sighandler);
1950 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1951 signal (SIGPIPE, sighandler);
1952 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1953 signal (SIGTERM, sighandler);
1954 #endif /* !SA_INTERRUPT */
1956 gkey.sword = gkey.eword = -1;
1957 gkey.ignore = NULL;
1958 gkey.translate = NULL;
1959 gkey.numeric = gkey.general_numeric = gkey.month = gkey.reverse = 0;
1960 gkey.skipsblanks = gkey.skipeblanks = 0;
1962 files = (char **) xmalloc (sizeof (char *) * argc);
1964 for (i = 1; i < argc; ++i)
1966 if (argv[i][0] == '+')
1968 if (key)
1969 insertkey (key);
1970 key = (struct keyfield *) xmalloc (sizeof (struct keyfield));
1971 key_init (key);
1972 s = argv[i] + 1;
1973 if (! (ISDIGIT (*s) || (*s == '.' && ISDIGIT (s[1]))))
1974 badfieldspec (argv[i]);
1975 for (t = 0; ISDIGIT (*s); ++s)
1976 t = 10 * t + *s - '0';
1977 t2 = 0;
1978 if (*s == '.')
1979 for (++s; ISDIGIT (*s); ++s)
1980 t2 = 10 * t2 + *s - '0';
1981 if (t2 || t)
1983 key->sword = t;
1984 key->schar = t2;
1986 else
1987 key->sword = -1;
1988 s = set_ordering (s, key, bl_start);
1989 if (*s)
1990 badfieldspec (argv[i]);
1992 else if (argv[i][0] == '-' && argv[i][1])
1994 s = argv[i] + 1;
1995 if (ISDIGIT (*s) || (*s == '.' && ISDIGIT (s[1])))
1997 if (!key)
1999 /* Provoke with `sort -9'. */
2000 error (0, 0, _("when using the old-style +POS and -POS \
2001 key specifiers,\nthe +POS specifier must come first"));
2002 usage (SORT_FAILURE);
2004 for (t = 0; ISDIGIT (*s); ++s)
2005 t = t * 10 + *s - '0';
2006 t2 = 0;
2007 if (*s == '.')
2008 for (++s; ISDIGIT (*s); ++s)
2009 t2 = t2 * 10 + *s - '0';
2010 key->eword = t;
2011 key->echar = t2;
2012 s = set_ordering (s, key, bl_end);
2013 if (*s)
2014 badfieldspec (argv[i]);
2015 insertkey (key);
2016 key = NULL;
2018 else
2019 while (*s)
2021 s = set_ordering (s, &gkey, bl_both);
2022 switch (*s)
2024 case '\0':
2025 break;
2026 case 'c':
2027 checkonly = 1;
2028 break;
2029 case 'k':
2030 if (s[1])
2031 ++s;
2032 else
2034 if (i == argc - 1)
2035 error (SORT_FAILURE, 0,
2036 _("option `-k' requires an argument"));
2037 else
2038 s = argv[++i];
2040 if (key)
2041 insertkey (key);
2042 key = (struct keyfield *)
2043 xmalloc (sizeof (struct keyfield));
2044 key_init (key);
2045 /* Get POS1. */
2046 if (!ISDIGIT (*s))
2047 badfieldspec (argv[i]);
2048 for (t = 0; ISDIGIT (*s); ++s)
2049 t = 10 * t + *s - '0';
2050 if (t == 0)
2052 /* Provoke with `sort -k0' */
2053 error (0, 0, _("the starting field number argument \
2054 to the `-k' option must be positive"));
2055 badfieldspec (argv[i]);
2057 --t;
2058 t2 = 0;
2059 if (*s == '.')
2061 if (!ISDIGIT (s[1]))
2063 /* Provoke with `sort -k1.' */
2064 error (0, 0, _("starting field spec has `.' but \
2065 lacks following character offset"));
2066 badfieldspec (argv[i]);
2068 for (++s; ISDIGIT (*s); ++s)
2069 t2 = 10 * t2 + *s - '0';
2070 if (t2 == 0)
2072 /* Provoke with `sort -k1.0' */
2073 error (0, 0, _("starting field character offset \
2074 argument to the `-k' option\nmust be positive"));
2075 badfieldspec (argv[i]);
2077 --t2;
2079 if (t2 || t)
2081 key->sword = t;
2082 key->schar = t2;
2084 else
2085 key->sword = -1;
2086 s = set_ordering (s, key, bl_start);
2087 if (*s == 0)
2089 key->eword = -1;
2090 key->echar = 0;
2092 else if (*s != ',')
2093 badfieldspec (argv[i]);
2094 else if (*s == ',')
2096 /* Skip over comma. */
2097 ++s;
2098 if (*s == 0)
2100 /* Provoke with `sort -k1,' */
2101 error (0, 0, _("field specification has `,' but \
2102 lacks following field spec"));
2103 badfieldspec (argv[i]);
2105 /* Get POS2. */
2106 for (t = 0; ISDIGIT (*s); ++s)
2107 t = t * 10 + *s - '0';
2108 if (t == 0)
2110 /* Provoke with `sort -k1,0' */
2111 error (0, 0, _("ending field number argument \
2112 to the `-k' option must be positive"));
2113 badfieldspec (argv[i]);
2115 --t;
2116 t2 = 0;
2117 if (*s == '.')
2119 if (!ISDIGIT (s[1]))
2121 /* Provoke with `sort -k1,1.' */
2122 error (0, 0, _("ending field spec has `.' \
2123 but lacks following character offset"));
2124 badfieldspec (argv[i]);
2126 for (++s; ISDIGIT (*s); ++s)
2127 t2 = t2 * 10 + *s - '0';
2129 else
2131 /* `-k 2,3' is equivalent to `+1 -3'. */
2132 ++t;
2134 key->eword = t;
2135 key->echar = t2;
2136 s = set_ordering (s, key, bl_end);
2137 if (*s)
2138 badfieldspec (argv[i]);
2140 insertkey (key);
2141 key = NULL;
2142 goto outer;
2143 case 'm':
2144 mergeonly = 1;
2145 break;
2146 case 'o':
2147 if (s[1])
2148 outfile = s + 1;
2149 else
2151 if (i == argc - 1)
2152 error (SORT_FAILURE, 0,
2153 _("option `-o' requires an argument"));
2154 else
2155 outfile = argv[++i];
2157 goto outer;
2158 case 's':
2159 stable = 1;
2160 break;
2161 case 't':
2162 if (s[1])
2163 tab = *++s;
2164 else if (i < argc - 1)
2166 tab = *argv[++i];
2167 goto outer;
2169 else
2170 error (SORT_FAILURE, 0,
2171 _("option `-t' requires an argument"));
2172 break;
2173 case 'T':
2174 if (s[1])
2175 temp_dir = ++s;
2176 else
2178 if (i < argc - 1)
2179 temp_dir = argv[++i];
2180 else
2181 error (SORT_FAILURE, 0,
2182 _("option `-T' requires an argument"));
2184 goto outer;
2185 /* break; */
2186 case 'u':
2187 unique = 1;
2188 break;
2189 case 'z':
2190 eolchar = 0;
2191 break;
2192 case 'y':
2193 /* Accept and ignore e.g. -y0 for compatibility with
2194 Solaris 2. */
2195 goto outer;
2196 default:
2197 fprintf (stderr, _("%s: unrecognized option `-%c'\n"),
2198 argv[0], *s);
2199 usage (SORT_FAILURE);
2201 if (*s)
2202 ++s;
2205 else /* Not an option. */
2207 files[nfiles++] = argv[i];
2209 outer:;
2212 if (key)
2213 insertkey (key);
2215 /* Inheritance of global options to individual keys. */
2216 for (key = keyhead.next; key; key = key->next)
2217 if (!key->ignore && !key->translate && !key->skipsblanks && !key->reverse
2218 && !key->skipeblanks && !key->month && !key->numeric
2219 && !key->general_numeric)
2221 key->ignore = gkey.ignore;
2222 key->translate = gkey.translate;
2223 key->skipsblanks = gkey.skipsblanks;
2224 key->skipeblanks = gkey.skipeblanks;
2225 key->month = gkey.month;
2226 key->numeric = gkey.numeric;
2227 key->general_numeric = gkey.general_numeric;
2228 key->reverse = gkey.reverse;
2231 if (!keyhead.next && (gkey.ignore || gkey.translate || gkey.skipsblanks
2232 || gkey.skipeblanks || gkey.month || gkey.numeric
2233 || gkey.general_numeric))
2234 insertkey (&gkey);
2235 reverse = gkey.reverse;
2237 if (nfiles == 0)
2239 nfiles = 1;
2240 files = &minus;
2243 if (checkonly)
2245 /* POSIX requires that sort return 1 IFF invoked with -c and the
2246 input is not properly sorted. */
2247 exit (check (files, nfiles) == 0 ? 0 : 1);
2250 if (!STREQ (outfile, "-"))
2252 struct stat outstat;
2253 if (stat (outfile, &outstat) == 0)
2255 /* The following code prevents a race condition when
2256 people use the brain dead shell programming idiom:
2257 cat file | sort -o file
2258 This feature is provided for historical compatibility,
2259 but we strongly discourage ever relying on this in
2260 new shell programs. */
2262 /* Temporarily copy each input file that might be another name
2263 for the output file. When in doubt (e.g. a pipe), copy. */
2264 for (i = 0; i < nfiles; ++i)
2266 char buf[8192];
2267 FILE *in_fp;
2268 FILE *out_fp;
2269 int cc;
2271 if (S_ISREG (outstat.st_mode) && !STREQ (outfile, files[i]))
2273 struct stat instat;
2274 if ((STREQ (files[i], "-")
2275 ? fstat (STDIN_FILENO, &instat)
2276 : stat (files[i], &instat)) != 0)
2278 error (0, errno, "%s", files[i]);
2279 cleanup ();
2280 exit (SORT_FAILURE);
2282 if (S_ISREG (instat.st_mode) && !SAME_INODE (instat, outstat))
2284 /* We know the files are distinct. */
2285 continue;
2289 in_fp = xfopen (files[i], "r");
2290 tmp = tempname ();
2291 out_fp = xtmpfopen (tmp);
2292 /* FIXME: maybe use copy.c(copy) here. */
2293 while ((cc = fread (buf, 1, sizeof buf, in_fp)) > 0)
2294 write_bytes (buf, cc, out_fp, tmp);
2295 if (ferror (in_fp))
2297 error (0, errno, "%s", files[i]);
2298 cleanup ();
2299 exit (SORT_FAILURE);
2301 xfclose (out_fp);
2302 xfclose (in_fp);
2303 files[i] = tmp;
2305 ofp = xfopen (outfile, "w");
2307 else
2309 /* A non-`-' outfile was specified, but the file doesn't yet exist.
2310 Before opening it for writing (thus creating it), make sure all
2311 of the input files exist. Otherwise, creating the output file
2312 could create an otherwise missing input file, making sort succeed
2313 when it should fail. */
2314 for (i = 0; i < nfiles; ++i)
2316 struct stat sb;
2317 if (STREQ (files[i], "-"))
2318 continue;
2319 if (stat (files[i], &sb))
2321 error (0, errno, "%s", files[i]);
2322 cleanup ();
2323 exit (SORT_FAILURE);
2327 ofp = xfopen (outfile, "w");
2330 else
2332 ofp = stdout;
2335 if (mergeonly)
2336 merge (files, nfiles, ofp, outfile);
2337 else
2338 sort (files, nfiles, ofp, outfile);
2339 cleanup ();
2341 /* If we wait for the implicit flush on exit, and the parent process
2342 has closed stdout (e.g., exec >&- in a shell), then the output file
2343 winds up empty. I don't understand why. This is under SunOS,
2344 Solaris, Ultrix, and Irix. This premature fflush makes the output
2345 reappear. --karl@cs.umb.edu */
2346 if (fflush (ofp) < 0)
2347 error (SORT_FAILURE, errno, _("%s: write error"), outfile);
2349 if (have_read_stdin && fclose (stdin) == EOF)
2350 error (SORT_FAILURE, errno, "%s", outfile);
2351 if (ferror (stdout) || fclose (stdout) == EOF)
2352 error (SORT_FAILURE, errno, _("%s: write error"), outfile);
2354 exit (EXIT_SUCCESS);