xref: /illumos-gate/usr/src/cmd/sort/main.c (revision 101e15b5f8a77d9433805e541996abaabc9ca8c1)
1*101e15b5SRichard Lowe /*
2*101e15b5SRichard Lowe  * CDDL HEADER START
3*101e15b5SRichard Lowe  *
4*101e15b5SRichard Lowe  * The contents of this file are subject to the terms of the
5*101e15b5SRichard Lowe  * Common Development and Distribution License, Version 1.0 only
6*101e15b5SRichard Lowe  * (the "License").  You may not use this file except in compliance
7*101e15b5SRichard Lowe  * with the License.
8*101e15b5SRichard Lowe  *
9*101e15b5SRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*101e15b5SRichard Lowe  * or http://www.opensolaris.org/os/licensing.
11*101e15b5SRichard Lowe  * See the License for the specific language governing permissions
12*101e15b5SRichard Lowe  * and limitations under the License.
13*101e15b5SRichard Lowe  *
14*101e15b5SRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
15*101e15b5SRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*101e15b5SRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
17*101e15b5SRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
18*101e15b5SRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
19*101e15b5SRichard Lowe  *
20*101e15b5SRichard Lowe  * CDDL HEADER END
21*101e15b5SRichard Lowe  */
22*101e15b5SRichard Lowe /*
23*101e15b5SRichard Lowe  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24*101e15b5SRichard Lowe  * Use is subject to license terms.
25*101e15b5SRichard Lowe  */
26*101e15b5SRichard Lowe 
27*101e15b5SRichard Lowe /*
28*101e15b5SRichard Lowe  * Overview of sort(1)
29*101e15b5SRichard Lowe  *
30*101e15b5SRichard Lowe  * sort(1) implements a robust sorting program, compliant with the POSIX
31*101e15b5SRichard Lowe  * specifications for sort, that is capable of handling large sorts and merges
32*101e15b5SRichard Lowe  * in single byte and multibyte locales.  Like most sort(1) implementations,
33*101e15b5SRichard Lowe  * this implementation uses an internal algorithm for sorting subsets of the
34*101e15b5SRichard Lowe  * requested data set and an external algorithm for sorting the subsets into the
35*101e15b5SRichard Lowe  * final output.  In the current implementation, the internal algorithm is a
36*101e15b5SRichard Lowe  * ternary radix quicksort, modified from the algorithm described in Bentley and
37*101e15b5SRichard Lowe  * Sedgewick [1], while the external algorithm is a priority-queue based
38*101e15b5SRichard Lowe  * heapsort, as outlined in Sedgewick [2].
39*101e15b5SRichard Lowe  *
40*101e15b5SRichard Lowe  * We use three major datatypes, defined in ./types.h: the line record,
41*101e15b5SRichard Lowe  * line_rec_t; the stream, stream_t; and the field definition, field_t.
42*101e15b5SRichard Lowe  * Because sort supports efficient code paths for each of the C, single-byte,
43*101e15b5SRichard Lowe  * and wide character/multibyte locales, each of these types contains unions
44*101e15b5SRichard Lowe  * and/or function pointers to describe appropriate properties or operations for
45*101e15b5SRichard Lowe  * each locale type.
46*101e15b5SRichard Lowe  *
47*101e15b5SRichard Lowe  * To utilize the radix quicksort algorithm with the potentially complex sort
48*101e15b5SRichard Lowe  * keys definable via the POSIX standard, we convert each line to a collatable
49*101e15b5SRichard Lowe  * string based on the key definition.  This approach is somewhat different from
50*101e15b5SRichard Lowe  * historical implementations of sort(1), which have built a complex
51*101e15b5SRichard Lowe  * field-by-field comparison function.  There are, of course, tradeoffs that
52*101e15b5SRichard Lowe  * accompany this decision, particularly when the duration of use of a given
53*101e15b5SRichard Lowe  * collated form is short.  However, the maintenance costs of parallel
54*101e15b5SRichard Lowe  * conversion and collation functions are estimated to be high, and the
55*101e15b5SRichard Lowe  * performance costs of a shared set of functions were found to be excessive in
56*101e15b5SRichard Lowe  * prototype.
57*101e15b5SRichard Lowe  *
58*101e15b5SRichard Lowe  * [1]	J. Bentley and R. Sedgewick, Fast Algorithms for Sorting and Searching
59*101e15b5SRichard Lowe  *	Strings, in Eighth Annual ACM-SIAM Symposium on Discrete Algorithms,
60*101e15b5SRichard Lowe  *	1997 (SODA 1997),
61*101e15b5SRichard Lowe  * [2]	R. Sedgewick, Algorithms in C, 3rd ed., vol. 1, Addison-Wesley, 1998.
62*101e15b5SRichard Lowe  */
63*101e15b5SRichard Lowe 
64*101e15b5SRichard Lowe #include "main.h"
65*101e15b5SRichard Lowe 
66*101e15b5SRichard Lowe static sort_t S;
67*101e15b5SRichard Lowe 
68*101e15b5SRichard Lowe int
main(int argc,char * argv[])69*101e15b5SRichard Lowe main(int argc, char *argv[])
70*101e15b5SRichard Lowe {
71*101e15b5SRichard Lowe 	initialize_pre(&S);
72*101e15b5SRichard Lowe 
73*101e15b5SRichard Lowe 	if (options(&S, argc, argv))
74*101e15b5SRichard Lowe 		return (2);
75*101e15b5SRichard Lowe 
76*101e15b5SRichard Lowe 	initialize_post(&S);
77*101e15b5SRichard Lowe 
78*101e15b5SRichard Lowe 	if (S.m_check_if_sorted_only)
79*101e15b5SRichard Lowe 		check_if_sorted(&S);
80*101e15b5SRichard Lowe 
81*101e15b5SRichard Lowe 	if (!S.m_merge_only)
82*101e15b5SRichard Lowe 		internal_sort(&S);
83*101e15b5SRichard Lowe 
84*101e15b5SRichard Lowe 	merge(&S);
85*101e15b5SRichard Lowe 
86*101e15b5SRichard Lowe 	return (0);
87*101e15b5SRichard Lowe }
88