aboutsummaryrefslogtreecommitdiff
path: root/coreutils/uniq.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /coreutils/uniq.c
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'coreutils/uniq.c')
-rw-r--r--coreutils/uniq.c225
1 files changed, 111 insertions, 114 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index 965d290c2..1e41eaacd 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini uniq implementation for busybox 3 * Mini uniq implementation for busybox
3 * 4 *
@@ -27,119 +28,116 @@
27#include <errno.h> 28#include <errno.h>
28 29
29static const char uniq_usage[] = 30static const char uniq_usage[] =
30"uniq [OPTION]... [INPUT [OUTPUT]]\n" 31 "uniq [OPTION]... [INPUT [OUTPUT]]\n"
31"Discard all but one of successive identical lines from INPUT (or\n" 32 "Discard all but one of successive identical lines from INPUT (or\n"
32"standard input), writing to OUTPUT (or standard output).\n" 33 "standard input), writing to OUTPUT (or standard output).\n"
33"\n" 34 "\n"
34"\t-h\tdisplay this help and exit\n" 35 "\t-h\tdisplay this help and exit\n"
35"\n" 36
36"A field is a run of whitespace, then non-whitespace characters.\n" 37 "\n"
37"Fields are skipped before chars.\n" 38 "A field is a run of whitespace, then non-whitespace characters.\n"
38; 39 "Fields are skipped before chars.\n";
39 40
40/* max chars in line */ 41/* max chars in line */
41#define UNIQ_MAX 4096 42#define UNIQ_MAX 4096
42 43
43typedef void (Print)(FILE *, const char *); 44typedef void (Print) (FILE *, const char *);
44 45
45typedef int (Decide)(const char *, const char *); 46typedef int (Decide) (const char *, const char *);
46 47
47/* container for two lines to be compared */ 48/* container for two lines to be compared */
48typedef struct { 49typedef struct {
49 char *a; 50 char *a;
50 char *b; 51 char *b;
51 int recurrence; 52 int recurrence;
52 FILE *in; 53 FILE *in;
53 FILE *out; 54 FILE *out;
54 void *func; 55 void *func;
55} Subject; 56} Subject;
56 57
57/* set up all the variables of a uniq operation */ 58/* set up all the variables of a uniq operation */
58static Subject * 59static Subject *subject_init(Subject * self, FILE * in, FILE * out,
59subject_init(Subject *self, FILE *in, FILE *out, void *func) 60 void *func)
60{ 61{
61 self->a = NULL; 62 self->a = NULL;
62 self->b = NULL; 63 self->b = NULL;
63 self->in = in; 64 self->in = in;
64 self->out = out; 65 self->out = out;
65 self->func = func; 66 self->func = func;
66 self->recurrence = 0; 67 self->recurrence = 0;
67 return self; 68 return self;
68} 69}
69 70
70/* point a and b to the appropriate lines; 71/* point a and b to the appropriate lines;
71 * count the recurrences (if any) of a string; 72 * count the recurrences (if any) of a string;
72 */ 73 */
73static Subject * 74static Subject *subject_next(Subject * self)
74subject_next(Subject *self)
75{ 75{
76 /* tmp line holders */ 76 /* tmp line holders */
77 static char line[2][UNIQ_MAX]; 77 static char line[2][UNIQ_MAX];
78 static int alternator = 0; 78 static int alternator = 0;
79 79
80 if (fgets(line[alternator], UNIQ_MAX, self->in)) { 80 if (fgets(line[alternator], UNIQ_MAX, self->in)) {
81 self->a = self->b; 81 self->a = self->b;
82 self->b = line[alternator]; 82 self->b = line[alternator];
83 alternator ^= 1; 83 alternator ^= 1;
84 return self; 84 return self;
85 } 85 }
86 86
87 return NULL; 87 return NULL;
88} 88}
89 89
90static Subject * 90static Subject *subject_last(Subject * self)
91subject_last(Subject *self)
92{ 91{
93 self->a = self->b; 92 self->a = self->b;
94 self->b = NULL; 93 self->b = NULL;
95 return self; 94 return self;
96} 95}
97 96
98static Subject * 97static Subject *subject_study(Subject * self)
99subject_study(Subject *self)
100{ 98{
101 if (self->a == NULL) { 99 if (self->a == NULL) {
102 return self; 100 return self;
103 } 101 }
104 if (self->b == NULL) { 102 if (self->b == NULL) {
105 fprintf(self->out, "%s", self->a); 103 fprintf(self->out, "%s", self->a);
104 return self;
105 }
106 if (strcmp(self->a, self->b) == 0) {
107 self->recurrence++;
108 } else {
109 fprintf(self->out, "%s", self->a);
110 self->recurrence = 0;
111 }
106 return self; 112 return self;
107 }
108 if (strcmp(self->a, self->b) == 0) {
109 self->recurrence++;
110 } else {
111 fprintf(self->out, "%s", self->a);
112 self->recurrence = 0;
113 }
114 return self;
115} 113}
116 114
117static int 115static int
118set_file_pointers(int schema, FILE **in, FILE **out, char **argv) 116set_file_pointers(int schema, FILE ** in, FILE ** out, char **argv)
119{ 117{
120 switch (schema) { 118 switch (schema) {
121 case 0: 119 case 0:
122 *in = stdin; 120 *in = stdin;
123 *out = stdout; 121 *out = stdout;
124 break; 122 break;
125 case 1: 123 case 1:
126 *in = fopen(argv[0], "r"); 124 *in = fopen(argv[0], "r");
127 *out = stdout; 125 *out = stdout;
128 break; 126 break;
129 case 2: 127 case 2:
130 *in = fopen(argv[0], "r"); 128 *in = fopen(argv[0], "r");
131 *out = fopen(argv[1], "w"); 129 *out = fopen(argv[1], "w");
132 break; 130 break;
133 } 131 }
134 if (*in == NULL) { 132 if (*in == NULL) {
135 fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno)); 133 fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno));
136 return errno; 134 return errno;
137 } 135 }
138 if (*out == NULL) { 136 if (*out == NULL) {
139 fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno)); 137 fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno));
140 return errno; 138 return errno;
141 } 139 }
142 return 0; 140 return 0;
143} 141}
144 142
145 143
@@ -152,45 +150,44 @@ set_file_pointers(int schema, FILE **in, FILE **out, char **argv)
152/* it seems like GNU/uniq only takes one or two files as an option */ 150/* it seems like GNU/uniq only takes one or two files as an option */
153 151
154/* ________________________________________________________________________ */ 152/* ________________________________________________________________________ */
155int 153int uniq_main(int argc, char **argv)
156uniq_main(int argc, char **argv)
157{ 154{
158 int i; 155 int i;
159 char opt; 156 char opt;
160 FILE *in, *out; 157 FILE *in, *out;
161 Subject s; 158 Subject s;
162 159
163 /* parse argv[] */ 160 /* parse argv[] */
164 for (i = 1; i < argc; i++) { 161 for (i = 1; i < argc; i++) {
165 if (argv[i][0] == '-') { 162 if (argv[i][0] == '-') {
166 opt = argv[i][1]; 163 opt = argv[i][1];
167 switch (opt) { 164 switch (opt) {
168 case '-': 165 case '-':
169 case 'h': 166 case 'h':
170 usage(uniq_usage); 167 usage(uniq_usage);
171 default: 168 default:
172 usage(uniq_usage); 169 usage(uniq_usage);
173 } 170 }
174 } else { 171 } else {
175 break; 172 break;
173 }
176 } 174 }
177 }
178 175
179 /* 0 src: stdin; dst: stdout */ 176 /* 0 src: stdin; dst: stdout */
180 /* 1 src: file; dst: stdout */ 177 /* 1 src: file; dst: stdout */
181 /* 2 src: file; dst: file */ 178 /* 2 src: file; dst: file */
182 if (set_file_pointers((argc - 1), &in, &out, &argv[i])) { 179 if (set_file_pointers((argc - 1), &in, &out, &argv[i])) {
183 exit(1); 180 exit(1);
184 } 181 }
185 182
186 subject_init(&s, in, out, NULL); 183 subject_init(&s, in, out, NULL);
187 while (subject_next(&s)) { 184 while (subject_next(&s)) {
185 subject_study(&s);
186 }
187 subject_last(&s);
188 subject_study(&s); 188 subject_study(&s);
189 }
190 subject_last(&s);
191 subject_study(&s);
192 189
193 exit(0); 190 exit(0);
194} 191}
195 192
196/* $Id: uniq.c,v 1.6 2000/02/07 05:29:42 erik Exp $ */ 193/* $Id: uniq.c,v 1.7 2000/02/08 19:58:47 erik Exp $ */