annotate toys/sort.c @ 533:31215cc6c9f2

Consolidate headers.
author Rob Landley <rob@landley.net>
date Wed, 07 Mar 2012 19:04:50 -0600
parents 55d815926c5c
children 9ab5ee341d47
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
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
9 USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")USE_SORT_BIG("S:T:m" "o:k*t:xbMcszdfi") "run", TOYFLAG_USR|TOYFLAG_BIN))
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
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
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
24 bool "SuSv3 options (Support -ktcsbdfiozM)"
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
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
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
28 usage: sort [-bcdfiMsz] [-k#[,#[x]] [-t X]] [-o FILE]
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
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 -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
35 -M month sort (jan, feb, etc).
407
b8390ededd02 Add -x option to sort (like -n, but hexadecimal), because I needed it for something.
Rob Landley <rob@landley.net>
parents: 368
diff changeset
36 -x Hexadecimal numerical sort
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
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 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
44 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
45 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
46 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
47 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
48 (such as -2,2n) applies only to sorting that key.
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
49
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
50 config SORT_FLOAT
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
51 bool "Floating point (-g)"
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
52 default y
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
53 depends on SORT_BIG
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
54 help
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
55 usage: sort [-g]
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
56
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
57 This version of sort requires floating point.
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
58
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
59 -g general numeric sort (double precision with nan and inf)
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
60
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
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
b142219d828f Most 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 #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
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
65 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
66 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
67 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
68 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
69 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
70
b142219d828f Most 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 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
72 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
73 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
74 )
b142219d828f Most 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
b142219d828f Most 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 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
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 // 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
79 // 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
80 // 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
81 // 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
82
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
83 #define FLAG_n (1<<0) // Sort type: numeric
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
84 #define FLAG_u (1<<1) // Unique
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
85 #define FLAG_r (1<<2) // Reverse output order
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
86
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
87 #define FLAG_i (1<<3) // Ignore !isprint()
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
88 #define FLAG_f (1<<4) // Force uppercase
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
89 #define FLAG_d (1<<5) // Ignore !(isalnum()|isspace())
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
90 #define FLAG_z (1<<6) // Input is null terminated, not \n
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
91 #define FLAG_s (1<<7) // Stable sort, no ascii fallback at end
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
92 #define FLAG_c (1<<8) // Check only. No output, exit(!ordered)
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
93 #define FLAG_M (1<<9) // Sort type: date
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
94 #define FLAG_b (1<<10) // Ignore leading blanks
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
95 #define FLAG_x (1<<11) // Hex sort
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
96 #define FLAG_g (1<<18) // Sort type: strtod()
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
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 // 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
99
407
b8390ededd02 Add -x option to sort (like -n, but hexadecimal), because I needed it for something.
Rob Landley <rob@landley.net>
parents: 368
diff changeset
100 #define FLAG_bb (1<<31) // Ignore trailing blanks
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
101
b142219d828f Most 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 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
103 {
b142219d828f Most 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 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
105 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
106 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
107 };
b142219d828f Most 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
b142219d828f Most 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 // 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
110
b142219d828f Most 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 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
112 {
b142219d828f Most 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 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
114
b142219d828f Most 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 // 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
116
b142219d828f Most 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 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
118 && !(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
119
b142219d828f Most 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 // 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
121
b142219d828f Most 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 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
123 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
124 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
125
b142219d828f Most 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 // 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
127 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
128 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
129 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
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 // 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
132 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
133 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
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 // 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
136 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
137 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
138 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
139 } 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
140 }
b142219d828f Most 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 }
b142219d828f Most 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 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
144 }
b142219d828f Most 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
b142219d828f Most 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 // 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
147 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
148
b142219d828f Most 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 // 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
150 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
151 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
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 // 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
154 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
155 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
156 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
157 }
b142219d828f Most 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 (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
159 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
160 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
161 }
b142219d828f Most 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 // 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
164 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
165 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
166
b142219d828f Most 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 // 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
168 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
169 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
170 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
171 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
172 }
b142219d828f Most 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 // 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
175 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
176 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
177 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
178 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
179 }
b142219d828f Most 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 // 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
182 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
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 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
185 }
b142219d828f Most 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 // 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
188
b142219d828f Most 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 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
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 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
192 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
193
b142219d828f Most 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 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
195 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
196 }
b142219d828f Most 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
b142219d828f Most 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 // 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
199 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
200 {
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
201 int ff = flags & (FLAG_n|FLAG_g|FLAG_M|FLAG_x);
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
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 // 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
204 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
205
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
206 if (CFG_SORT_FLOAT && ff == FLAG_g) {
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
207 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
208 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
209 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
210
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
211 // not numbers < NaN < -infinity < numbers < +infinity
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
212
b142219d828f Most 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 (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
214 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
215
b142219d828f Most 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 // 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
217 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
218 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
219
b142219d828f Most 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 // 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
221 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
222 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
223 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
224 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
225 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
226 }
b142219d828f Most 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 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
228
b142219d828f Most 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 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
230 } 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
231 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
232 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
233 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
234
b142219d828f Most 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 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
236 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
237 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
238 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
239 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
240 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
241
407
b8390ededd02 Add -x option to sort (like -n, but hexadecimal), because I needed it for something.
Rob Landley <rob@landley.net>
parents: 368
diff changeset
242 } else if (CFG_SORT_BIG && ff == FLAG_x) {
b8390ededd02 Add -x option to sort (like -n, but hexadecimal), because I needed it for something.
Rob Landley <rob@landley.net>
parents: 368
diff changeset
243 return strtol(x, NULL, 16)-strtol(y, NULL, 16);
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
244 // This has to be ff == FLAG_n
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
245 } 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
246 // Full floating point version of -n
431
87edfe8ae99e Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents: 407
diff changeset
247 if (CFG_SORT_FLOAT) {
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
248 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
249
b142219d828f Most 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 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
251 // 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
252 } 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
253 }
b142219d828f Most 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 }
b142219d828f Most 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
b142219d828f Most 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 // 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
258 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
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 int flags = toys.optflags, retval = 0;
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
261 char *x, *y, *xx = *(char **)xarg, *yy = *(char **)yarg;
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
262 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
263
b142219d828f Most 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 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
265 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
266 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
267 {
302
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
268 flags = key->flags ? key->flags : toys.optflags;
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
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 // 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
271
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
272 x = get_key_data(xx, key, flags);
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
273 y = get_key_data(yy, key, flags);
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
274
b142219d828f Most 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 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
276
b142219d828f Most 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 // 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
278
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
279 if (x != xx) free(x);
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
280 if (y != yy) free(y);
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
281
b142219d828f Most 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 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
283 }
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
284 } else retval = compare_values(flags, xx, yy);
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
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 // Perform fallback sort if necessary
302
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
287 if (!retval && !(CFG_SORT_BIG && (toys.optflags&FLAG_s))) {
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
288 retval = strcmp(xx, yy);
302
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
289 flags = toys.optflags;
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
290 }
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
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 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
293 }
b142219d828f Most 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
b142219d828f Most 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 // 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
296 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
297 {
b142219d828f Most 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 // 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
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
300 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
301 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
302 ? 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
303
b142219d828f Most 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 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
305
b142219d828f Most 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 // 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
307 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
308 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
309
460
55d815926c5c Tweak from Frank Bergmann: some broken optimizers complain about "type-punned pointers", so typecast to void * instead to shut it up. (Up there with the "may be used uninitialized" gripes.)
Rob Landley <rob@landley.net>
parents: 433
diff changeset
310 if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
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
311 error_exit("%s: Check line %d\n", name, TT.linecount);
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
312 free(TT.lines);
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
313 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
314 } 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
315 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
316 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
317 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
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 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
320 }
b142219d828f Most 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 }
b142219d828f Most 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 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
324 {
b142219d828f Most 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 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
326
b142219d828f Most 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 // 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
328 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
329 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
330
b142219d828f Most 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 // 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
332 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
333 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
334
b142219d828f Most 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 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
336 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
337 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
338 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
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 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
341 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
342 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
343 // 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
344 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
345 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
346 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
347
b142219d828f Most 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 // 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
349 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
350 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
351
b142219d828f Most 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 // 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
353
b142219d828f Most 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 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
355 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
356 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
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
b142219d828f Most 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 // 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
360
b142219d828f Most 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 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
362 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
363 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
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 // 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
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 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
368 || (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
369 {
b142219d828f Most 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 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
371 }
b142219d828f Most 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 // 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
373 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
374 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
375 }
b142219d828f Most 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 }
b142219d828f Most 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 }
b142219d828f Most 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
b142219d828f Most 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 // 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
381 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
382
b142219d828f Most 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 // 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
384 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
385
b142219d828f Most 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 // 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
387 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
388
b142219d828f Most 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 // 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
390 // so if we got here, we're done.
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
391 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) goto exit_now;
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
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 // 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
394 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
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 // 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
397 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
398 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
399
b142219d828f Most 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 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
401 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
402 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
403 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
404 }
b142219d828f Most 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 (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
406 }
b142219d828f Most 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 // 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
409 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
410 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
411 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
412 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
413 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
414 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
415
433
9e7aaecf0683 Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents: 431
diff changeset
416 exit_now:
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
417 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
418 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
419 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
420 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
421 }