diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2005-09-15 08:06:42 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2005-09-15 08:06:42 +0000 |
commit | 84b93f7ef18105db1270c12b16885106df31f2cb (patch) | |
tree | a1901da5a1861f4cdf9986ac3f9ad146a097a5b5 | |
parent | 86097b3f3180dea02423103a7ef5a63818af3837 (diff) | |
download | busybox-w32-84b93f7ef18105db1270c12b16885106df31f2cb.tar.gz busybox-w32-84b93f7ef18105db1270c12b16885106df31f2cb.tar.bz2 busybox-w32-84b93f7ef18105db1270c12b16885106df31f2cb.zip |
Get rid of warnings, remove compat with older gnu uniq to truly conform to susv3, and cut size a bit.
-rw-r--r-- | coreutils/uniq.c | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c index d5c6f719d..93ae169f4 100644 --- a/coreutils/uniq.c +++ b/coreutils/uniq.c | |||
@@ -2,7 +2,7 @@ | |||
2 | /* | 2 | /* |
3 | * uniq implementation for busybox | 3 | * uniq implementation for busybox |
4 | * | 4 | * |
5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> | 5 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org> |
6 | * | 6 | * |
7 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | 8 | * it under the terms of the GNU General Public License as published by |
@@ -31,41 +31,39 @@ | |||
31 | #include "busybox.h" | 31 | #include "busybox.h" |
32 | #include "libcoreutils/coreutils.h" | 32 | #include "libcoreutils/coreutils.h" |
33 | 33 | ||
34 | static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4"; | 34 | static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4"; |
35 | 35 | ||
36 | static FILE *xgetoptfile_sort_uniq_s(char **argv, const char *mode) | 36 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) |
37 | { | 37 | { |
38 | const char *n; | 38 | const char *n; |
39 | 39 | ||
40 | if ((n = *argv) != NULL) { | 40 | if ((n = *argv) != NULL) { |
41 | if ((*n != '-') || n[1]) { | 41 | if ((*n != '-') || n[1]) { |
42 | return bb_xfopen(n, mode); | 42 | return bb_xfopen(n, "r\0w" + read0write2); |
43 | } | 43 | } |
44 | } | 44 | } |
45 | return (*mode == 'r') ? stdin : stdout; | 45 | return (read0write2) ? stdout : stdin; |
46 | } | 46 | } |
47 | 47 | ||
48 | int uniq_main(int argc, char **argv) | 48 | int uniq_main(int argc, char **argv) |
49 | { | 49 | { |
50 | FILE *in, *out; | 50 | FILE *in, *out; |
51 | /* Note: Ignore the warning about dups and e0 being used uninitialized. | 51 | unsigned long dups, skip_fields, skip_chars, i, uniq_flags; |
52 | * They will be initialized on the fist pass of the loop (since s0 is NULL). */ | ||
53 | #warning The dups and e0 warnings are OK, ignore them | ||
54 | unsigned long dups, skip_fields, skip_chars, i; | ||
55 | const char *s0, *e0, *s1, *e1, *input_filename; | 52 | const char *s0, *e0, *s1, *e1, *input_filename; |
56 | int opt; | 53 | int opt; |
57 | int uniq_flags = 6; /* -u */ | ||
58 | 54 | ||
59 | skip_fields = skip_chars = 0; | 55 | uniq_flags = skip_fields = skip_chars = 0; |
60 | 56 | ||
61 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { | 57 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { |
62 | if (opt == 'f') { | 58 | if ((opt == 'f') || (opt == 's')) { |
63 | skip_fields = bb_xgetularg10(optarg); | 59 | int t = bb_xgetularg10(optarg); |
64 | } else if (opt == 's') { | 60 | if (opt == 'f') { |
65 | skip_chars = bb_xgetularg10(optarg); | 61 | skip_fields = t; |
62 | } else { | ||
63 | skip_chars = t; | ||
64 | } | ||
66 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { | 65 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { |
67 | uniq_flags &= s0[4]; | 66 | uniq_flags |= s0[4]; |
68 | uniq_flags |= s0[7]; | ||
69 | } else { | 67 | } else { |
70 | bb_show_usage(); | 68 | bb_show_usage(); |
71 | } | 69 | } |
@@ -73,51 +71,50 @@ int uniq_main(int argc, char **argv) | |||
73 | 71 | ||
74 | input_filename = *(argv += optind); | 72 | input_filename = *(argv += optind); |
75 | 73 | ||
76 | in = xgetoptfile_sort_uniq_s(argv, "r"); | 74 | in = xgetoptfile_uniq_s(argv, 0); |
77 | if (*argv) { | 75 | if (*argv) { |
78 | ++argv; | 76 | ++argv; |
79 | } | 77 | } |
80 | out = xgetoptfile_sort_uniq_s(argv, "w"); | 78 | out = xgetoptfile_uniq_s(argv, 2); |
81 | if (*argv && argv[1]) { | 79 | if (*argv && argv[1]) { |
82 | bb_show_usage(); | 80 | bb_show_usage(); |
83 | } | 81 | } |
84 | 82 | ||
85 | s0 = NULL; | 83 | s1 = e1 = NULL; /* prime the pump */ |
84 | |||
85 | do { | ||
86 | s0 = s1; | ||
87 | e0 = e1; | ||
88 | dups = 0; | ||
86 | 89 | ||
87 | /* gnu uniq ignores newlines */ | 90 | /* gnu uniq ignores newlines */ |
88 | while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { | 91 | while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { |
89 | e1 = s1; | 92 | e1 = s1; |
90 | for (i=skip_fields ; i ; i--) { | 93 | for (i=skip_fields ; i ; i--) { |
91 | e1 = bb_skip_whitespace(e1); | 94 | e1 = bb_skip_whitespace(e1); |
92 | while (*e1 && !isspace(*e1)) { | 95 | while (*e1 && !isspace(*e1)) { |
96 | ++e1; | ||
97 | } | ||
98 | } | ||
99 | for (i = skip_chars ; *e1 && i ; i--) { | ||
93 | ++e1; | 100 | ++e1; |
94 | } | 101 | } |
102 | |||
103 | if (!s0 || strcmp(e0, e1)) { | ||
104 | break; | ||
105 | } | ||
106 | |||
107 | ++dups; /* Note: Testing for overflow seems excessive. */ | ||
95 | } | 108 | } |
96 | for (i = skip_chars ; *e1 && i ; i--) { | 109 | |
97 | ++e1; | ||
98 | } | ||
99 | if (s0) { | 110 | if (s0) { |
100 | if (strcmp(e0, e1) == 0) { | 111 | if (!(uniq_flags & (2 << !!dups))) { |
101 | ++dups; /* Note: Testing for overflow seems excessive. */ | 112 | bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1); |
102 | continue; | ||
103 | } | ||
104 | DO_LAST: | ||
105 | if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) { | ||
106 | bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1); | ||
107 | bb_fprintf(out, "%s\n", s0); | 113 | bb_fprintf(out, "%s\n", s0); |
108 | } | 114 | } |
109 | free((void *)s0); | 115 | free((void *)s0); |
110 | } | 116 | } |
111 | 117 | } while (s1); | |
112 | s0 = s1; | ||
113 | e0 = e1; | ||
114 | dups = 0; | ||
115 | } | ||
116 | |||
117 | if (s0) { | ||
118 | e1 = NULL; | ||
119 | goto DO_LAST; | ||
120 | } | ||
121 | 118 | ||
122 | bb_xferror(in, input_filename); | 119 | bb_xferror(in, input_filename); |
123 | 120 | ||