diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-31 10:19:11 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-31 10:19:11 +0000 |
commit | 4caa09a78291addce3868d3d468cbd6c669a1879 (patch) | |
tree | 312ddfb8d502acccf4c0bc6a28d81e85114530e3 /coreutils/uniq.c | |
parent | 666c40c9fce062f7466f424176ef2d0b8f581b38 (diff) | |
download | busybox-w32-4caa09a78291addce3868d3d468cbd6c669a1879.tar.gz busybox-w32-4caa09a78291addce3868d3d468cbd6c669a1879.tar.bz2 busybox-w32-4caa09a78291addce3868d3d468cbd6c669a1879.zip |
uniq: getopt32-ization. -38 bytes.
Diffstat (limited to 'coreutils/uniq.c')
-rw-r--r-- | coreutils/uniq.c | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c index 0cc0c83b3..bcdf44026 100644 --- a/coreutils/uniq.c +++ b/coreutils/uniq.c | |||
@@ -12,13 +12,14 @@ | |||
12 | 12 | ||
13 | #include "busybox.h" | 13 | #include "busybox.h" |
14 | 14 | ||
15 | static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4"; | 15 | static const char uniq_opts[] = "cdu" "f:s:" "cdu\0\1\2\4"; |
16 | 16 | ||
17 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) | 17 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) |
18 | { | 18 | { |
19 | const char *n; | 19 | const char *n; |
20 | 20 | ||
21 | if ((n = *argv) != NULL) { | 21 | n = *argv; |
22 | if (n != NULL) { | ||
22 | if ((*n != '-') || n[1]) { | 23 | if ((*n != '-') || n[1]) { |
23 | return xfopen(n, "r\0w" + read0write2); | 24 | return xfopen(n, "r\0w" + read0write2); |
24 | } | 25 | } |
@@ -30,28 +31,28 @@ int uniq_main(int argc, char **argv); | |||
30 | int uniq_main(int argc, char **argv) | 31 | int uniq_main(int argc, char **argv) |
31 | { | 32 | { |
32 | FILE *in, *out; | 33 | FILE *in, *out; |
33 | unsigned long dups, skip_fields, skip_chars, i, uniq_flags; | 34 | unsigned long dups, skip_fields, skip_chars, i; |
34 | const char *s0, *e0, *s1, *e1, *input_filename; | 35 | const char *s0, *e0, *s1, *e1, *input_filename; |
35 | int opt; | 36 | unsigned opt; |
36 | 37 | ||
37 | 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 | }; | ||
38 | 45 | ||
39 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { | 46 | skip_fields = skip_chars = 0; |
40 | if ((opt == 'f') || (opt == 's')) { | 47 | |
41 | unsigned long t = xatoul(optarg); | 48 | opt = getopt32(argc, argv, "cduf:s:", &s0, &s1); |
42 | if (opt == 'f') { | 49 | if (opt & OPT_f) |
43 | skip_fields = t; | 50 | skip_fields = xatoul(s0); |
44 | } else { | 51 | if (opt & OPT_s) |
45 | skip_chars = t; | 52 | skip_chars = xatoul(s1); |
46 | } | 53 | argv += optind; |
47 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { | ||
48 | uniq_flags |= s0[4]; | ||
49 | } else { | ||
50 | bb_show_usage(); | ||
51 | } | ||
52 | } | ||
53 | 54 | ||
54 | input_filename = *(argv += optind); | 55 | input_filename = *argv; |
55 | 56 | ||
56 | in = xgetoptfile_uniq_s(argv, 0); | 57 | in = xgetoptfile_uniq_s(argv, 0); |
57 | if (*argv) { | 58 | if (*argv) { |
@@ -90,8 +91,8 @@ int uniq_main(int argc, char **argv) | |||
90 | } | 91 | } |
91 | 92 | ||
92 | if (s0) { | 93 | if (s0) { |
93 | if (!(uniq_flags & (2 << !!dups))) { | 94 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */ |
94 | fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1); | 95 | fprintf(out, "\0%d " + (opt & 1), dups + 1); |
95 | fprintf(out, "%s\n", s0); | 96 | fprintf(out, "%s\n", s0); |
96 | } | 97 | } |
97 | free((void *)s0); | 98 | free((void *)s0); |