Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/sort.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/sort.c
r1765 r2725 7 7 * MAINTAINER: Rob Landley <rob@landley.net> 8 8 * 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.9 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 10 10 * 11 11 * See SuS3 sort standard at: … … 33 33 FLAG_c = 0x10, /* Check: no output, exit(!ordered) */ 34 34 FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */ 35 FLAG_z = 0x40, /* Input is nullterminated, not \n */35 FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */ 36 36 /* These can be applied to search keys, the previous four can't */ 37 37 FLAG_b = 0x80, /* Ignore leading blanks */ … … 53 53 54 54 static struct sort_key { 55 struct sort_key *next_key; 56 unsigned range[4]; 55 struct sort_key *next_key; /* linked list */ 56 unsigned range[4]; /* start word, start char, end word, end char */ 57 57 unsigned flags; 58 58 } *key_list; … … 60 60 static char *get_key(char *str, struct sort_key *key, int flags) 61 61 { 62 int start = 0, end = 0, len, i, j; 62 int start = 0, end = 0, len, j; 63 unsigned i; 63 64 64 65 /* Special case whole string, so we don't have to make a copy */ … … 128 129 if (flags & FLAG_i) { 129 130 for (start = end = 0; str[end]; end++) 130 if (isprint (str[end]))131 if (isprint_asciionly(str[end])) 131 132 str[start++] = str[end]; 132 133 str[start] = '\0'; … … 151 152 ((option_mask32 & FLAG_z) \ 152 153 ? bb_get_chunk_from_file(fp, NULL) \ 153 : xmalloc_ getline(fp))154 : xmalloc_fgetline(fp)) 154 155 #else 155 #define GET_LINE(fp) xmalloc_ getline(fp)156 #define GET_LINE(fp) xmalloc_fgetline(fp) 156 157 #endif 157 158 … … 275 276 #endif 276 277 277 int sort_main(int argc, char **argv) ;278 int sort_main(int argc , char **argv)278 int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 279 int sort_main(int argc UNUSED_PARAM, char **argv) 279 280 { 280 FILE *fp, *outfile = stdout; 281 char *line, **lines = NULL; 281 char *line, **lines; 282 282 char *str_ignored, *str_o, *str_t; 283 283 llist_t *lst_k = NULL; 284 284 int i, flag; 285 int linecount = 0; 285 int linecount; 286 unsigned opts; 286 287 287 288 xfunc_error_retval = 2; … … 289 290 /* Parse command line options */ 290 291 /* -o and -t can be given at most once */ 291 opt_complementary = "o--o:t--t:" /* -t, -o: maximumone of each */292 opt_complementary = "o--o:t--t:" /* -t, -o: at most one of each */ 292 293 "k::"; /* -k takes list */ 293 getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t); 294 #if ENABLE_FEATURE_SORT_BIG 295 if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w"); 296 if (option_mask32 & FLAG_t) { 294 opts = getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t); 295 /* global b strips leading and trailing spaces */ 296 if (opts & FLAG_b) 297 option_mask32 |= FLAG_bb; 298 #if ENABLE_FEATURE_SORT_BIG 299 if (opts & FLAG_t) { 297 300 if (!str_t[0] || str_t[1]) 298 301 bb_error_msg_and_die("bad -t parameter"); 299 302 key_separator = str_t[0]; 300 303 } 304 /* note: below this point we use option_mask32, not opts, 305 * since that reduces register pressure and makes code smaller */ 306 301 307 /* parse sort key */ 302 308 while (lst_k) { … … 314 320 }; 315 321 struct sort_key *key = add_key(); 316 char *str_k = lst_k->data; 317 const char *temp2; 322 char *str_k = llist_pop(&lst_k); 318 323 319 324 i = 0; /* i==0 before comma, 1 after (-k3,6) */ … … 327 332 } 328 333 while (*str_k) { 334 const char *temp2; 335 329 336 if (*str_k == ',' && !i++) { 330 337 str_k++; 331 338 break; 332 339 } /* no else needed: fall through to syntax error 333 340 because comma isn't in OPT_STR */ 334 341 temp2 = strchr(OPT_STR, *str_k); 335 342 if (!temp2) … … 339 346 bb_error_msg_and_die("unknown sort type"); 340 347 /* b after ',' means strip _trailing_ space */ 341 if (i && flag == FLAG_b) flag = FLAG_bb; 348 if (i && flag == FLAG_b) 349 flag = FLAG_bb; 342 350 key->flags |= flag; 343 351 str_k++; 344 352 } 345 353 } 346 /* leaking lst_k... */ 347 lst_k = lst_k->link; 348 } 349 #endif 350 /* global b strips leading and trailing spaces */ 351 if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb; 354 } 355 #endif 352 356 353 357 /* Open input files and read data */ 354 for (i = argv[optind] ? optind : optind-1; argv[i]; i++) { 355 fp = stdin; 356 if (i >= optind && NOT_LONE_DASH(argv[i])) 357 fp = xfopen(argv[i], "r"); 358 argv += optind; 359 if (!*argv) 360 *--argv = (char*)"-"; 361 linecount = 0; 362 lines = NULL; 363 do { 364 /* coreutils 6.9 compat: abort on first open error, 365 * do not continue to next file: */ 366 FILE *fp = xfopen_stdin(*argv); 358 367 for (;;) { 359 368 line = GET_LINE(fp); 360 if (!line) break;361 if (!(linecount & 63))362 lines = xrealloc(lines, sizeof(char *) * (linecount + 64));369 if (!line) 370 break; 371 lines = xrealloc_vector(lines, 6, linecount); 363 372 lines[linecount++] = line; 364 373 } 365 fclose(fp); 366 } 374 fclose_if_not_stdin(fp); 375 } while (*++argv); 376 367 377 #if ENABLE_FEATURE_SORT_BIG 368 378 /* if no key, perform alphabetic sort */ … … 372 382 if (option_mask32 & FLAG_c) { 373 383 int j = (option_mask32 & FLAG_u) ? -1 : 0; 374 for (i = 1; i < linecount; i++) 384 for (i = 1; i < linecount; i++) { 375 385 if (compare_keys(&lines[i-1], &lines[i]) > j) { 376 fprintf(stderr, "Check line % d\n", i);377 return 1;386 fprintf(stderr, "Check line %u\n", i); 387 return EXIT_FAILURE; 378 388 } 379 return 0; 389 } 390 return EXIT_SUCCESS; 380 391 } 381 392 #endif 382 393 /* Perform the actual sort */ 383 qsort(lines, linecount, sizeof( char *), compare_keys);394 qsort(lines, linecount, sizeof(lines[0]), compare_keys); 384 395 /* handle -u */ 385 396 if (option_mask32 & FLAG_u) { … … 389 400 option_mask32 |= FLAG_s; 390 401 for (i = 1; i < linecount; i++) { 391 if ( !compare_keys(&lines[flag], &lines[i]))402 if (compare_keys(&lines[flag], &lines[i]) == 0) 392 403 free(lines[i]); 393 404 else 394 405 lines[++flag] = lines[i]; 395 406 } 396 if (linecount) linecount = flag+1; 397 } 407 if (linecount) 408 linecount = flag+1; 409 } 410 398 411 /* Print it */ 412 #if ENABLE_FEATURE_SORT_BIG 413 /* Open output file _after_ we read all input ones */ 414 if (option_mask32 & FLAG_o) 415 xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); 416 #endif 417 flag = (option_mask32 & FLAG_z) ? '\0' : '\n'; 399 418 for (i = 0; i < linecount; i++) 400 fprintf(outfile, "%s\n", lines[i]);419 printf("%s%c", lines[i], flag); 401 420 402 421 fflush_stdout_and_exit(EXIT_SUCCESS);
Note:
See TracChangeset
for help on using the changeset viewer.