Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/sort.c
Parent Directory | Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 10898 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 10898 byte(s)
-updated to busybox-1.13.4
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * SuS3 compliant sort implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 2004 by Rob Landley <rob@landley.net> | ||
6 | * | ||
7 | * MAINTAINER: Rob Landley <rob@landley.net> | ||
8 | * | ||
9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
10 | * | ||
11 | * See SuS3 sort standard at: | ||
12 | * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html | ||
13 | */ | ||
14 | |||
15 | niro | 816 | #include "libbb.h" |
16 | niro | 532 | |
17 | niro | 816 | /* This is a NOEXEC applet. Be very careful! */ |
18 | |||
19 | |||
20 | niro | 532 | /* |
21 | sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...] | ||
22 | sort -c [-bdfinru][-t char][-k keydef][file] | ||
23 | */ | ||
24 | |||
25 | /* These are sort types */ | ||
26 | niro | 816 | static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:"; |
27 | niro | 532 | enum { |
28 | FLAG_n = 1, /* Numeric sort */ | ||
29 | FLAG_g = 2, /* Sort using strtod() */ | ||
30 | FLAG_M = 4, /* Sort date */ | ||
31 | /* ucsz apply to root level only, not keys. b at root level implies bb */ | ||
32 | FLAG_u = 8, /* Unique */ | ||
33 | FLAG_c = 0x10, /* Check: no output, exit(!ordered) */ | ||
34 | FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */ | ||
35 | niro | 816 | FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */ |
36 | niro | 532 | /* These can be applied to search keys, the previous four can't */ |
37 | FLAG_b = 0x80, /* Ignore leading blanks */ | ||
38 | FLAG_r = 0x100, /* Reverse */ | ||
39 | FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */ | ||
40 | FLAG_f = 0x400, /* Force uppercase */ | ||
41 | FLAG_i = 0x800, /* Ignore !isprint() */ | ||
42 | FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */ | ||
43 | FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */ | ||
44 | FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */ | ||
45 | FLAG_o = 0x8000, | ||
46 | FLAG_k = 0x10000, | ||
47 | FLAG_t = 0x20000, | ||
48 | FLAG_bb = 0x80000000, /* Ignore trailing blanks */ | ||
49 | }; | ||
50 | |||
51 | #if ENABLE_FEATURE_SORT_BIG | ||
52 | static char key_separator; | ||
53 | |||
54 | static struct sort_key { | ||
55 | struct sort_key *next_key; /* linked list */ | ||
56 | unsigned range[4]; /* start word, start char, end word, end char */ | ||
57 | unsigned flags; | ||
58 | } *key_list; | ||
59 | |||
60 | static char *get_key(char *str, struct sort_key *key, int flags) | ||
61 | { | ||
62 | niro | 816 | int start = 0, end = 0, len, j; |
63 | unsigned i; | ||
64 | niro | 532 | |
65 | /* Special case whole string, so we don't have to make a copy */ | ||
66 | if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3] | ||
67 | && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb)) | ||
68 | ) { | ||
69 | return str; | ||
70 | } | ||
71 | |||
72 | /* Find start of key on first pass, end on second pass */ | ||
73 | len = strlen(str); | ||
74 | for (j = 0; j < 2; j++) { | ||
75 | if (!key->range[2*j]) | ||
76 | end = len; | ||
77 | /* Loop through fields */ | ||
78 | else { | ||
79 | end = 0; | ||
80 | for (i = 1; i < key->range[2*j] + j; i++) { | ||
81 | if (key_separator) { | ||
82 | /* Skip body of key and separator */ | ||
83 | while (str[end]) { | ||
84 | if (str[end++] == key_separator) | ||
85 | break; | ||
86 | } | ||
87 | } else { | ||
88 | /* Skip leading blanks */ | ||
89 | while (isspace(str[end])) | ||
90 | end++; | ||
91 | /* Skip body of key */ | ||
92 | while (str[end]) { | ||
93 | if (isspace(str[end])) | ||
94 | break; | ||
95 | end++; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | if (!j) start = end; | ||
101 | } | ||
102 | /* Strip leading whitespace if necessary */ | ||
103 | //XXX: skip_whitespace() | ||
104 | if (flags & FLAG_b) | ||
105 | while (isspace(str[start])) start++; | ||
106 | /* Strip trailing whitespace if necessary */ | ||
107 | if (flags & FLAG_bb) | ||
108 | while (end > start && isspace(str[end-1])) end--; | ||
109 | /* Handle offsets on start and end */ | ||
110 | if (key->range[3]) { | ||
111 | end += key->range[3] - 1; | ||
112 | if (end > len) end = len; | ||
113 | } | ||
114 | if (key->range[1]) { | ||
115 | start += key->range[1] - 1; | ||
116 | if (start > len) start = len; | ||
117 | } | ||
118 | /* Make the copy */ | ||
119 | if (end < start) end = start; | ||
120 | str = xstrndup(str+start, end-start); | ||
121 | /* Handle -d */ | ||
122 | if (flags & FLAG_d) { | ||
123 | for (start = end = 0; str[end]; end++) | ||
124 | if (isspace(str[end]) || isalnum(str[end])) | ||
125 | str[start++] = str[end]; | ||
126 | str[start] = '\0'; | ||
127 | } | ||
128 | /* Handle -i */ | ||
129 | if (flags & FLAG_i) { | ||
130 | for (start = end = 0; str[end]; end++) | ||
131 | if (isprint(str[end])) | ||
132 | str[start++] = str[end]; | ||
133 | str[start] = '\0'; | ||
134 | } | ||
135 | /* Handle -f */ | ||
136 | if (flags & FLAG_f) | ||
137 | for (i = 0; str[i]; i++) | ||
138 | str[i] = toupper(str[i]); | ||
139 | |||
140 | return str; | ||
141 | } | ||
142 | |||
143 | static struct sort_key *add_key(void) | ||
144 | { | ||
145 | struct sort_key **pkey = &key_list; | ||
146 | while (*pkey) | ||
147 | pkey = &((*pkey)->next_key); | ||
148 | return *pkey = xzalloc(sizeof(struct sort_key)); | ||
149 | } | ||
150 | |||
151 | #define GET_LINE(fp) \ | ||
152 | ((option_mask32 & FLAG_z) \ | ||
153 | ? bb_get_chunk_from_file(fp, NULL) \ | ||
154 | niro | 816 | : xmalloc_fgetline(fp)) |
155 | niro | 532 | #else |
156 | niro | 816 | #define GET_LINE(fp) xmalloc_fgetline(fp) |
157 | niro | 532 | #endif |
158 | |||
159 | /* Iterate through keys list and perform comparisons */ | ||
160 | static int compare_keys(const void *xarg, const void *yarg) | ||
161 | { | ||
162 | int flags = option_mask32, retval = 0; | ||
163 | char *x, *y; | ||
164 | |||
165 | #if ENABLE_FEATURE_SORT_BIG | ||
166 | struct sort_key *key; | ||
167 | |||
168 | for (key = key_list; !retval && key; key = key->next_key) { | ||
169 | flags = key->flags ? key->flags : option_mask32; | ||
170 | /* Chop out and modify key chunks, handling -dfib */ | ||
171 | x = get_key(*(char **)xarg, key, flags); | ||
172 | y = get_key(*(char **)yarg, key, flags); | ||
173 | #else | ||
174 | /* This curly bracket serves no purpose but to match the nesting | ||
175 | level of the for () loop we're not using */ | ||
176 | { | ||
177 | x = *(char **)xarg; | ||
178 | y = *(char **)yarg; | ||
179 | #endif | ||
180 | /* Perform actual comparison */ | ||
181 | switch (flags & 7) { | ||
182 | default: | ||
183 | bb_error_msg_and_die("unknown sort type"); | ||
184 | break; | ||
185 | /* Ascii sort */ | ||
186 | case 0: | ||
187 | #if ENABLE_LOCALE_SUPPORT | ||
188 | retval = strcoll(x, y); | ||
189 | #else | ||
190 | retval = strcmp(x, y); | ||
191 | #endif | ||
192 | break; | ||
193 | #if ENABLE_FEATURE_SORT_BIG | ||
194 | case FLAG_g: { | ||
195 | char *xx, *yy; | ||
196 | double dx = strtod(x, &xx); | ||
197 | double dy = strtod(y, &yy); | ||
198 | /* not numbers < NaN < -infinity < numbers < +infinity) */ | ||
199 | if (x == xx) | ||
200 | retval = (y == yy ? 0 : -1); | ||
201 | else if (y == yy) | ||
202 | retval = 1; | ||
203 | /* Check for isnan */ | ||
204 | else if (dx != dx) | ||
205 | retval = (dy != dy) ? 0 : -1; | ||
206 | else if (dy != dy) | ||
207 | retval = 1; | ||
208 | /* Check for infinity. Could underflow, but it avoids libm. */ | ||
209 | else if (1.0 / dx == 0.0) { | ||
210 | if (dx < 0) | ||
211 | retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1; | ||
212 | else | ||
213 | retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1; | ||
214 | } else if (1.0 / dy == 0.0) | ||
215 | retval = (dy < 0) ? 1 : -1; | ||
216 | else | ||
217 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0); | ||
218 | break; | ||
219 | } | ||
220 | case FLAG_M: { | ||
221 | struct tm thyme; | ||
222 | int dx; | ||
223 | char *xx, *yy; | ||
224 | |||
225 | xx = strptime(x, "%b", &thyme); | ||
226 | dx = thyme.tm_mon; | ||
227 | yy = strptime(y, "%b", &thyme); | ||
228 | if (!xx) | ||
229 | retval = (!yy) ? 0 : -1; | ||
230 | else if (!yy) | ||
231 | retval = 1; | ||
232 | else | ||
233 | retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon; | ||
234 | break; | ||
235 | } | ||
236 | /* Full floating point version of -n */ | ||
237 | case FLAG_n: { | ||
238 | double dx = atof(x); | ||
239 | double dy = atof(y); | ||
240 | retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0); | ||
241 | break; | ||
242 | } | ||
243 | } /* switch */ | ||
244 | /* Free key copies. */ | ||
245 | if (x != *(char **)xarg) free(x); | ||
246 | if (y != *(char **)yarg) free(y); | ||
247 | /* if (retval) break; - done by for () anyway */ | ||
248 | #else | ||
249 | /* Integer version of -n for tiny systems */ | ||
250 | case FLAG_n: | ||
251 | retval = atoi(x) - atoi(y); | ||
252 | break; | ||
253 | } /* switch */ | ||
254 | #endif | ||
255 | } /* for */ | ||
256 | |||
257 | /* Perform fallback sort if necessary */ | ||
258 | if (!retval && !(option_mask32 & FLAG_s)) | ||
259 | retval = strcmp(*(char **)xarg, *(char **)yarg); | ||
260 | |||
261 | if (flags & FLAG_r) return -retval; | ||
262 | return retval; | ||
263 | } | ||
264 | |||
265 | #if ENABLE_FEATURE_SORT_BIG | ||
266 | static unsigned str2u(char **str) | ||
267 | { | ||
268 | unsigned long lu; | ||
269 | if (!isdigit((*str)[0])) | ||
270 | bb_error_msg_and_die("bad field specification"); | ||
271 | lu = strtoul(*str, str, 10); | ||
272 | if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu) | ||
273 | bb_error_msg_and_die("bad field specification"); | ||
274 | return lu; | ||
275 | } | ||
276 | #endif | ||
277 | |||
278 | niro | 816 | int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
279 | int sort_main(int argc UNUSED_PARAM, char **argv) | ||
280 | niro | 532 | { |
281 | FILE *fp, *outfile = stdout; | ||
282 | char *line, **lines = NULL; | ||
283 | niro | 816 | char *str_ignored, *str_o, *str_t; |
284 | llist_t *lst_k = NULL; | ||
285 | niro | 532 | int i, flag; |
286 | int linecount = 0; | ||
287 | |||
288 | xfunc_error_retval = 2; | ||
289 | |||
290 | /* Parse command line options */ | ||
291 | /* -o and -t can be given at most once */ | ||
292 | niro | 816 | opt_complementary = "o--o:t--t:" /* -t, -o: maximum one of each */ |
293 | "k::"; /* -k takes list */ | ||
294 | getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t); | ||
295 | niro | 532 | #if ENABLE_FEATURE_SORT_BIG |
296 | niro | 816 | if (option_mask32 & FLAG_o) outfile = xfopen_for_write(str_o); |
297 | niro | 532 | if (option_mask32 & FLAG_t) { |
298 | if (!str_t[0] || str_t[1]) | ||
299 | bb_error_msg_and_die("bad -t parameter"); | ||
300 | key_separator = str_t[0]; | ||
301 | } | ||
302 | /* parse sort key */ | ||
303 | niro | 816 | while (lst_k) { |
304 | niro | 532 | enum { |
305 | FLAG_allowed_for_k = | ||
306 | FLAG_n | /* Numeric sort */ | ||
307 | FLAG_g | /* Sort using strtod() */ | ||
308 | FLAG_M | /* Sort date */ | ||
309 | FLAG_b | /* Ignore leading blanks */ | ||
310 | FLAG_r | /* Reverse */ | ||
311 | FLAG_d | /* Ignore !(isalnum()|isspace()) */ | ||
312 | FLAG_f | /* Force uppercase */ | ||
313 | FLAG_i | /* Ignore !isprint() */ | ||
314 | 0 | ||
315 | }; | ||
316 | struct sort_key *key = add_key(); | ||
317 | niro | 816 | char *str_k = llist_pop(&lst_k); |
318 | niro | 532 | const char *temp2; |
319 | |||
320 | i = 0; /* i==0 before comma, 1 after (-k3,6) */ | ||
321 | while (*str_k) { | ||
322 | /* Start of range */ | ||
323 | /* Cannot use bb_strtou - suffix can be a letter */ | ||
324 | key->range[2*i] = str2u(&str_k); | ||
325 | if (*str_k == '.') { | ||
326 | str_k++; | ||
327 | key->range[2*i+1] = str2u(&str_k); | ||
328 | } | ||
329 | while (*str_k) { | ||
330 | if (*str_k == ',' && !i++) { | ||
331 | str_k++; | ||
332 | break; | ||
333 | } /* no else needed: fall through to syntax error | ||
334 | because comma isn't in OPT_STR */ | ||
335 | temp2 = strchr(OPT_STR, *str_k); | ||
336 | if (!temp2) | ||
337 | bb_error_msg_and_die("unknown key option"); | ||
338 | flag = 1 << (temp2 - OPT_STR); | ||
339 | if (flag & ~FLAG_allowed_for_k) | ||
340 | bb_error_msg_and_die("unknown sort type"); | ||
341 | /* b after ',' means strip _trailing_ space */ | ||
342 | if (i && flag == FLAG_b) flag = FLAG_bb; | ||
343 | key->flags |= flag; | ||
344 | str_k++; | ||
345 | } | ||
346 | } | ||
347 | } | ||
348 | #endif | ||
349 | /* global b strips leading and trailing spaces */ | ||
350 | if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb; | ||
351 | |||
352 | /* Open input files and read data */ | ||
353 | niro | 816 | argv += optind; |
354 | if (!*argv) | ||
355 | *--argv = (char*)"-"; | ||
356 | do { | ||
357 | /* coreutils 6.9 compat: abort on first open error, | ||
358 | * do not continue to next file: */ | ||
359 | fp = xfopen_stdin(*argv); | ||
360 | niro | 532 | for (;;) { |
361 | line = GET_LINE(fp); | ||
362 | if (!line) break; | ||
363 | niro | 816 | lines = xrealloc_vector(lines, 6, linecount); |
364 | niro | 532 | lines[linecount++] = line; |
365 | } | ||
366 | niro | 816 | fclose_if_not_stdin(fp); |
367 | } while (*++argv); | ||
368 | |||
369 | niro | 532 | #if ENABLE_FEATURE_SORT_BIG |
370 | /* if no key, perform alphabetic sort */ | ||
371 | if (!key_list) | ||
372 | add_key()->range[0] = 1; | ||
373 | /* handle -c */ | ||
374 | if (option_mask32 & FLAG_c) { | ||
375 | int j = (option_mask32 & FLAG_u) ? -1 : 0; | ||
376 | for (i = 1; i < linecount; i++) | ||
377 | if (compare_keys(&lines[i-1], &lines[i]) > j) { | ||
378 | fprintf(stderr, "Check line %d\n", i); | ||
379 | niro | 816 | return EXIT_FAILURE; |
380 | niro | 532 | } |
381 | niro | 816 | return EXIT_SUCCESS; |
382 | niro | 532 | } |
383 | #endif | ||
384 | /* Perform the actual sort */ | ||
385 | qsort(lines, linecount, sizeof(char *), compare_keys); | ||
386 | /* handle -u */ | ||
387 | if (option_mask32 & FLAG_u) { | ||
388 | flag = 0; | ||
389 | /* coreutils 6.3 drop lines for which only key is the same */ | ||
390 | /* -- disabling last-resort compare... */ | ||
391 | option_mask32 |= FLAG_s; | ||
392 | for (i = 1; i < linecount; i++) { | ||
393 | if (!compare_keys(&lines[flag], &lines[i])) | ||
394 | free(lines[i]); | ||
395 | else | ||
396 | lines[++flag] = lines[i]; | ||
397 | } | ||
398 | if (linecount) linecount = flag+1; | ||
399 | } | ||
400 | /* Print it */ | ||
401 | niro | 816 | flag = (option_mask32 & FLAG_z) ? '\0' : '\n'; |
402 | niro | 532 | for (i = 0; i < linecount; i++) |
403 | niro | 816 | fprintf(outfile, "%s%c", lines[i], flag); |
404 | niro | 532 | |
405 | fflush_stdout_and_exit(EXIT_SUCCESS); | ||
406 | } |