Mercurial > hg > toybox
annotate toys/posix/sort.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
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 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/007904975/utilities/sort.html |
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
|
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 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
63 #define FOR_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
|
64 #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
|
65 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
66 GLOBALS( |
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
|
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 // 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
|
78 // 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
|
79 // 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
|
80 // 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
|
81 |
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
|
82 #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
|
83 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 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
|
85 { |
b142219d828f
Most 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 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
|
87 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
|
88 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
|
89 }; |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 // 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
|
92 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 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
|
94 { |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 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
|
96 |
b142219d828f
Most 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 // 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
|
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 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
|
100 && !(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
|
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 // 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
|
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 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
|
105 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
|
106 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
|
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 // 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
|
109 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
|
110 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
|
111 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
|
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 // 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
|
114 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
|
115 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
|
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 // 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
|
118 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
|
119 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
|
120 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
|
121 } 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
|
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 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 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
|
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 |
b142219d828f
Most 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 // 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
|
129 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
|
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 // 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
|
132 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
|
133 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
|
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 // 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
|
136 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
|
137 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
|
138 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
|
139 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 if (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
|
141 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
|
142 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
|
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 |
b142219d828f
Most 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 // 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
|
146 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
|
147 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
|
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 // 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 } |
b142219d828f
Most 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 |
b142219d828f
Most 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 // 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
|
157 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
|
158 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
|
159 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
|
160 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
|
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 // 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
|
164 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
|
165 |
b142219d828f
Most 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 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
|
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 |
b142219d828f
Most 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 // 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
|
170 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 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
|
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 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
|
174 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
|
175 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 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
|
177 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
|
178 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 |
b142219d828f
Most 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 // 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
|
181 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
|
182 { |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
183 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
|
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 // 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
|
186 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
|
187 |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 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
|
192 |
431
87edfe8ae99e
Bugfix for -x, add CONFIG_SORT_FLOAT, and new test suite entry.
Rob Landley <rob@landley.net>
parents:
407
diff
changeset
|
193 // 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
|
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 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
|
196 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
|
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 // 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
|
199 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
|
200 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
|
201 |
b142219d828f
Most 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 // 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 } |
b142219d828f
Most 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 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
|
210 |
b142219d828f
Most 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 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
|
212 } 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
|
213 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
|
214 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
|
215 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
|
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 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 |
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
|
224 } 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
|
225 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
|
226 // 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
|
227 } 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
|
228 // 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
|
229 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
|
230 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
|
231 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 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
|
233 // 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
|
234 } 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
|
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 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
238 |
b142219d828f
Most 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 // 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
|
240 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
|
241 { |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
242 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
|
243 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
|
244 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
|
245 |
b142219d828f
Most 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 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
|
247 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
|
248 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
|
249 { |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
250 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
|
251 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
252 // 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
|
253 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
254 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
|
255 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
|
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 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
|
258 |
b142219d828f
Most 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 // 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
|
260 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
261 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
|
262 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
|
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 (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
|
265 } |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
266 } 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
|
267 |
b142219d828f
Most 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 // Perform fallback sort if necessary |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
269 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
|
270 retval = strcmp(xx, yy); |
302
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
271 flags = toys.optflags; |
87d624e86785
Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents:
299
diff
changeset
|
272 } |
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
|
273 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 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
|
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 |
b142219d828f
Most 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 // 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
|
278 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
|
279 { |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
280 // 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
|
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 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
|
283 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
|
284 ? 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
|
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 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
|
287 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 // 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
|
289 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
|
290 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
|
291 |
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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 } 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
|
297 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
|
298 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
|
299 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
|
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 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
|
302 } |
b142219d828f
Most 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 |
b142219d828f
Most 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 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
|
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 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
|
308 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
309 // 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
|
310 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
|
311 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
|
312 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 // 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
|
314 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
|
315 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
|
316 |
b142219d828f
Most 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 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
|
318 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
|
319 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
|
320 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
|
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 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
|
323 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
|
324 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
|
325 // 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
|
326 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
|
327 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
|
328 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
|
329 |
b142219d828f
Most 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 // 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
|
331 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
|
332 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
|
333 |
b142219d828f
Most 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 // 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
|
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 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
|
337 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 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
|
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 |
b142219d828f
Most 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 // 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
|
342 |
b142219d828f
Most 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 optlist = toys.which->options; |
538
9ab5ee341d47
Replace deprecated libc function with its totally renamed equivalent.
Rob Landley <rob@landley.net>
parents:
533
diff
changeset
|
344 temp2 = strchr(optlist, *temp); |
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
|
345 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
|
346 |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 // 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
|
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 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
|
350 || (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
|
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 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
|
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 // 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
|
355 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
|
356 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
|
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 } |
b142219d828f
Most 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 |
b142219d828f
Most 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 // 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
|
363 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
|
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 // 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
|
366 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
|
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 // 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
|
369 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
|
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 // 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
|
372 // 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
|
373 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
|
374 |
b142219d828f
Most 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 // 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
|
376 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
|
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 // 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
|
379 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
|
380 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
|
381 |
b142219d828f
Most 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 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
|
383 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
|
384 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
|
385 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
|
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 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
|
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 |
b142219d828f
Most 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 // 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
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 |
433
9e7aaecf0683
Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
Rob Landley <rob@landley.net>
parents:
431
diff
changeset
|
398 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
|
399 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
|
400 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
|
401 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
|
402 } |
b142219d828f
Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
403 } |