annotate toys/sort.c @ 407:b8390ededd02

Add -x option to sort (like -n, but hexadecimal), because I needed it for something.
author Rob Landley <rob@landley.net>
date Wed, 18 Jan 2012 20:31:59 -0600
parents f90c9600a1f4
children 87edfe8ae99e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
299
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * sort.c - put input lines into order
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2004, 2008 Rob Landley <rob@landley.net>
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
8
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
9 USE_SORT(NEWTOY(sort, USE_SORT_BIG("S:T:m" "o:k*t:xbgMcszdfi") "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
368
f90c9600a1f4 menuconfig indentation should be handled by dependencies, not whitespace in description.
Rob Landley <rob@landley.net>
parents: 302
diff changeset
24 bool "all SuSv3 options (Support -ktcsbdfiozgM)"
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
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
28 usage: sort [-bcdfgiMsz] [-k#[,#[x]] [-t X]] [-o FILE]
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
29
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
30 -b ignore leading blanks (or trailing blanks in second part of key)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
31 -c check whether input is sorted
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 -d dictionary order (use alphanumeric and whitespace chars only)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
33 -f force uppercase (case insensitive sort)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 -g general numeric sort (double precision with nan and inf)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
35 -i ignore nonprinting characters
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
36 -M month sort (jan, feb, etc).
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
37 -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
38 -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
39 -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
40 -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
41 -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
42 -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
43
b142219d828f Most 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 This version of sort requires floating point.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
45
b142219d828f Most 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 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
47 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
48 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
49 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
50 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
51 (such as -2,2n) applies only to sorting that key.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
52 */
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
53
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
54 #include "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
55 #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
56
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 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
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 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
64 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
65 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
66 )
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
67
b142219d828f Most 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 #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
69
b142219d828f Most 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 // 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
71 // 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
72 // 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
73 // 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
74
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
75 #define FLAG_n 1 // Sort type: numeric
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
76 #define FLAG_u 2 // Unique
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
77 #define FLAG_r 4 // Reverse output order
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
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 #define FLAG_i 8 // Ignore !isprint()
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
80 #define FLAG_f 16 // Force uppercase
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
81 #define FLAG_d 32 // Ignore !(isalnum()|isspace())
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
82 #define FLAG_z 64 // Input is null terminated, not \n
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
83 #define FLAG_s 128 // Stable sort, no ascii fallback at end
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
84 #define FLAG_c 256 // Check only. No output, exit(!ordered)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
85 #define FLAG_M 512 // Sort type: date
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
86 #define FLAG_g 1024 // Sort type: strtod()
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
87 #define FLAG_b 2048 // Ignore leading blanks
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
88 #define FLAG_x 4096 // Hex 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
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 // 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
91
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
92 #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
93
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
94 struct sort_key
b142219d828f Most 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 {
b142219d828f Most 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 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
97 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
98 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
99 };
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
100
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
101 // 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
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 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
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 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
106
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
107 // 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
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 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
110 && !(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
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 // 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
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 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
115 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
116 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
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 // 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
119 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
120 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
121 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
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 // 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
124 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
125 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
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 // 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
128 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
129 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
130 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
131 } 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
132 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
133 }
b142219d828f Most 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 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
136 }
b142219d828f Most 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
b142219d828f Most 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 // 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
139 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
140
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
141 // 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
142 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
143 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
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 // 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
146 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
147 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
148 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
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 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
151 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
152 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
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
b142219d828f Most 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 // 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
156 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
157 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
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 // 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
160 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
161 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
162 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
163 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
164 }
b142219d828f Most 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 // 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
167 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
168 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
169 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
170 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
171 }
b142219d828f Most 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 // 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
174 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
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 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
177 }
b142219d828f Most 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 // 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
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 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
182 {
b142219d828f Most 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 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
184 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
185
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
186 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
187 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
188 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
189
b142219d828f Most 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 // 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
191 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
192 {
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
193 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
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 // 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
196 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
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 if (CFG_SORT_BIG && ff == FLAG_g) {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
199 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
200 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
201 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
202
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
203 // not numbers < NaN < -infinity < numbers < +infinity)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
204
b142219d828f Most 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 (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
206 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
207
b142219d828f Most 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 // 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
209 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
210 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
211
b142219d828f Most 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 // 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
213 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
214 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
215 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
216 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
217 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
218 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
219 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
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 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
222 } 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
223 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
224 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
225 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
226
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
227 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
228 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
229 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
230 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
231 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
232 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
233
b142219d828f Most 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 // This has to be ff == FLAG_n
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
235 } 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
236 return strtol(x, NULL, 16)-strtol(y, NULL, 16);
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
237 } 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
238 // Full floating point version of -n
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
239 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
240 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
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 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
243 // 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
244 } 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
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 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
247
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
248
b142219d828f Most 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 // 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
250 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
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 int flags = toys.optflags, retval = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
253 char *x, *y, **xx = (char **)xarg, **yy = (char **)yarg;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
254 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
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 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
257 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
258 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
259 {
302
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
260 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
261
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
262 // 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
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 x = get_key_data(*xx, key, flags);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
265 y = get_key_data(*yy, key, flags);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
266
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
267 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
268
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
269 // 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
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 if (x != *xx) free(x);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
272 if (y != *yy) free(y);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
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 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
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 } else retval = compare_values(flags, *xx, *yy);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
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 // Perform fallback sort if necessary
302
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
279 if (!retval && !(CFG_SORT_BIG && (toys.optflags&FLAG_s))) {
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
280 retval = strcmp(*xx, *yy);
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
281 flags = toys.optflags;
87d624e86785 Only apply global flags to fallback sort.
Rob Landley <rob@landley.net>
parents: 299
diff changeset
282 }
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
283
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
284 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
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
b142219d828f Most 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 // 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
288 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
289 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
290 // 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
291
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
292 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
293 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
294 ? 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
295
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
296 if (!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
297
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
298 // 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
299 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
300 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
301
b142219d828f Most 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 if (TT.linecount && compare_keys((char *)TT.lines,line)>j)
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
303 error_exit("%s: Check line %d\n", name, TT.linecount);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
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 (TT.lines) free(TT.lines);
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
306 else TT.linecount = 0;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
307 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
308 } 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
309 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
310 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
311 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
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 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
314 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
315 }
b142219d828f Most 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 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
318 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
319 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
320
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
321 // 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
322 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
323 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
324
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
325 // 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
326 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
327 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
328
b142219d828f Most 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 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
330 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
331 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
332 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
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 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
335 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
336 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
337 // 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
338 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
339 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
340 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
341
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
342 // 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
343 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
344 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
345
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
346 // 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
347
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
348 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
349 temp++;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
350 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
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
b142219d828f Most 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 // 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
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 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
356 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
357 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
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 // 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
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 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
362 || (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
363 {
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
364 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
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 // 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
367 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
368 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
369 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
370 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
371 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
372 }
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
373
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
374 // 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
375 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
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 // 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
378 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
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 // 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
381 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
382
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
383 // 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
384 // so if we got here, we're done.
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
385 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) return;
b142219d828f Most of an susv3 compliant sort implementation (loosely based on the one I wrote back in 2005).
Rob Landley <rob@landley.net>
parents:
diff changeset
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 // 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
388 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
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 // 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
391 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
392 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
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 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
395 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
396 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
397 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
398 }
b142219d828f Most 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 (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
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
b142219d828f Most 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 // 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
403 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
404 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
405 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
406 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
407 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
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
b142219d828f Most 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 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
411 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
412 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
413 }
b142219d828f Most 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 }