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