diff options
-rw-r--r-- | coreutils/uniq.c | 61 | ||||
-rw-r--r-- | uniq.c | 61 |
2 files changed, 92 insertions, 30 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c index 251cf2dec..5f0e192a2 100644 --- a/coreutils/uniq.c +++ b/coreutils/uniq.c | |||
@@ -23,6 +23,8 @@ | |||
23 | 23 | ||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <string.h> | ||
27 | #include <errno.h> | ||
26 | 28 | ||
27 | static const char uniq_usage[] = | 29 | static const char uniq_usage[] = |
28 | "haha\n" | 30 | "haha\n" |
@@ -105,6 +107,35 @@ subject_study(Subject *self) | |||
105 | return self; | 107 | return self; |
106 | } | 108 | } |
107 | 109 | ||
110 | static int | ||
111 | set_file_pointers(int schema, FILE **in, FILE **out, char **argv) | ||
112 | { | ||
113 | switch (schema) { | ||
114 | case 0: | ||
115 | *in = stdin; | ||
116 | *out = stdout; | ||
117 | break; | ||
118 | case 1: | ||
119 | *in = fopen(argv[0], "r"); | ||
120 | *out = stdout; | ||
121 | break; | ||
122 | case 2: | ||
123 | *in = fopen(argv[0], "r"); | ||
124 | *out = fopen(argv[1], "w"); | ||
125 | break; | ||
126 | } | ||
127 | if (*in == NULL) { | ||
128 | fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno)); | ||
129 | return errno; | ||
130 | } | ||
131 | if (*out == NULL) { | ||
132 | fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno)); | ||
133 | return errno; | ||
134 | } | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | |||
108 | /* one variable is the decision algo */ | 139 | /* one variable is the decision algo */ |
109 | /* another variable is the printing algo */ | 140 | /* another variable is the printing algo */ |
110 | 141 | ||
@@ -122,20 +153,6 @@ uniq_main(int argc, char **argv) | |||
122 | FILE *in, *out; | 153 | FILE *in, *out; |
123 | Subject s; | 154 | Subject s; |
124 | 155 | ||
125 | /* init */ | ||
126 | in = stdin; | ||
127 | out = stdout; | ||
128 | |||
129 | subject_init(&s, in, out, NULL); | ||
130 | while (subject_next(&s)) { | ||
131 | subject_study(&s); | ||
132 | } | ||
133 | subject_last(&s); | ||
134 | subject_study(&s); | ||
135 | exit(0); | ||
136 | |||
137 | /* XXX : finish the tedious stuff */ | ||
138 | |||
139 | /* parse argv[] */ | 156 | /* parse argv[] */ |
140 | for (i = 1; i < argc; i++) { | 157 | for (i = 1; i < argc; i++) { |
141 | if (argv[i][0] == '-') { | 158 | if (argv[i][0] == '-') { |
@@ -152,7 +169,21 @@ uniq_main(int argc, char **argv) | |||
152 | } | 169 | } |
153 | } | 170 | } |
154 | 171 | ||
172 | /* 0 src: stdin; dst: stdout */ | ||
173 | /* 1 src: file; dst: stdout */ | ||
174 | /* 2 src: file; dst: file */ | ||
175 | if (set_file_pointers((argc - 1), &in, &out, &argv[i])) { | ||
176 | exit(1); | ||
177 | } | ||
178 | |||
179 | subject_init(&s, in, out, NULL); | ||
180 | while (subject_next(&s)) { | ||
181 | subject_study(&s); | ||
182 | } | ||
183 | subject_last(&s); | ||
184 | subject_study(&s); | ||
185 | |||
155 | exit(0); | 186 | exit(0); |
156 | } | 187 | } |
157 | 188 | ||
158 | /* $Id: uniq.c,v 1.2 2000/01/06 01:14:56 erik Exp $ */ | 189 | /* $Id: uniq.c,v 1.3 2000/01/06 23:49:21 beppu Exp $ */ |
@@ -23,6 +23,8 @@ | |||
23 | 23 | ||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <string.h> | ||
27 | #include <errno.h> | ||
26 | 28 | ||
27 | static const char uniq_usage[] = | 29 | static const char uniq_usage[] = |
28 | "haha\n" | 30 | "haha\n" |
@@ -105,6 +107,35 @@ subject_study(Subject *self) | |||
105 | return self; | 107 | return self; |
106 | } | 108 | } |
107 | 109 | ||
110 | static int | ||
111 | set_file_pointers(int schema, FILE **in, FILE **out, char **argv) | ||
112 | { | ||
113 | switch (schema) { | ||
114 | case 0: | ||
115 | *in = stdin; | ||
116 | *out = stdout; | ||
117 | break; | ||
118 | case 1: | ||
119 | *in = fopen(argv[0], "r"); | ||
120 | *out = stdout; | ||
121 | break; | ||
122 | case 2: | ||
123 | *in = fopen(argv[0], "r"); | ||
124 | *out = fopen(argv[1], "w"); | ||
125 | break; | ||
126 | } | ||
127 | if (*in == NULL) { | ||
128 | fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno)); | ||
129 | return errno; | ||
130 | } | ||
131 | if (*out == NULL) { | ||
132 | fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno)); | ||
133 | return errno; | ||
134 | } | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | |||
108 | /* one variable is the decision algo */ | 139 | /* one variable is the decision algo */ |
109 | /* another variable is the printing algo */ | 140 | /* another variable is the printing algo */ |
110 | 141 | ||
@@ -122,20 +153,6 @@ uniq_main(int argc, char **argv) | |||
122 | FILE *in, *out; | 153 | FILE *in, *out; |
123 | Subject s; | 154 | Subject s; |
124 | 155 | ||
125 | /* init */ | ||
126 | in = stdin; | ||
127 | out = stdout; | ||
128 | |||
129 | subject_init(&s, in, out, NULL); | ||
130 | while (subject_next(&s)) { | ||
131 | subject_study(&s); | ||
132 | } | ||
133 | subject_last(&s); | ||
134 | subject_study(&s); | ||
135 | exit(0); | ||
136 | |||
137 | /* XXX : finish the tedious stuff */ | ||
138 | |||
139 | /* parse argv[] */ | 156 | /* parse argv[] */ |
140 | for (i = 1; i < argc; i++) { | 157 | for (i = 1; i < argc; i++) { |
141 | if (argv[i][0] == '-') { | 158 | if (argv[i][0] == '-') { |
@@ -152,7 +169,21 @@ uniq_main(int argc, char **argv) | |||
152 | } | 169 | } |
153 | } | 170 | } |
154 | 171 | ||
172 | /* 0 src: stdin; dst: stdout */ | ||
173 | /* 1 src: file; dst: stdout */ | ||
174 | /* 2 src: file; dst: file */ | ||
175 | if (set_file_pointers((argc - 1), &in, &out, &argv[i])) { | ||
176 | exit(1); | ||
177 | } | ||
178 | |||
179 | subject_init(&s, in, out, NULL); | ||
180 | while (subject_next(&s)) { | ||
181 | subject_study(&s); | ||
182 | } | ||
183 | subject_last(&s); | ||
184 | subject_study(&s); | ||
185 | |||
155 | exit(0); | 186 | exit(0); |
156 | } | 187 | } |
157 | 188 | ||
158 | /* $Id: uniq.c,v 1.2 2000/01/06 01:14:56 erik Exp $ */ | 189 | /* $Id: uniq.c,v 1.3 2000/01/06 23:49:21 beppu Exp $ */ |