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