Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/coreutils/sort.c


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

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

    r3232 r3621  
    1515//usage:#define sort_trivial_usage
    1616//usage:       "[-nru"
    17 //usage:    IF_FEATURE_SORT_BIG("gMcszbdfimSTokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
     17//usage:    IF_FEATURE_SORT_BIG("gMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
    1818//usage:       "] [FILE]..."
    1919//usage:#define sort_full_usage "\n\n"
    2020//usage:       "Sort lines of text\n"
    2121//usage:    IF_FEATURE_SORT_BIG(
     22//usage:     "\n    -o FILE Output to FILE"
     23//usage:     "\n    -c  Check whether input is sorted"
    2224//usage:     "\n    -b  Ignore leading blanks"
    23 //usage:     "\n    -c  Check whether input is sorted"
     25//usage:     "\n    -f  Ignore case"
     26//usage:     "\n    -i  Ignore unprintable characters"
    2427//usage:     "\n    -d  Dictionary order (blank or alphanumeric only)"
    25 //usage:     "\n    -f  Ignore case"
    2628//usage:     "\n    -g  General numerical sort"
    27 //usage:     "\n    -i  Ignore unprintable characters"
    28 //usage:     "\n    -k  Sort key"
    2929//usage:     "\n    -M  Sort month"
    3030//usage:    )
     31//-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G)
    3132//usage:     "\n    -n  Sort numbers"
    3233//usage:    IF_FEATURE_SORT_BIG(
    33 //usage:     "\n    -o  Output to file"
    34 //usage:     "\n    -k  Sort by key"
    35 //usage:     "\n    -t CHAR Key separator"
     34//usage:     "\n    -t CHAR Field separator"
     35//usage:     "\n    -k N[,M] Sort by Nth field"
    3636//usage:    )
    3737//usage:     "\n    -r  Reverse sort order"
     
    4242//usage:    IF_FEATURE_SORT_BIG(
    4343//usage:     "\n    -z  Lines are terminated by NUL, not newline"
    44 //usage:     "\n    -mST    Ignored for GNU compatibility")
     44////usage:     "\n  -m  Ignored for GNU compatibility"
     45////usage:     "\n  -S BUFSZ Ignored for GNU compatibility"
     46////usage:     "\n  -T TMPDIR Ignored for GNU compatibility"
     47//usage:    )
    4548//usage:
    4649//usage:#define sort_example_usage
     
    107110static char *get_key(char *str, struct sort_key *key, int flags)
    108111{
    109     int start = 0, end = 0, len, j;
     112    int start = start; /* for compiler */
     113    int end;
     114    int len, j;
    110115    unsigned i;
    111116
     
    124129        /* Loop through fields */
    125130        else {
     131            unsigned char ch = 0;
     132
    126133            end = 0;
    127134            for (i = 1; i < key->range[2*j] + j; i++) {
    128135                if (key_separator) {
    129136                    /* Skip body of key and separator */
    130                     while (str[end]) {
    131                         if (str[end++] == key_separator)
     137                    while ((ch = str[end]) != '\0') {
     138                            end++;
     139                        if (ch == key_separator)
    132140                            break;
    133141                    }
     
    137145                        end++;
    138146                    /* Skip body of key */
    139                     while (str[end]) {
     147                    while (str[end] != '\0') {
    140148                        if (isspace(str[end]))
    141149                            break;
     
    144152                }
    145153            }
     154            /* Remove last delim: "abc:def:" => "abc:def" */
     155            if (j && ch) {
     156                //if (str[end-1] != key_separator)
     157                //  bb_error_msg(_and_die("BUG! "
     158                //  "str[start:%d,end:%d]:'%.*s'",
     159                //  start, end, (int)(end-start), &str[start]);
     160                end--;
     161            }
    146162        }
    147163        if (!j) start = end;
    148164    }
    149165    /* Strip leading whitespace if necessary */
    150 //XXX: skip_whitespace()
    151166    if (flags & FLAG_b)
     167        /* not using skip_whitespace() for speed */
    152168        while (isspace(str[start])) start++;
    153169    /* Strip trailing whitespace if necessary */
    154170    if (flags & FLAG_bb)
    155171        while (end > start && isspace(str[end-1])) end--;
    156     /* Handle offsets on start and end */
     172    /* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */
    157173    if (key->range[3]) {
    158         end += key->range[3] - 1;
     174        end = key->range[3];
    159175        if (end > len) end = len;
    160176    }
     177    /* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */
    161178    if (key->range[1]) {
    162179        start += key->range[1] - 1;
     
    164181    }
    165182    /* Make the copy */
    166     if (end < start) end = start;
     183    if (end < start)
     184        end = start;
    167185    str = xstrndup(str+start, end-start);
    168186    /* Handle -d */
     
    226244#endif
    227245        /* Perform actual comparison */
    228         switch (flags & 7) {
     246        switch (flags & (FLAG_n | FLAG_M | FLAG_g)) {
    229247        default:
    230248            bb_error_msg_and_die("unknown sort type");
     
    278296                retval = 1;
    279297            else
    280                 retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
     298                retval = dx - thyme.tm_mon;
    281299            break;
    282300        }
     
    303321
    304322    /* Perform fallback sort if necessary */
    305     if (!retval && !(option_mask32 & FLAG_s))
     323    if (!retval && !(option_mask32 & FLAG_s)) {
     324        flags = option_mask32;
    306325        retval = strcmp(*(char **)xarg, *(char **)yarg);
    307 
    308     if (flags & FLAG_r) return -retval;
     326    }
     327
     328    if (flags & FLAG_r)
     329        return -retval;
     330
    309331    return retval;
    310332}
     
    329351    char *str_ignored, *str_o, *str_t;
    330352    llist_t *lst_k = NULL;
    331     int i, flag;
     353    int i;
    332354    int linecount;
    333355    unsigned opts;
     
    352374     * since that reduces register pressure and makes code smaller */
    353375
    354     /* parse sort key */
     376    /* Parse sort key */
    355377    while (lst_k) {
    356378        enum {
     
    379401            }
    380402            while (*str_k) {
    381                 const char *temp2;
     403                int flag;
     404                const char *idx;
    382405
    383406                if (*str_k == ',' && !i++) {
     
    386409                } /* no else needed: fall through to syntax error
    387410                    because comma isn't in OPT_STR */
    388                 temp2 = strchr(OPT_STR, *str_k);
    389                 if (!temp2)
     411                idx = strchr(OPT_STR, *str_k);
     412                if (!idx)
    390413                    bb_error_msg_and_die("unknown key option");
    391                 flag = 1 << (temp2 - OPT_STR);
     414                flag = 1 << (idx - OPT_STR);
    392415                if (flag & ~FLAG_allowed_for_k)
    393416                    bb_error_msg_and_die("unknown sort type");
     
    423446
    424447#if ENABLE_FEATURE_SORT_BIG
    425     /* if no key, perform alphabetic sort */
     448    /* If no key, perform alphabetic sort */
    426449    if (!key_list)
    427450        add_key()->range[0] = 1;
    428     /* handle -c */
     451    /* Handle -c */
    429452    if (option_mask32 & FLAG_c) {
    430453        int j = (option_mask32 & FLAG_u) ? -1 : 0;
     
    440463    /* Perform the actual sort */
    441464    qsort(lines, linecount, sizeof(lines[0]), compare_keys);
    442     /* handle -u */
     465
     466    /* Handle -u */
    443467    if (option_mask32 & FLAG_u) {
    444         flag = 0;
     468        int j = 0;
    445469        /* coreutils 6.3 drop lines for which only key is the same */
    446470        /* -- disabling last-resort compare... */
    447471        option_mask32 |= FLAG_s;
    448472        for (i = 1; i < linecount; i++) {
    449             if (compare_keys(&lines[flag], &lines[i]) == 0)
     473            if (compare_keys(&lines[j], &lines[i]) == 0)
    450474                free(lines[i]);
    451475            else
    452                 lines[++flag] = lines[i];
     476                lines[++j] = lines[i];
    453477        }
    454478        if (linecount)
    455             linecount = flag+1;
     479            linecount = j+1;
    456480    }
    457481
     
    462486        xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
    463487#endif
    464     flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
    465     for (i = 0; i < linecount; i++)
    466         printf("%s%c", lines[i], flag);
     488    {
     489        int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
     490        for (i = 0; i < linecount; i++)
     491            printf("%s%c", lines[i], ch);
     492    }
    467493
    468494    fflush_stdout_and_exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.