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