1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61#define FOR_sort
62#include "toys.h"
63
64GLOBALS(
65 char *key_separator;
66 struct arg_list *raw_keys;
67 char *outfile;
68 char *ignore1, ignore2;
69
70 void *key_list;
71 int linecount;
72 char **lines;
73)
74
75
76
77
78
79
80#define FLAG_bb (1<<31)
81
82struct sort_key
83{
84 struct sort_key *next_key;
85 unsigned range[4];
86 int flags;
87};
88
89
90
91static char *get_key_data(char *str, struct sort_key *key, int flags)
92{
93 int start=0, end, len, i, j;
94
95
96
97 if(key->range[0]==1 && !key->range[1] && !key->range[2] && !key->range[3]
98 && !(flags&(FLAG_b|FLAG_d|FLAG_i|FLAG_bb))) return str;
99
100
101
102 len = strlen(str);
103 for (j=0; j<2; j++) {
104 if (!key->range[2*j]) end=len;
105
106
107 else {
108 end=0;
109 for (i=1; i < key->range[2*j]+j; i++) {
110
111
112 if (str[end] && !TT.key_separator)
113 while (isspace(str[end])) end++;
114
115
116 for (; str[end]; end++) {
117 if (TT.key_separator) {
118 if (str[end]==*TT.key_separator) {
119 end++;
120 break;
121 }
122 } else if (isspace(str[end])) break;
123 }
124 }
125 }
126 if (!j) start=end;
127 }
128
129
130 if (TT.key_separator && str[start]==*TT.key_separator) start++;
131
132
133 if (flags&FLAG_b) while (isspace(str[start])) start++;
134 if (flags&FLAG_bb) while (end>start && isspace(str[end-1])) end--;
135
136
137 if (key->range[3]) {
138 end += key->range[3]-1;
139 if (end>len) end=len;
140 }
141 if (key->range[1]) {
142 start += key->range[1]-1;
143 if (start>len) start=len;
144 }
145
146
147 if (end<start) end=start;
148 str = xstrndup(str+start, end-start);
149
150
151 if (flags&FLAG_d) {
152 for (start = end = 0; str[end]; end++)
153 if (isspace(str[end]) || isalnum(str[end])) str[start++] = str[end];
154 str[start] = 0;
155 }
156
157
158 if (flags&FLAG_i) {
159 for (start = end = 0; str[end]; end++)
160 if (isprint(str[end])) str[start++] = str[end];
161 str[start] = 0;
162 }
163
164 return str;
165}
166
167
168
169static struct sort_key *add_key(void)
170{
171 void **stupid_compiler = &TT.key_list;
172 struct sort_key **pkey = (struct sort_key **)stupid_compiler;
173
174 while (*pkey) pkey = &((*pkey)->next_key);
175 return *pkey = xzalloc(sizeof(struct sort_key));
176}
177
178
179static int compare_values(int flags, char *x, char *y)
180{
181 int ff = flags & (FLAG_n|FLAG_g|FLAG_M|FLAG_x);
182
183
184 if (!ff) return ((flags&FLAG_f) ? strcasecmp : strcmp)(x, y);
185
186 if (CFG_SORT_FLOAT && ff == FLAG_g) {
187 char *xx,*yy;
188 double dx = strtod(x,&xx), dy = strtod(y,&yy);
189 int xinf, yinf;
190
191
192
193 if (x==xx) return y==yy ? 0 : -1;
194 if (y==yy) return 1;
195
196
197 if (dx!=dx) return (dy!=dy) ? 0 : -1;
198 if (dy!=dy) return 1;
199
200
201 xinf = (1.0/dx == 0.0);
202 yinf = (1.0/dy == 0.0);
203 if (xinf) {
204 if(dx<0) return (yinf && dy<0) ? 0 : -1;
205 return (yinf && dy>0) ? 0 : 1;
206 }
207 if (yinf) return dy<0 ? 1 : -1;
208
209 return dx>dy ? 1 : (dx<dy ? -1 : 0);
210 } else if (CFG_SORT_BIG && ff == FLAG_M) {
211 struct tm thyme;
212 int dx;
213 char *xx,*yy;
214
215 xx = strptime(x,"%b",&thyme);
216 dx = thyme.tm_mon;
217 yy = strptime(y,"%b",&thyme);
218 if (!xx) return !yy ? 0 : -1;
219 else if (!yy) return 1;
220 else return dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon;
221
222 } else if (CFG_SORT_BIG && ff == FLAG_x) {
223 return strtol(x, NULL, 16)-strtol(y, NULL, 16);
224
225 } else {
226
227 if (CFG_SORT_FLOAT) {
228 double dx = atof(x), dy = atof(y);
229
230 return dx>dy ? 1 : (dx<dy ? -1 : 0);
231
232 } else return atoi(x)-atoi(y);
233 }
234}
235
236
237static int compare_keys(const void *xarg, const void *yarg)
238{
239 int flags = toys.optflags, retval = 0;
240 char *x, *y, *xx = *(char **)xarg, *yy = *(char **)yarg;
241 struct sort_key *key;
242
243 if (CFG_SORT_BIG) {
244 for (key=(struct sort_key *)TT.key_list; !retval && key;
245 key = key->next_key)
246 {
247 flags = key->flags ? key->flags : toys.optflags;
248
249
250
251 x = get_key_data(xx, key, flags);
252 y = get_key_data(yy, key, flags);
253
254 retval = compare_values(flags, x, y);
255
256
257
258 if (x != xx) free(x);
259 if (y != yy) free(y);
260
261 if (retval) break;
262 }
263 } else retval = compare_values(flags, xx, yy);
264
265
266
267 if (!retval && !(CFG_SORT_BIG && (toys.optflags&FLAG_s))) {
268 flags = toys.optflags;
269 retval = strcmp(xx, yy);
270 }
271
272 return retval * ((flags&FLAG_r) ? -1 : 1);
273}
274
275
276static void sort_read(int fd, char *name)
277{
278
279
280 for (;;) {
281 char * line = (CFG_SORT_BIG && (toys.optflags&FLAG_z))
282 ? get_rawline(fd, NULL, 0) : get_line(fd);
283
284 if (!line) break;
285
286
287 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) {
288 int j = (toys.optflags&FLAG_u) ? -1 : 0;
289
290 if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
291 error_exit("%s: Check line %d\n", name, TT.linecount);
292 free(TT.lines);
293 TT.lines = (char **)line;
294 } else {
295 if (!(TT.linecount&63))
296 TT.lines = xrealloc(TT.lines, sizeof(char *)*(TT.linecount+64));
297 TT.lines[TT.linecount] = line;
298 }
299 TT.linecount++;
300 }
301}
302
303void sort_main(void)
304{
305 int idx, fd = 1;
306
307
308 if (CFG_SORT_BIG && TT.outfile)
309 fd = xcreate(TT.outfile, O_CREAT|O_TRUNC|O_WRONLY, 0666);
310
311
312 if (CFG_SORT_BIG && TT.raw_keys) {
313 struct arg_list *arg;
314
315 for (arg = TT.raw_keys; arg; arg = arg->next) {
316 struct sort_key *key = add_key();
317 char *temp;
318 int flag;
319
320 idx = 0;
321 temp = arg->arg;
322 while (*temp) {
323
324 key->range[2*idx] = (unsigned)strtol(temp, &temp, 10);
325 if (*temp=='.')
326 key->range[(2*idx)+1] = (unsigned)strtol(temp+1, &temp, 10);
327
328
329 for (;*temp;temp++) {
330 char *temp2, *optlist;
331
332
333
334 if (*temp==',' && !idx++) {
335 temp++;
336 break;
337 }
338
339
340
341 optlist = toys.which->options;
342 temp2 = strchr(optlist, *temp);
343 flag = (1<<(optlist-temp2+strlen(optlist)-1));
344
345
346
347 if (!temp2 || flag>FLAG_b
348 || (flag&(FLAG_u|FLAG_c|FLAG_s|FLAG_z)))
349 {
350 error_exit("Unknown key option.");
351 }
352
353 if (idx && flag==FLAG_b) flag = FLAG_bb;
354 key->flags |= flag;
355 }
356 }
357 }
358 }
359
360
361 if (toys.optflags&FLAG_b) toys.optflags |= FLAG_bb;
362
363
364 if (CFG_SORT_BIG && !TT.key_list) add_key()->range[0] = 1;
365
366
367 loopfiles(toys.optargs, sort_read);
368
369
370
371 if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) goto exit_now;
372
373
374 qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys);
375
376
377 if (toys.optflags&FLAG_u) {
378 int jdx;
379
380 for (jdx=0, idx=1; idx<TT.linecount; idx++) {
381 if (!compare_keys(&TT.lines[jdx], &TT.lines[idx]))
382 free(TT.lines[idx]);
383 else TT.lines[++jdx] = TT.lines[idx];
384 }
385 if (TT.linecount) TT.linecount = jdx+1;
386 }
387
388
389 for (idx = 0; idx<TT.linecount; idx++) {
390 char *s = TT.lines[idx];
391 unsigned i = strlen(s);
392
393 if (!(toys.optflags&FLAG_z)) s[i] = '\n';
394 xwrite(fd, s, i+1);
395 if (CFG_TOYBOX_FREE) free(s);
396 }
397
398exit_now:
399 if (CFG_TOYBOX_FREE) {
400 if (fd != 1) close(fd);
401 free(TT.lines);
402 }
403}
404