annotate toys/sort.c @ 299:b142219d828f

Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005). Still a few bugs. Needs a _biiiiig_ test suite...
author Rob Landley <rob@landley.net>
date Wed, 18 Jun 2008 19:31:32 -0500
parents
children 87d624e86785
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
299
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * sort.c - put input lines into order
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2004, 2008 Rob Landley <rob@landley.net>
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
8
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_SORT(NEWTOY(sort, USE_SORT_BIG("S:T:m" "o:k*t:bgMcszdfi") "run", TOYFLAG_USR|TOYFLAG_BIN))
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
10
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config SORT
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "sort"
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: sort [-run] [FILE...]
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
16
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Sort all lines of text from input files (or stdin) to stdout.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
18
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -r reverse
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -u unique lines only
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -n numeric order (instead of alphabetical)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
22
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
23 config SORT_BIG
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
24 bool " all SuSv3 options (Support -ktcsbdfiozgM)"
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
25 default y
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
26 depends on SORT
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
27 help
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
28 usage: sort [-bcdfgiMsz] [-k#[,#[x]] [-t X]] [-o FILE]
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
29
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
30 -b ignore leading blanks (or trailing blanks in second part of key)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
31 -c check whether input is sorted
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 -d dictionary order (use alphanumeric and whitespace chars only)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
33 -f force uppercase (case insensitive sort)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 -g general numeric sort (double precision with nan and inf)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
35 -i ignore nonprinting characters
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
36 -M month sort (jan, feb, etc).
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
37 -s skip fallback sort (only sort with keys)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
38 -z zero (null) terminated input
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
39 -k sort by "key" (see below)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
40 -t use a key separator other than whitespace
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
41 -o output to FILE instead of stdout
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
42
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
43 This version of sort requires floating point.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
44
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
45 Sorting by key looks at a subset of the words on each line. -k2
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
46 uses the second word to the end of the line, -k2,2 looks at only
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
47 the second word, -k2,4 looks from the start of the second to the end
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
48 of the fourth word. Specifying multiple keys uses the later keys as
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
49 tie breakers, in order. A type specifier appended to a sort key
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
50 (such as -2,2n) applies only to sorting that key.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
51 */
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
52
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
53 #include "toys.h"
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
54 #include <math.h>
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
55
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
56 DEFINE_GLOBALS(
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
57 char *key_separator;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
58 struct arg_list *raw_keys;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
59 char *outfile;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
60 char *ignore1, ignore2; // GNU compatability NOPs for -S and -T.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
61
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
62 void *key_list;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
63 int linecount;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
64 char **lines;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
65 )
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
66
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
67 #define TT this.sort
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
68
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
69 // The sort types are n, g, and M.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
70 // u, c, s, and z apply to top level only, not to keys.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
71 // b at top level implies bb.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
72 // The remaining options can be applied to search keys.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
73
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
74 #define FLAG_n 1 // Sort type: numeric
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
75 #define FLAG_u 2 // Unique
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
76 #define FLAG_r 4 // Reverse output order
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
77
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
78 #define FLAG_i 8 // Ignore !isprint()
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
79 #define FLAG_f 16 // Force uppercase
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
80 #define FLAG_d 32 // Ignore !(isalnum()|isspace())
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
81 #define FLAG_z 64 // Input is null terminated, not \n
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
82 #define FLAG_s 128 // Stable sort, no ascii fallback at end
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
83 #define FLAG_c 256 // Check only. No output, exit(!ordered)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
84 #define FLAG_M 512 // Sort type: date
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
85 #define FLAG_g 1024 // Sort type: strtod()
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
86 #define FLAG_b 2048 // Ignore leading blanks
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
87
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
88 // Left off dealing with FLAG_b/FLAG_bb logic...
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
89
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
90 #define FLAG_bb 32768 // Ignore trailing blanks
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
91
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
92 struct sort_key
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
93 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
94 struct sort_key *next_key; // linked list
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
95 unsigned range[4]; // start word, start char, end word, end char
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
96 int flags;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
97 };
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
98
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
99 // Copy of the part of this string corresponding to a key/flags.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
100
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
101 static char *get_key_data(char *str, struct sort_key *key, int flags)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
102 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
103 int start=0, end, len, i, j;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
104
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
105 // Special case whole string, so we don't have to make a copy
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
106
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
107 if(key->range[0]==1 && !key->range[1] && !key->range[2] && !key->range[3]
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
108 && !(flags&(FLAG_b&FLAG_d&FLAG_f&FLAG_i&FLAG_bb))) return str;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
109
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
110 // Find start of key on first pass, end on second pass
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
111
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
112 len = strlen(str);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
113 for (j=0; j<2; j++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
114 if (!key->range[2*j]) end=len;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
115
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
116 // Loop through fields
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
117 else {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
118 end=0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
119 for (i=1; i < key->range[2*j]+j; i++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
120
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
121 // Skip leading blanks
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
122 if (str[end] && !TT.key_separator)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
123 while (isspace(str[end])) end++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
124
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
125 // Skip body of key
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
126 for (; str[end]; end++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
127 if (TT.key_separator) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
128 if (str[end]==*TT.key_separator) break;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
129 } else if (isspace(str[end])) break;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
130 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
131 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
132 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
133 if (!j) start=end;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
134 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
135
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
136 // Key with explicit separator starts after the separator
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
137 if (TT.key_separator && str[start]==*TT.key_separator) start++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
138
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
139 // Strip leading and trailing whitespace if necessary
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
140 if (flags&FLAG_b) while (isspace(str[start])) start++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
141 if (flags&FLAG_bb) while (end>start && isspace(str[end-1])) end--;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
142
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
143 // Handle offsets on start and end
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
144 if (key->range[3]) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
145 end += key->range[3]-1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
146 if (end>len) end=len;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
147 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
148 if (key->range[1]) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
149 start += key->range[1]-1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
150 if (start>len) start=len;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
151 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
152
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
153 // Make the copy
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
154 if (end<start) end=start;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
155 str = xstrndup(str+start, end-start);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
156
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
157 // Handle -d
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
158 if (flags&FLAG_d) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
159 for (start = end = 0; str[end]; end++)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
160 if (isspace(str[end]) || isalnum(str[end])) str[start++] = str[end];
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
161 str[start] = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
162 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
163
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
164 // Handle -i
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
165 if (flags&FLAG_i) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
166 for (start = end = 0; str[end]; end++)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
167 if (isprint(str[end])) str[start++] = str[end];
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
168 str[start] = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
169 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
170
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
171 // Handle -f
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
172 if (flags*FLAG_f) for(i=0; str[i]; i++) str[i] = toupper(str[i]);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
173
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
174 return str;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
175 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
176
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
177 // append a sort_key to key_list.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
178
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
179 static struct sort_key *add_key(void)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
180 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
181 void **stupid_compiler = &TT.key_list;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
182 struct sort_key **pkey = (struct sort_key **)stupid_compiler;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
183
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
184 while (*pkey) pkey = &((*pkey)->next_key);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
185 return *pkey = xzalloc(sizeof(struct sort_key));
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
186 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
187
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
188 // Perform actual comparison
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
189 static int compare_values(int flags, char *x, char *y)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
190 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
191 int ff = flags & (FLAG_n|FLAG_g|FLAG_M);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
192
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
193 // Ascii sort
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
194 if (!ff) return strcmp(x, y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
195
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
196 if (CFG_SORT_BIG && ff == FLAG_g) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
197 char *xx,*yy;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
198 double dx = strtod(x,&xx), dy = strtod(y,&yy);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
199 int xinf, yinf;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
200
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
201 // not numbers < NaN < -infinity < numbers < +infinity)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
202
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
203 if (x==xx) return y==yy ? 0 : -1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
204 if (y==yy) return 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
205
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
206 // Check for isnan
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
207 if (dx!=dx) return (dy!=dy) ? 0 : -1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
208 if (dy!=dy) return 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
209
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
210 // Check for infinity. (Could underflow, but avoids needing libm.)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
211 xinf = (1.0/dx == 0.0);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
212 yinf = (1.0/dy == 0.0);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
213 if (xinf) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
214 if(dx<0) return (yinf && dy<0) ? 0 : -1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
215 return (yinf && dy>0) ? 0 : 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
216 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
217 if (yinf) return dy<0 ? 1 : -1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
218
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
219 return dx>dy ? 1 : (dx<dy ? -1 : 0);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
220 } else if (CFG_SORT_BIG && ff == FLAG_M) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
221 struct tm thyme;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
222 int dx;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
223 char *xx,*yy;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
224
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
225 xx = strptime(x,"%b",&thyme);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
226 dx = thyme.tm_mon;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
227 yy = strptime(y,"%b",&thyme);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
228 if (!xx) return !yy ? 0 : -1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
229 else if (!yy) return 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
230 else return dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
231
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
232 // This has to be ff == FLAG_n
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
233 } else {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
234 // Full floating point version of -n
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
235 if (CFG_SORT_BIG) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
236 double dx = atof(x), dy = atof(y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
237
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
238 return dx>dy ? 1 : (dx<dy ? -1 : 0);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
239 // Integer version of -n for tiny systems
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
240 } else return atoi(x)-atoi(y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
241 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
242 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
243
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
244
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
245 // Callback from qsort(): Iterate through key_list and perform comparisons.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
246 static int compare_keys(const void *xarg, const void *yarg)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
247 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
248 int flags = toys.optflags, retval = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
249 char *x, *y, **xx = (char **)xarg, **yy = (char **)yarg;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
250 struct sort_key *key;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
251
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
252 if (CFG_SORT_BIG) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
253 for (key=(struct sort_key *)TT.key_list; !retval && key;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
254 key = key->next_key)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
255 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
256 flags = (key->flags) ? key->flags : toys.optflags;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
257
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
258 // Chop out and modify key chunks, handling -dfib
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
259
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
260 x = get_key_data(*xx, key, flags);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
261 y = get_key_data(*yy, key, flags);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
262
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
263 retval = compare_values(flags, x, y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
264
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
265 // Free the copies get_key_data() made.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
266
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
267 if (x != *xx) free(x);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
268 if (y != *yy) free(y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
269
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
270 if (retval) break;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
271 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
272 } else retval = compare_values(flags, *xx, *yy);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
273
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
274 // Perform fallback sort if necessary
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
275
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
276 if (!retval && !(toys.optflags&FLAG_s)) retval = strcmp(*xx, *yy);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
277
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
278 return retval * ((flags&FLAG_r) ? -1 : 1);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
279 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
280
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
281 // Callback from loopfiles to handle input files.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
282 static void sort_read(int fd, char *name)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
283 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
284 // Read each line from file, appending to a big array.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
285
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
286 for (;;) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
287 char * line = (CFG_SORT_BIG && (toys.optflags&FLAG_z))
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
288 ? get_rawline(fd, NULL, 0) : get_line(fd);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
289
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
290 if (!line) break;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
291
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
292 // handle -c here so we don't allocate more memory than necessary.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
293 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
294 int j = (toys.optflags&FLAG_u) ? -1 : 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
295
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
296 if (TT.linecount && compare_keys((char *)TT.lines,line)>j)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
297 error_exit("%s: Check line %d\n", name, TT.linecount);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
298
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
299 if (TT.lines) free(TT.lines);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
300 else TT.linecount = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
301 TT.lines = (char **)line;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
302 } else {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
303 if (!(TT.linecount&63))
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
304 TT.lines = xrealloc(TT.lines, sizeof(char *)*(TT.linecount+64));
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
305 TT.lines[TT.linecount] = line;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
306 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
307 TT.linecount++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
308 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
309 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
310
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
311 void sort_main(void)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
312 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
313 int idx, fd = 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
314
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
315 // Open output file if necessary.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
316 if (CFG_SORT_BIG && TT.outfile)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
317 fd = xcreate(TT.outfile, O_CREAT|O_TRUNC|O_WRONLY, 0666);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
318
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
319 // Parse -k sort keys.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
320 if (CFG_SORT_BIG && TT.raw_keys) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
321 struct arg_list *arg;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
322
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
323 for (arg = TT.raw_keys; arg; arg = arg->next) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
324 struct sort_key *key = add_key();
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
325 char *temp;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
326 int flag;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
327
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
328 idx = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
329 temp = arg->arg;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
330 while (*temp) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
331 // Start of range
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
332 key->range[2*idx] = (unsigned)strtol(temp, &temp, 10);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
333 if (*temp=='.')
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
334 key->range[(2*idx)+1] = (unsigned)strtol(temp+1, &temp, 10);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
335
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
336 // Handle flags appended to a key type.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
337 for (;*temp;temp++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
338 char *temp2, *optlist;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
339
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
340 // Note that a second comma becomes an "Unknown key" error.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
341
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
342 if (*temp==',' && !idx++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
343 temp++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
344 break;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
345 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
346
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
347 // Which flag is this?
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
348
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
349 optlist = toys.which->options;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
350 temp2 = index(optlist, *temp);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
351 flag = (1<<(optlist-temp2+strlen(optlist)-1));
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
352
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
353 // Was it a flag that can apply to a key?
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
354
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
355 if (!temp2 || flag>FLAG_b
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
356 || (flag&(FLAG_u|FLAG_c|FLAG_s|FLAG_z)))
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
357 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
358 error_exit("Unknown key option.");
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
359 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
360 // b after , means strip _trailing_ space, not leading.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
361 if (idx && flag==FLAG_b) flag = FLAG_bb;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
362 key->flags |= flag;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
363 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
364 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
365 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
366 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
367
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
368 // global b flag strips both leading and trailing spaces
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
369 if (toys.optflags&FLAG_b) toys.optflags |= FLAG_bb;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
370
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
371 // If no keys, perform alphabetic sort over the whole line.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
372 if (CFG_SORT_BIG && !TT.key_list) add_key()->range[0] = 1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
373
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
374 // Open input files and read data, populating TT.lines[TT.linecount]
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
375 loopfiles(toys.optargs, sort_read);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
376
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
377 // The compare (-c) logic was handled in sort_read(),
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
378 // so if we got here, we're done.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
379 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) return;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
380
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
381 // Perform the actual sort
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
382 qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
383
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
384 // handle unique (-u)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
385 if (toys.optflags&FLAG_u) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
386 int jdx;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
387
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
388 for (jdx=0, idx=1; idx<TT.linecount; idx++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
389 if (!compare_keys(&TT.lines[jdx], &TT.lines[idx]))
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
390 free(TT.lines[idx]);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
391 else TT.lines[++jdx] = TT.lines[idx];
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
392 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
393 if (TT.linecount) TT.linecount = jdx+1;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
394 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
395
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
396 // Output result
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
397 for (idx = 0; idx<TT.linecount; idx++) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
398 char *s = TT.lines[idx];
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
399 xwrite(fd, s, strlen(s));
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
400 if (CFG_TOYBOX_FREE) free(s);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
401 xwrite(fd, "\n", 1);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
402 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
403
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
404 if (CFG_TOYBOX_FREE) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
405 if (fd != 1) close(fd);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
406 free(TT.lines);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
407 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
408 }