Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/coreutils/uniq.c

    r821 r1765  
    1111/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
    1212
    13 #include "busybox.h"
    14 #include <string.h>
    15 #include <ctype.h>
    16 #include <unistd.h>
     13#include "libbb.h"
    1714
    18 static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4";
     15static const char uniq_opts[] ALIGN1 = "cdu" "f:s:" "cdu\0\1\2\4";
    1916
    2017static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
     
    2219    const char *n;
    2320
    24     if ((n = *argv) != NULL) {
     21    n = *argv;
     22    if (n != NULL) {
    2523        if ((*n != '-') || n[1]) {
    26             return bb_xfopen(n, "r\0w" + read0write2);
     24            return xfopen(n, "r\0w" + read0write2);
    2725        }
    2826    }
     
    3028}
    3129
     30int uniq_main(int argc, char **argv);
    3231int uniq_main(int argc, char **argv)
    3332{
    3433    FILE *in, *out;
    35     unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
     34    unsigned long dups, skip_fields, skip_chars, i;
    3635    const char *s0, *e0, *s1, *e1, *input_filename;
    37     int opt;
     36    unsigned opt;
    3837
    39     uniq_flags = skip_fields = skip_chars = 0;
     38    enum {
     39        OPT_c = 0x1,
     40        OPT_d = 0x2,
     41        OPT_u = 0x4,
     42        OPT_f = 0x8,
     43        OPT_s = 0x10,
     44    };
    4045
    41     while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
    42         if ((opt == 'f') || (opt == 's')) {
    43             int t = bb_xgetularg10(optarg);
    44             if (opt == 'f') {
    45                 skip_fields = t;
    46             } else {
    47                 skip_chars = t;
    48             }
    49         } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
    50             uniq_flags |= s0[4];
    51         } else {
    52             bb_show_usage();
    53         }
    54     }
     46    skip_fields = skip_chars = 0;
    5547
    56     input_filename = *(argv += optind);
     48    opt = getopt32(argv, "cduf:s:", &s0, &s1);
     49    if (opt & OPT_f)
     50        skip_fields = xatoul(s0);
     51    if (opt & OPT_s)
     52        skip_chars = xatoul(s1);
     53    argv += optind;
     54
     55    input_filename = *argv;
    5756
    5857    in = xgetoptfile_uniq_s(argv, 0);
     
    7372
    7473        /* gnu uniq ignores newlines */
    75         while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
     74        while ((s1 = xmalloc_getline(in)) != NULL) {
    7675            e1 = s1;
    77             for (i=skip_fields ; i ; i--) {
     76            for (i = skip_fields; i; i--) {
    7877                e1 = skip_whitespace(e1);
    79                 while (*e1 && !isspace(*e1)) {
    80                     ++e1;
    81                 }
     78                e1 = skip_non_whitespace(e1);
    8279            }
    83             for (i = skip_chars ; *e1 && i ; i--) {
     80            for (i = skip_chars; *e1 && i; i--) {
    8481                ++e1;
    8582            }
     
    9390
    9491        if (s0) {
    95             if (!(uniq_flags & (2 << !!dups))) {
    96                 bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
    97                 bb_fprintf(out, "%s\n", s0);
     92            if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */
     93                fprintf(out, "\0%d " + (opt & 1), dups + 1);
     94                fprintf(out, "%s\n", s0);
    9895            }
    9996            free((void *)s0);
     
    10198    } while (s1);
    10299
    103     bb_xferror(in, input_filename);
     100    die_if_ferror(in, input_filename);
    104101
    105     bb_fflush_stdout_and_exit(EXIT_SUCCESS);
     102    fflush_stdout_and_exit(EXIT_SUCCESS);
    106103}
Note: See TracChangeset for help on using the changeset viewer.