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