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