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)
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. */
26 #include <sys/types.h>
31 #include "long-options.h"
33 #include "hard-locale.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>
46 #if HAVE_PATHCONF && defined _PC_NAME_MAX
47 # define NAME_MAX_IN_DIR(Dir) pathconf (Dir, _PC_NAME_MAX)
49 # define NAME_MAX_IN_DIR(Dir) 255
56 /* Undefine, to avoid warning about redefinition on some systems. */
58 #define min(a, b) ((a) < (b) ? (a) : (b))
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"
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'
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
;
89 static int hard_LC_TIME
;
92 # define IS_THOUSANDS_SEP(x) ((x) == th_sep)
96 # define decimal_point C_DECIMAL_POINT
97 # define IS_THOUSANDS_SEP(x) 0
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. */
107 /* Lines are held in core as counted strings. */
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. */
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. */
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. */
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. */
160 /* The name this program was run with. */
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 */
181 # define MONTHTAB_CONST const
184 /* Table mapping month names to integers.
185 Alphabetic order allows binary search. */
186 static MONTHTAB_CONST
struct month monthtab
[] =
202 /* During the merge phase, the number of files to merge at once. */
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. */
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. */
231 /* Tab character separating fields. If NUL, then fields are separated
232 by the empty string between a non-whitespace character and a whitespace
236 /* Flag to remove consecutive duplicate lines from the output.
237 Only the last of a sequence of equal lines will be output. */
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
;
250 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
255 Usage: %s [OPTION]... [FILE]...\n\
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\
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
);
306 /* The list of temporary files. */
307 static struct tempnode
310 struct tempnode
*next
;
313 /* Clean up any remaining temporary files. */
318 struct tempnode
*node
;
320 for (node
= temphead
.next
; node
; node
= node
->next
)
325 xtmpfopen (const char *file
)
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
);
344 xfopen (const char *file
, const char *how
)
348 if (STREQ (file
, "-"))
354 if ((fp
= fopen (file
, how
)) == NULL
)
356 error (0, errno
, "%s", file
);
372 /* Allow reading stdin from tty more than once. */
376 else if (fp
== stdout
)
378 if (fflush (fp
) != 0)
380 error (0, errno
, _("flushing file"));
387 if (fclose (fp
) != 0)
389 error (0, errno
, _("error closing file"));
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
);
407 /* Return a name for a temporary file. */
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. */
422 "%s%ssort%5.5d%5.5d",
424 (len
&& temp_dir
[len
- 1] != '/') ? "/" : "",
425 (unsigned int) getpid () & 0xffff, seq
);
427 sprintf (name
, "%s%ss%5.5d%2.2d.%3.3d",
429 (len
&& temp_dir
[len
- 1] != '/') ? "/" : "",
430 (unsigned int) getpid () & 0xffff, seq
/ 1000, seq
% 1000);
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)
439 node
= (struct tempnode
*) xmalloc (sizeof (struct tempnode
));
441 node
->next
= temphead
.next
;
442 temphead
.next
= node
;
446 /* Search through the list of temporary files for NAME;
447 remove it if it is found on the list. */
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
))
462 node
->next
= temp
->next
;
463 free ((char *) temp
);
470 struct_month_cmp (const void *m1
, const void *m2
)
472 return strcmp (((const struct month
*) m1
)->name
,
473 ((const struct month
*) m2
)->name
);
478 /* Initialize the character class tables. */
485 for (i
= 0; i
< UCHAR_LIM
; ++i
)
491 if (!ISALNUM (i
) && !ISBLANK (i
))
492 nondictionary
[i
] = 1;
494 fold_toupper
[i
] = toupper (i
);
499 #if defined ENABLE_NLS && HAVE_NL_LANGINFO
500 /* If we're not in the "C" locale, read different names for months. */
503 for (i
= 0; i
< MONTHS_PER_YEAR
; i
++)
510 s
= (char *) nl_langinfo (ABMON_1
+ i
);
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
])];
519 qsort ((void *) monthtab
, MONTHS_PER_YEAR
,
520 sizeof (struct month
), struct_month_cmp
);
525 /* Initialize BUF, allocating ALLOC bytes initially. */
528 initbuf (struct buffer
*buf
, int 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. */
541 fillbuf (struct buffer
*buf
, FILE *fp
)
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
)
554 buf
->buf
= xrealloc (buf
->buf
, buf
->alloc
);
556 cc
= fread (buf
->buf
+ buf
->used
, 1, buf
->alloc
- buf
->used
, fp
);
559 error (0, errno
, _("read error"));
566 if (feof (fp
) && buf
->used
&& buf
->buf
[buf
->used
- 1] != eolchar
)
568 if (buf
->used
== buf
->alloc
)
571 buf
->buf
= xrealloc (buf
->buf
, buf
->alloc
);
573 buf
->buf
[buf
->used
++] = eolchar
;
579 /* Initialize LINES, allocating space for ALLOC lines initially.
580 LIMIT is the maximum possible number of lines to allocate space
584 initlines (struct lines
*lines
, int alloc
, int limit
)
586 lines
->alloc
= alloc
;
587 lines
->lines
= (struct line
*) xmalloc (lines
->alloc
* sizeof (struct line
));
589 lines
->limit
= limit
;
592 /* Return a pointer to the first character of the field specified
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
;
602 while (ptr
< lim
&& sword
--)
604 while (ptr
< lim
&& *ptr
!= tab
)
610 while (ptr
< lim
&& sword
--)
612 while (ptr
< lim
&& blanks
[UCHAR (*ptr
)])
614 while (ptr
< lim
&& !blanks
[UCHAR (*ptr
)])
618 if (key
->skipsblanks
)
619 while (ptr
< lim
&& blanks
[UCHAR (*ptr
)])
622 if (ptr
+ schar
<= lim
)
630 /* Return the limit of (a pointer to the first character after) the field
631 in LINE specified by KEY. */
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. */
651 while (ptr
< lim
&& eword
--)
653 while (ptr
< lim
&& *ptr
!= tab
)
655 if (ptr
< lim
&& (eword
|| echar
> 0))
659 while (ptr
< lim
&& eword
--)
661 while (ptr
< lim
&& blanks
[UCHAR (*ptr
)])
663 while (ptr
< lim
&& !blanks
[UCHAR (*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'.
681 $ textutils-1.15/src/sort +0.6 -0.7 </tmp/sort.in
684 $ /bin/sort +0.6 -0.7 </tmp/sort.in
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. */
701 newlim
= memchr (ptr
, tab
, lim
- ptr
);
709 while (newlim
< lim
&& blanks
[UCHAR (*newlim
)])
711 while (newlim
< lim
&& !blanks
[UCHAR (*newlim
)])
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
)])
723 /* Advance PTR by ECHAR (if possible), but no further than LIM. */
724 if (ptr
+ echar
<= lim
)
735 trim_trailing_blanks (const char *a_start
, char **a_end
)
737 while (*a_end
> a_start
&& blanks
[UCHAR (*(*a_end
- 1))])
741 /* Find the lines in BUF, storing pointers and lengths in LINES. */
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
;
751 while (beg
< lim
&& (ptr
= memchr (beg
, eolchar
, lim
- beg
))
752 && lines
->used
< lines
->limit
)
754 if (lines
->used
== lines
->alloc
)
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. */
769 lines
->lines
[lines
->used
].keylim
=
770 limfield (&lines
->lines
[lines
->used
], key
);
772 lines
->lines
[lines
->used
].keylim
= ptr
;
775 lines
->lines
[lines
->used
].keybeg
=
776 begfield (&lines
->lines
[lines
->used
], key
);
779 if (key
->skipsblanks
)
780 while (blanks
[UCHAR (*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
);
792 lines
->lines
[lines
->used
].keybeg
= 0;
793 lines
->lines
[lines
->used
].keylim
= 0;
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.
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.
823 if digit return 1, else 0
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
836 fraccompare (register const char *a
, register const char *b
)
838 if (*a
== decimal_point
&& *b
== decimal_point
)
843 if (ISDIGIT (*a
) && ISDIGIT (*b
))
846 goto a_trailing_nonzero
;
848 goto b_trailing_nonzero
;
851 else if (*a
++ == decimal_point
)
854 while (*a
== NUMERIC_ZERO
)
858 else if (*b
++ == decimal_point
)
861 while (*b
== NUMERIC_ZERO
)
863 return - ISDIGIT (*b
);
868 /* Compare strings A and B as numbers without explicitly converting them to
869 machine numbers. Comparatively slow for short strings, but asymptotically
873 numcompare (register const char *a
, register const char *b
)
875 register int tmpa
, tmpb
, loga
, logb
, tmp
;
880 while (blanks
[UCHAR (tmpa
)])
882 while (blanks
[UCHAR (tmpb
)])
885 if (tmpa
== NEGATION_SIGN
)
889 while (tmpa
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpa
));
890 if (tmpb
!= NEGATION_SIGN
)
892 if (tmpa
== decimal_point
)
895 while (tmpa
== NUMERIC_ZERO
);
898 while (tmpb
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpb
))
900 if (tmpb
== decimal_point
)
903 while (tmpb
== NUMERIC_ZERO
);
910 while (tmpb
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpb
));
912 while (tmpa
== tmpb
&& ISDIGIT (tmpa
))
916 while (IS_THOUSANDS_SEP (tmpa
));
919 while (IS_THOUSANDS_SEP (tmpb
));
922 if ((tmpa
== decimal_point
&& !ISDIGIT (tmpb
))
923 || (tmpb
== decimal_point
&& !ISDIGIT (tmpa
)))
924 return -fraccompare (a
, b
);
928 for (loga
= 0; ISDIGIT (tmpa
); ++loga
)
931 while (IS_THOUSANDS_SEP (tmpa
));
933 for (logb
= 0; ISDIGIT (tmpb
); ++logb
)
936 while (IS_THOUSANDS_SEP (tmpb
));
938 if (logb
- loga
!= 0)
946 else if (tmpb
== NEGATION_SIGN
)
950 while (tmpb
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpb
));
951 if (tmpb
== decimal_point
)
954 while (tmpb
== NUMERIC_ZERO
);
957 while (tmpa
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpa
))
959 if (tmpa
== decimal_point
)
962 while (tmpa
== NUMERIC_ZERO
);
969 while (tmpa
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpa
))
971 while (tmpb
== NUMERIC_ZERO
|| IS_THOUSANDS_SEP (tmpb
))
974 while (tmpa
== tmpb
&& ISDIGIT (tmpa
))
978 while (IS_THOUSANDS_SEP (tmpa
));
981 while (IS_THOUSANDS_SEP (tmpb
));
984 if ((tmpa
== decimal_point
&& !ISDIGIT (tmpb
))
985 || (tmpb
== decimal_point
&& !ISDIGIT (tmpa
)))
986 return fraccompare (a
, b
);
990 for (loga
= 0; ISDIGIT (tmpa
); ++loga
)
993 while (IS_THOUSANDS_SEP (tmpa
));
995 for (logb
= 0; ISDIGIT (tmpb
); ++logb
)
998 while (IS_THOUSANDS_SEP (tmpb
));
1000 if (loga
- logb
!= 0)
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. */
1019 double a
= strtod (sa
, &ea
);
1020 double b
= strtod (sb
, &eb
);
1022 /* Put conversion errors at the start of the collating sequence. */
1024 return sb
== eb
? 0 : -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. */
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. */
1043 getmonth (const char *s
, int len
)
1046 register int i
, lo
= 0, hi
= MONTHS_PER_YEAR
, result
;
1048 while (len
> 0 && blanks
[UCHAR (*s
)])
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])])
1066 int ix
= (lo
+ hi
) / 2;
1068 if (strncmp (month
, monthtab
[ix
].name
, strlen (monthtab
[ix
].name
)) < 0)
1073 while (hi
- lo
> 1);
1075 result
= (!strncmp (month
, monthtab
[lo
].name
, strlen (monthtab
[lo
].name
))
1076 ? monthtab
[lo
].val
: 0);
1081 /* Compare two lines A and B trying every key in sequence until there
1082 are no more keys or a difference is found. */
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
);
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
);
1112 texta
= a
->text
, textb
= b
->text
;
1113 if (key
->skipsblanks
)
1115 while (texta
< lima
&& blanks
[UCHAR (*texta
)])
1117 while (textb
< limb
&& blanks
[UCHAR (*textb
)])
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
;
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
)
1155 *lima
= savea
, *limb
= saveb
;
1157 return key
->reverse
? -diff
: diff
;
1160 else if (key
->month
)
1162 diff
= getmonth (texta
, lena
) - getmonth (textb
, lenb
);
1164 return key
->reverse
? -diff
: diff
;
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
++)
1184 copy_a
[new_len_a
] = (translate
1185 ? translate
[UCHAR (texta
[i
])]
1187 if (!ignore
|| !ignore
[UCHAR (texta
[i
])])
1192 copy_b
[new_len_b
] = (translate
1193 ? translate
[UCHAR (textb
[i
])]
1195 if (!ignore
|| !ignore
[UCHAR (textb
[i
])])
1200 diff
= memcoll (copy_a
, new_len_a
, copy_b
, new_len_b
);
1204 diff
= memcoll (texta
, lena
, textb
, lenb
);
1208 return key
->reverse
? -diff
: diff
;
1213 else if (ignore
&& translate
)
1215 #define CMP_WITH_IGNORE(A, B) \
1218 while (texta < lima && textb < limb) \
1220 while (texta < lima && ignore[UCHAR (*texta)]) \
1222 while (textb < limb && ignore[UCHAR (*textb)]) \
1224 if (texta < lima && textb < limb) \
1228 diff = UCHAR (A) - UCHAR (B); \
1235 if (texta == lima && textb < limb && !ignore[UCHAR (*textb)]) \
1237 else if (texta < lima && textb == limb \
1238 && !ignore[UCHAR (*texta)]) \
1244 while (texta < lima && ignore[UCHAR (*texta)]) \
1246 while (textb < limb && ignore[UCHAR (*textb)]) \
1249 if (texta == lima && textb < limb) \
1251 else if (texta < lima && textb == limb) \
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; \
1262 CMP_WITH_IGNORE (translate
[UCHAR (*texta
)], translate
[UCHAR (*textb
)]);
1264 CMP_WITH_IGNORE (UCHAR (*texta
), UCHAR (*textb
));
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
)]));
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
);
1288 diff
= memcmp (texta
, textb
, min (lena
, lenb
));
1293 return key
->reverse
? -diff
: diff
;
1294 if (comparable_lengths
&& (diff
= lena
- lenb
) != 0)
1295 return key
->reverse
? -diff
: diff
;
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. */
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. */
1314 diff
= keycompare (a
, b
);
1315 if (diff
!= 0 || unique
|| stable
)
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;
1327 if (hard_LC_COLLATE
)
1329 diff
= memcoll (a
->text
, alen
, b
->text
, blen
);
1331 return reverse
? -diff
: diff
;
1335 minlen
= min (alen
, blen
);
1337 || (! (diff
= UCHAR (a
->text
[0]) - UCHAR (b
->text
[0]))
1338 && ! (diff
= memcmp (a
->text
, b
->text
, minlen
))))
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. */
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. */
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
)));
1365 temp
.text
= xmalloc (alloc
);
1367 cc
= fillbuf (&buf
, fp
);
1371 findlines (&buf
, &lines
);
1375 struct line
*prev_line
; /* Pointer to previous line. */
1376 int cmp
; /* Result of calling compare. */
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;
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
)
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
);
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
;
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"));
1438 free ((char *) lines
.lines
);
1440 return disorder_line_number
;
1443 /* Merge lines from FPS onto OFP. NFPS cannot be greater than NMERGE.
1444 Close FPS before returning. */
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
1459 register int i
, j
, t
;
1461 /* Allocate space for a saved line if necessary. */
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
]))
1477 for (j
= i
; j
< nfps
; ++j
)
1478 fps
[j
] = fps
[j
+ 1];
1481 free (buffer
[i
].buf
);
1484 initlines (&lines
[i
], mergealloc
/ linelength
+ 1,
1485 LINEALLOC
/ ((NMERGE
+ NMERGE
) * sizeof (struct line
)));
1486 findlines (&buffer
[i
], &lines
[i
]);
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
)
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. */
1504 /* If uniquified output is turned on, output only the first of
1505 an identical series of lines. */
1508 if (savedflag
&& compare (&saved
, &lines
[ord
[0]].lines
[cur
[ord
[0]]]))
1510 write_bytes (saved
.text
, saved
.length
, ofp
, output_file
);
1515 if (savealloc
< lines
[ord
[0]].lines
[cur
[ord
[0]]].length
)
1517 while (savealloc
< lines
[ord
[0]].lines
[cur
[ord
[0]]].length
)
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
,
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
);
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]]);
1553 /* We reached EOF on fps[ord[0]]. */
1554 for (i
= 1; i
< nfps
; ++i
)
1555 if (ord
[i
] > ord
[0])
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];
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
]]]);
1582 t
= ord
[0] - ord
[i
];
1587 for (j
= 1; j
< i
; ++j
)
1588 ord
[j
- 1] = ord
[j
];
1592 if (unique
&& savedflag
)
1594 write_bytes (saved
.text
, saved
.length
, ofp
, output_file
);
1599 /* Sort the array LINES with NLINES members, using TEMP for temporary space. */
1602 sortlines (struct line
*lines
, int nlines
, struct line
*temp
)
1604 register struct line
*lo
, *hi
, *t
;
1605 register int nlo
, nhi
;
1609 if (compare (&lines
[0], &lines
[1]) > 0)
1612 lines
[0] = lines
[1];
1624 sortlines (lo
, nlo
, temp
);
1627 sortlines (hi
, nhi
, temp
);
1632 if (compare (lo
, hi
) <= 0)
1633 *t
++ = *lo
++, --nlo
;
1635 *t
++ = *hi
++, --nhi
;
1639 for (lo
= lines
, nlo
= nlines
- nhi
, t
= temp
; nlo
; --nlo
)
1643 /* Check that each of the NFILES FILES is ordered.
1644 Return a count of disordered files. */
1647 check (char **files
, int nfiles
)
1649 int i
, disorders
= 0;
1652 for (i
= 0; i
< nfiles
; ++i
)
1654 fp
= xfopen (files
[i
], "r");
1655 if (checkfp (fp
, files
[i
]))
1663 /* Merge NFILES FILES onto OFP. */
1666 merge (char **files
, int nfiles
, FILE *ofp
, const char *output_file
)
1670 FILE *fps
[NMERGE
], *tfp
;
1672 while (nfiles
> NMERGE
)
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
);
1682 for (j
= 0; j
< NMERGE
; ++j
)
1683 zaptemp (files
[i
* NMERGE
+ j
]);
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
);
1691 for (j
= 0; j
< nfiles
% NMERGE
; ++j
)
1692 zaptemp (files
[i
* NMERGE
+ j
]);
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
)
1704 /* Sort NFILES FILES onto OFP. */
1707 sort (char **files
, int nfiles
, FILE *ofp
, const char *output_file
)
1714 struct tempnode
*node
;
1715 int n_temp_files
= 0;
1718 initbuf (&buf
, sortalloc
);
1719 initlines (&lines
, sortalloc
/ linelength
+ 1,
1720 LINEALLOC
/ sizeof (struct line
));
1722 tmp
= (struct line
*) xmalloc (ntmp
* sizeof (struct line
));
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
)
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
)
1743 temp_output
= output_file
;
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
,
1762 free ((char *) lines
.lines
);
1763 free ((char *) tmp
);
1767 tempfiles
= (char **) xmalloc (n_temp_files
* sizeof (char *));
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'). */
1779 insertkey (struct keyfield
*key
)
1781 struct keyfield
*k
= &keyhead
;
1790 badfieldspec (const char *s
)
1792 error (SORT_FAILURE
, 0, _("invalid field specification `%s'"), s
);
1795 /* Handle interrupts and hangups. */
1798 sighandler (int sig
)
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 */
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. */
1820 set_ordering (register const char *s
, struct keyfield
*key
,
1821 enum blanktype blanktype
)
1828 if (blanktype
== bl_start
|| blanktype
== bl_both
)
1829 key
->skipsblanks
= 1;
1830 if (blanktype
== bl_end
|| blanktype
== bl_both
)
1831 key
->skipeblanks
= 1;
1834 key
->ignore
= nondictionary
;
1837 key
->translate
= fold_toupper
;
1840 key
->general_numeric
= 1;
1843 key
->ignore
= nonprinting
;
1863 key_init (struct keyfield
*key
)
1865 memset (key
, 0, sizeof (*key
));
1870 main (int argc
, char **argv
)
1872 struct keyfield
*key
= NULL
, gkey
;
1875 int checkonly
= 0, mergeonly
= 0, nfiles
= 0;
1876 char *minus
= "-", *outfile
= minus
, **files
, *tmp
;
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
);
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
);
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;
1914 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
1917 have_read_stdin
= 0;
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
;
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;
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] == '+')
1970 key
= (struct keyfield
*) xmalloc (sizeof (struct keyfield
));
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';
1979 for (++s
; ISDIGIT (*s
); ++s
)
1980 t2
= 10 * t2
+ *s
- '0';
1988 s
= set_ordering (s
, key
, bl_start
);
1990 badfieldspec (argv
[i
]);
1992 else if (argv
[i
][0] == '-' && argv
[i
][1])
1995 if (ISDIGIT (*s
) || (*s
== '.' && ISDIGIT (s
[1])))
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';
2008 for (++s
; ISDIGIT (*s
); ++s
)
2009 t2
= t2
* 10 + *s
- '0';
2012 s
= set_ordering (s
, key
, bl_end
);
2014 badfieldspec (argv
[i
]);
2021 s
= set_ordering (s
, &gkey
, bl_both
);
2035 error (SORT_FAILURE
, 0,
2036 _("option `-k' requires an argument"));
2042 key
= (struct keyfield
*)
2043 xmalloc (sizeof (struct keyfield
));
2047 badfieldspec (argv
[i
]);
2048 for (t
= 0; ISDIGIT (*s
); ++s
)
2049 t
= 10 * t
+ *s
- '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
]);
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';
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
]);
2086 s
= set_ordering (s
, key
, bl_start
);
2093 badfieldspec (argv
[i
]);
2096 /* Skip over comma. */
2100 /* Provoke with `sort -k1,' */
2101 error (0, 0, _("field specification has `,' but \
2102 lacks following field spec"));
2103 badfieldspec (argv
[i
]);
2106 for (t
= 0; ISDIGIT (*s
); ++s
)
2107 t
= t
* 10 + *s
- '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
]);
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';
2131 /* `-k 2,3' is equivalent to `+1 -3'. */
2136 s
= set_ordering (s
, key
, bl_end
);
2138 badfieldspec (argv
[i
]);
2152 error (SORT_FAILURE
, 0,
2153 _("option `-o' requires an argument"));
2155 outfile
= argv
[++i
];
2164 else if (i
< argc
- 1)
2170 error (SORT_FAILURE
, 0,
2171 _("option `-t' requires an argument"));
2179 temp_dir
= argv
[++i
];
2181 error (SORT_FAILURE
, 0,
2182 _("option `-T' requires an argument"));
2193 /* Accept and ignore e.g. -y0 for compatibility with
2197 fprintf (stderr
, _("%s: unrecognized option `-%c'\n"),
2199 usage (SORT_FAILURE
);
2205 else /* Not an option. */
2207 files
[nfiles
++] = argv
[i
];
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
))
2235 reverse
= gkey
.reverse
;
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
)
2271 if (S_ISREG (outstat
.st_mode
) && !STREQ (outfile
, files
[i
]))
2274 if ((STREQ (files
[i
], "-")
2275 ? fstat (STDIN_FILENO
, &instat
)
2276 : stat (files
[i
], &instat
)) != 0)
2278 error (0, errno
, "%s", files
[i
]);
2280 exit (SORT_FAILURE
);
2282 if (S_ISREG (instat
.st_mode
) && !SAME_INODE (instat
, outstat
))
2284 /* We know the files are distinct. */
2289 in_fp
= xfopen (files
[i
], "r");
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
);
2297 error (0, errno
, "%s", files
[i
]);
2299 exit (SORT_FAILURE
);
2305 ofp
= xfopen (outfile
, "w");
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
)
2317 if (STREQ (files
[i
], "-"))
2319 if (stat (files
[i
], &sb
))
2321 error (0, errno
, "%s", files
[i
]);
2323 exit (SORT_FAILURE
);
2327 ofp
= xfopen (outfile
, "w");
2336 merge (files
, nfiles
, ofp
, outfile
);
2338 sort (files
, nfiles
, ofp
, outfile
);
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
);