Mercurial > hg > toybox
annotate toys/sort.c @ 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.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 13 Feb 2012 21:48:22 -0600 |
parents | 9e7aaecf0683 |
children | 31215cc6c9f2 |
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 #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
|
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 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 |
b142219d828f
Most 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 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
|
73 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
|
74 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
|
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 |
b142219d828f
Most 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 #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
|
78 |
b142219d828f
Most 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 // 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
|
80 // 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
|
81 // 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
|
82 // 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
|
83 |
431
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_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
|
85 #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
|
86 #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
|
87 |
431
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_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
|
89 #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
|
90 #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
|
91 #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
|
92 #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
|
93 #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
|
94 #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
|
95 #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
|
96 #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
|
97 #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
|
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 // 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
|
100 |
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
|
101 #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
|
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 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
|
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 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
|
106 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
|
107 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
|
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 |
b142219d828f
Most 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 // 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
|
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 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
|
113 { |
b142219d828f
Most 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 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
|
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 // 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
|
117 |
b142219d828f
Most 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 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
|
119 && !(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
|
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 // 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
|
122 |
b142219d828f
Most 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 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
|
124 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
|
125 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
|
126 |
b142219d828f
Most 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 // 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
|
128 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
|
129 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
|
130 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
|
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 // 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
|
133 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
|
134 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
|
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 // 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
|
137 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
|
138 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
|
139 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
|
140 } 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
|
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 } |
b142219d828f
Most 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 (!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
|
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 |
b142219d828f
Most 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 // 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
|
148 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
|
149 |
b142219d828f
Most 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 // 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
|
151 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
|
152 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
|
153 |
b142219d828f
Most 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 // 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
|
155 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
|
156 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
|
157 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
|
158 } |
b142219d828f
Most 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 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
|
160 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
|
161 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
|
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 // 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
|
165 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
|
166 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
|
167 |
b142219d828f
Most 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 // 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
|
169 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
|
170 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
|
171 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
|
172 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
|
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 |
b142219d828f
Most 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 // 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
|
176 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
|
177 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
|
178 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
|
179 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
|
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 |
b142219d828f
Most 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 // 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
|
183 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
|
184 |
b142219d828f
Most 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 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
|
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 // 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
|
189 |
b142219d828f
Most 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 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
|
191 { |
b142219d828f
Most 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 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
|
193 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
|
194 |
b142219d828f
Most 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 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
|
196 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
|
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 |
b142219d828f
Most 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 // 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
|
200 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
|
201 { |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
202 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
|
203 |
b142219d828f
Most 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 // 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
|
205 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
|
206 |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
207 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
|
208 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
|
209 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
|
210 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
|
211 |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
212 // 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
|
213 |
b142219d828f
Most 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 (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
|
215 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
|
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 // 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
|
218 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
|
219 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
|
220 |
b142219d828f
Most 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 // 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 } |
b142219d828f
Most 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 (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
|
229 |
b142219d828f
Most 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 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
|
231 } 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
|
232 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
|
233 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
|
234 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
|
235 |
b142219d828f
Most 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 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 |
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
|
243 } 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
|
244 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
|
245 // 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
|
246 } 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
|
247 // 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
|
248 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
|
249 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
|
250 |
b142219d828f
Most 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 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
|
252 // 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
|
253 } 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
|
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 |
b142219d828f
Most 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 // 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
|
259 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
|
260 { |
b142219d828f
Most 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 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
|
262 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
|
263 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
|
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 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
|
266 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
|
267 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
|
268 { |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
269 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
|
270 |
b142219d828f
Most 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 // 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
|
272 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
273 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
|
274 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
|
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 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
|
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 // 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
|
279 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
280 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
|
281 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
|
282 |
b142219d828f
Most 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 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
|
284 } |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
285 } 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
|
286 |
b142219d828f
Most 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 // Perform fallback sort if necessary |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
288 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
|
289 retval = strcmp(xx, yy); |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
290 flags = toys.optflags; |
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
291 } |
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
|
292 |
b142219d828f
Most 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 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
|
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 |
b142219d828f
Most 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 // 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
|
297 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
|
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 // 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
|
300 |
b142219d828f
Most 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 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
|
302 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
|
303 ? 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
|
304 |
b142219d828f
Most 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 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
|
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 // 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
|
308 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
|
309 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
|
310 |
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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 } 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
|
316 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
|
317 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
|
318 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
|
319 } |
b142219d828f
Most 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 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
|
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 |
b142219d828f
Most 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 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
|
325 { |
b142219d828f
Most 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 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
|
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 // 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
|
329 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
|
330 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
|
331 |
b142219d828f
Most 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 // 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
|
333 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
|
334 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
|
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 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
|
337 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
|
338 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
|
339 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
|
340 |
b142219d828f
Most 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 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
|
342 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
|
343 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
|
344 // 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
|
345 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
|
346 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
|
347 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
|
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 // 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
|
350 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
|
351 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
|
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 // 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
|
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 (*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
|
356 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
|
357 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
|
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 |
b142219d828f
Most 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 // 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
|
361 |
b142219d828f
Most 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 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
|
363 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
|
364 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
|
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 // 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
|
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 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
|
369 || (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
|
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 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
|
372 } |
b142219d828f
Most 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 // 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
|
374 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
|
375 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
|
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 |
b142219d828f
Most 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 // 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
|
382 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
|
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 // 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
|
385 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
|
386 |
b142219d828f
Most 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 // 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
|
388 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
|
389 |
b142219d828f
Most 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 // 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
|
391 // 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
|
392 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
|
393 |
b142219d828f
Most 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 // 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
|
395 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
|
396 |
b142219d828f
Most 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 // 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
|
398 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
|
399 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
|
400 |
b142219d828f
Most 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 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
|
402 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
|
403 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
|
404 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
|
405 } |
b142219d828f
Most 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 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
|
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 |
b142219d828f
Most 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 // 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
416 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
417 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
|
418 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
|
419 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
|
420 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
|
421 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
422 } |