2 * Mini uniq implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@lineo.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 static const char uniq_usage[] =
33 /* max chars in line */
36 typedef void (Print)(FILE *, const char *);
38 typedef int (Decide)(const char *, const char *);
40 /* container for two lines to be compared */
50 /* set up all the variables of a uniq operation */
52 subject_init(Subject *self, FILE *in, FILE *out, void *func)
63 /* point a and b to the appropriate lines;
64 * count the recurrences (if any) of a string;
67 subject_next(Subject *self)
69 /* tmp line holders */
70 static char line[2][UNIQ_MAX];
71 static int alternator = 0;
73 if (fgets(line[alternator], UNIQ_MAX, self->in)) {
75 self->b = line[alternator];
84 subject_last(Subject *self)
92 subject_study(Subject *self)
94 if (self->a == NULL) {
97 if (self->b == NULL) {
98 fprintf(self->out, "%s", self->a);
101 if (strcmp(self->a, self->b) == 0) {
104 fprintf(self->out, "%s", self->a);
105 self->recurrence = 0;
111 set_file_pointers(int schema, FILE **in, FILE **out, char **argv)
119 *in = fopen(argv[0], "r");
123 *in = fopen(argv[0], "r");
124 *out = fopen(argv[1], "w");
128 fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno));
132 fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno));
139 /* one variable is the decision algo */
140 /* another variable is the printing algo */
142 /* I don't think I have to have more than a 1 line memory
143 this is the one constant */
145 /* it seems like GNU/uniq only takes one or two files as an option */
147 /* ________________________________________________________________________ */
149 uniq_main(int argc, char **argv)
157 for (i = 1; i < argc; i++) {
158 if (argv[i][0] == '-') {
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])) {
179 subject_init(&s, in, out, NULL);
180 while (subject_next(&s)) {
189 /* $Id: uniq.c,v 1.3 2000/01/06 23:49:21 beppu Exp $ */