aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2005-09-08 06:02:49 +0000
committerManuel Novoa III <mjn3@codepoet.org>2005-09-08 06:02:49 +0000
commit415f6c96287d1a43c916e23b601f0629f6bccbd7 (patch)
treeea1b72741e7cf85be78af1dea3d091992caf4572
parentae32016fc938b5074e0787654df39190364d40f3 (diff)
downloadbusybox-w32-415f6c96287d1a43c916e23b601f0629f6bccbd7.tar.gz
busybox-w32-415f6c96287d1a43c916e23b601f0629f6bccbd7.tar.bz2
busybox-w32-415f6c96287d1a43c916e23b601f0629f6bccbd7.zip
Rob, I don't know why you feel some unexplainable compulsion to get rid of
a meaningless warning. But I do expect you preserve the coding style and variable names when all you're doing is tweaking some of my code. I repeat... do NOT change whitespace, variable names, or coding style in any of my code simply to conform to your coding style.
-rw-r--r--coreutils/uniq.c118
1 files changed, 70 insertions, 48 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index aa26e0575..d5c6f719d 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -4,7 +4,19 @@
4 * 4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * Licensed under GPL v2, see file LICENSE in this tarball for details. 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
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8 * 20 *
9 */ 21 */
10 22
@@ -19,83 +31,93 @@
19#include "busybox.h" 31#include "busybox.h"
20#include "libcoreutils/coreutils.h" 32#include "libcoreutils/coreutils.h"
21 33
22/* The extra data is flags to make -d and -u switch each other off */ 34static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4";
23static const char uniq_opts[] = "cudf:s:\0\7\3\5\1\2\4";
24 35
25#define SHOW_COUNT 1 36static FILE *xgetoptfile_sort_uniq_s(char **argv, const char *mode)
26#define SHOW_UNIQUE 2
27#define SHOW_DUPLICATE 4
28
29static FILE *open_arg(char **argv, char *mode)
30{ 37{
31 char *n=*argv; 38 const char *n;
32 39
33 return (n && *n != '-' && n[1]) ? bb_xfopen(n, mode) : 40 if ((n = *argv) != NULL) {
34 *mode=='r' ? stdin : stdout; 41 if ((*n != '-') || n[1]) {
42 return bb_xfopen(n, mode);
43 }
44 }
45 return (*mode == 'r') ? stdin : stdout;
35} 46}
36 47
37
38int uniq_main(int argc, char **argv) 48int uniq_main(int argc, char **argv)
39{ 49{
40 FILE *in, *out; 50 FILE *in, *out;
51 /* Note: Ignore the warning about dups and e0 being used uninitialized.
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
41 unsigned long dups, skip_fields, skip_chars, i; 54 unsigned long dups, skip_fields, skip_chars, i;
42 const char *oldline, *oldskipped, *line, *skipped, *input_filename; 55 const char *s0, *e0, *s1, *e1, *input_filename;
43 int opt; 56 int opt;
44 int uniq_flags = SHOW_UNIQUE | SHOW_DUPLICATE; 57 int uniq_flags = 6; /* -u */
45 58
46 skip_fields = skip_chars = 0; 59 skip_fields = skip_chars = 0;
47 60
48 while ((opt = getopt(argc, argv, uniq_opts)) > 0) { 61 while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
49 if (opt == 'f') skip_fields = bb_xgetularg10(optarg); 62 if (opt == 'f') {
50 else if (opt == 's') skip_chars = bb_xgetularg10(optarg); 63 skip_fields = bb_xgetularg10(optarg);
51 64 } else if (opt == 's') {
52 /* This bit uses the extra data at the end of uniq_opts to make 65 skip_chars = bb_xgetularg10(optarg);
53 * -d and -u switch each other off in a very small amount of space */ 66 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
54 67 uniq_flags &= s0[4];
55 else if ((line = strchr(uniq_opts, opt)) != NULL) { 68 uniq_flags |= s0[7];
56 uniq_flags &= line[8]; 69 } else {
57 uniq_flags |= line[11]; 70 bb_show_usage();
58 } else bb_show_usage(); 71 }
59 } 72 }
60 73
61 input_filename = *(argv += optind); 74 input_filename = *(argv += optind);
62 75
63 in = open_arg(argv, "r"); 76 in = xgetoptfile_sort_uniq_s(argv, "r");
64 if (*argv) ++argv; 77 if (*argv) {
65 out = open_arg(argv, "w"); 78 ++argv;
66 if (*argv && argv[1]) bb_show_usage(); 79 }
80 out = xgetoptfile_sort_uniq_s(argv, "w");
81 if (*argv && argv[1]) {
82 bb_show_usage();
83 }
67 84
68 line = skipped = NULL; 85 s0 = NULL;
69 86
70NOT_DUPLICATE:
71 oldline = line;
72 oldskipped = skipped;
73 dups = 0;
74
75 /* gnu uniq ignores newlines */ 87 /* gnu uniq ignores newlines */
76 while ((line = bb_get_chomped_line_from_file(in)) != NULL) { 88 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
77 skipped = line; 89 e1 = s1;
78 for (i=skip_fields ; i ; i--) { 90 for (i=skip_fields ; i ; i--) {
79 skipped = bb_skip_whitespace(skipped); 91 e1 = bb_skip_whitespace(e1);
80 while (*skipped && !isspace(*skipped)) ++skipped; 92 while (*e1 && !isspace(*e1)) {
93 ++e1;
94 }
81 } 95 }
82 for (i = skip_chars ; *skipped && i ; i--) ++skipped; 96 for (i = skip_chars ; *e1 && i ; i--) {
83 if (oldline) { 97 ++e1;
84 if (!strcmp(oldskipped, skipped)) { 98 }
99 if (s0) {
100 if (strcmp(e0, e1) == 0) {
85 ++dups; /* Note: Testing for overflow seems excessive. */ 101 ++dups; /* Note: Testing for overflow seems excessive. */
86 continue; 102 continue;
87 } 103 }
88DO_LAST: 104 DO_LAST:
89 if (uniq_flags & (dups ? SHOW_DUPLICATE : SHOW_UNIQUE)) { 105 if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) {
90 bb_fprintf(out, "\0%7d " + (uniq_flags & SHOW_COUNT), dups + 1); 106 bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1);
91 bb_fprintf(out, "%s\n", oldline); 107 bb_fprintf(out, "%s\n", s0);
92 } 108 }
93 free((void *)oldline); 109 free((void *)s0);
94 } 110 }
95 goto NOT_DUPLICATE; 111
112 s0 = s1;
113 e0 = e1;
114 dups = 0;
96 } 115 }
97 116
98 if (oldline) goto DO_LAST; 117 if (s0) {
118 e1 = NULL;
119 goto DO_LAST;
120 }
99 121
100 bb_xferror(in, input_filename); 122 bb_xferror(in, input_filename);
101 123