Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/coreutils/sort.c

    r1765 r2725  
    77 * MAINTAINER: Rob Landley <rob@landley.net>
    88 *
    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.
    1010 *
    1111 * See SuS3 sort standard at:
     
    3333    FLAG_c  = 0x10,         /* Check: no output, exit(!ordered) */
    3434    FLAG_s  = 0x20,         /* Stable sort, no ascii fallback at end */
    35     FLAG_z  = 0x40,         /* Input is null terminated, not \n */
     35    FLAG_z  = 0x40,         /* Input and output is NUL terminated, not \n */
    3636/* These can be applied to search keys, the previous four can't */
    3737    FLAG_b  = 0x80,         /* Ignore leading blanks */
     
    5353
    5454static struct sort_key {
    55     struct sort_key *next_key;  /* linked list */
    56     unsigned range[4];  /* start word, start char, end word, end char */
     55    struct sort_key *next_key;  /* linked list */
     56    unsigned range[4];          /* start word, start char, end word, end char */
    5757    unsigned flags;
    5858} *key_list;
     
    6060static char *get_key(char *str, struct sort_key *key, int flags)
    6161{
    62     int start = 0, end = 0, len, i, j;
     62    int start = 0, end = 0, len, j;
     63    unsigned i;
    6364
    6465    /* Special case whole string, so we don't have to make a copy */
     
    128129    if (flags & FLAG_i) {
    129130        for (start = end = 0; str[end]; end++)
    130             if (isprint(str[end]))
     131            if (isprint_asciionly(str[end]))
    131132                str[start++] = str[end];
    132133        str[start] = '\0';
     
    151152    ((option_mask32 & FLAG_z) \
    152153    ? bb_get_chunk_from_file(fp, NULL) \
    153     : xmalloc_getline(fp))
     154    : xmalloc_fgetline(fp))
    154155#else
    155 #define GET_LINE(fp) xmalloc_getline(fp)
     156#define GET_LINE(fp) xmalloc_fgetline(fp)
    156157#endif
    157158
     
    275276#endif
    276277
    277 int sort_main(int argc, char **argv);
    278 int sort_main(int argc, char **argv)
     278int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     279int sort_main(int argc UNUSED_PARAM, char **argv)
    279280{
    280     FILE *fp, *outfile = stdout;
    281     char *line, **lines = NULL;
     281    char *line, **lines;
    282282    char *str_ignored, *str_o, *str_t;
    283283    llist_t *lst_k = NULL;
    284284    int i, flag;
    285     int linecount = 0;
     285    int linecount;
     286    unsigned opts;
    286287
    287288    xfunc_error_retval = 2;
     
    289290    /* Parse command line options */
    290291    /* -o and -t can be given at most once */
    291     opt_complementary = "o--o:t--t:" /* -t, -o: maximum one of each */
     292    opt_complementary = "o--o:t--t:" /* -t, -o: at most one of each */
    292293            "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) {
    297300        if (!str_t[0] || str_t[1])
    298301            bb_error_msg_and_die("bad -t parameter");
    299302        key_separator = str_t[0];
    300303    }
     304    /* note: below this point we use option_mask32, not opts,
     305     * since that reduces register pressure and makes code smaller */
     306
    301307    /* parse sort key */
    302308    while (lst_k) {
     
    314320        };
    315321        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);
    318323
    319324        i = 0; /* i==0 before comma, 1 after (-k3,6) */
     
    327332            }
    328333            while (*str_k) {
     334                const char *temp2;
     335
    329336                if (*str_k == ',' && !i++) {
    330337                    str_k++;
    331338                    break;
    332339                } /* no else needed: fall through to syntax error
    333                      because comma isn't in OPT_STR */
     340                    because comma isn't in OPT_STR */
    334341                temp2 = strchr(OPT_STR, *str_k);
    335342                if (!temp2)
     
    339346                    bb_error_msg_and_die("unknown sort type");
    340347                /* 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;
    342350                key->flags |= flag;
    343351                str_k++;
    344352            }
    345353        }
    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
    352356
    353357    /* 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);
    358367        for (;;) {
    359368            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);
    363372            lines[linecount++] = line;
    364373        }
    365         fclose(fp);
    366     }
     374        fclose_if_not_stdin(fp);
     375    } while (*++argv);
     376
    367377#if ENABLE_FEATURE_SORT_BIG
    368378    /* if no key, perform alphabetic sort */
     
    372382    if (option_mask32 & FLAG_c) {
    373383        int j = (option_mask32 & FLAG_u) ? -1 : 0;
    374         for (i = 1; i < linecount; i++)
     384        for (i = 1; i < linecount; i++) {
    375385            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;
    378388            }
    379         return 0;
     389        }
     390        return EXIT_SUCCESS;
    380391    }
    381392#endif
    382393    /* Perform the actual sort */
    383     qsort(lines, linecount, sizeof(char *), compare_keys);
     394    qsort(lines, linecount, sizeof(lines[0]), compare_keys);
    384395    /* handle -u */
    385396    if (option_mask32 & FLAG_u) {
     
    389400        option_mask32 |= FLAG_s;
    390401        for (i = 1; i < linecount; i++) {
    391             if (!compare_keys(&lines[flag], &lines[i]))
     402            if (compare_keys(&lines[flag], &lines[i]) == 0)
    392403                free(lines[i]);
    393404            else
    394405                lines[++flag] = lines[i];
    395406        }
    396         if (linecount) linecount = flag+1;
    397     }
     407        if (linecount)
     408            linecount = flag+1;
     409    }
     410
    398411    /* 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';
    399418    for (i = 0; i < linecount; i++)
    400         fprintf(outfile, "%s\n", lines[i]);
     419        printf("%s%c", lines[i], flag);
    401420
    402421    fflush_stdout_and_exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.