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[] =
30 "Usage: uniq [OPTION]... [INPUT [OUTPUT]]\n"
31 "Discard all but one of successive identical lines from INPUT (or\n"
32 "standard input), writing to OUTPUT (or standard output).\n"
34 "\t-h\tdisplay this help and exit\n"
36 "A field is a run of whitespace, then non-whitespace characters.\n"
37 "Fields are skipped before chars.\n"
40 /* max chars in line */
43 typedef void (Print)(FILE *, const char *);
45 typedef int (Decide)(const char *, const char *);
47 /* container for two lines to be compared */
57 /* set up all the variables of a uniq operation */
59 subject_init(Subject *self, FILE *in, FILE *out, void *func)
70 /* point a and b to the appropriate lines;
71 * count the recurrences (if any) of a string;
74 subject_next(Subject *self)
76 /* tmp line holders */
77 static char line[2][UNIQ_MAX];
78 static int alternator = 0;
80 if (fgets(line[alternator], UNIQ_MAX, self->in)) {
82 self->b = line[alternator];
91 subject_last(Subject *self)
99 subject_study(Subject *self)
101 if (self->a == NULL) {
104 if (self->b == NULL) {
105 fprintf(self->out, "%s", self->a);
108 if (strcmp(self->a, self->b) == 0) {
111 fprintf(self->out, "%s", self->a);
112 self->recurrence = 0;
118 set_file_pointers(int schema, FILE **in, FILE **out, char **argv)
126 *in = fopen(argv[0], "r");
130 *in = fopen(argv[0], "r");
131 *out = fopen(argv[1], "w");
135 fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno));
139 fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno));
146 /* one variable is the decision algo */
147 /* another variable is the printing algo */
149 /* I don't think I have to have more than a 1 line memory
150 this is the one constant */
152 /* it seems like GNU/uniq only takes one or two files as an option */
154 /* ________________________________________________________________________ */
156 uniq_main(int argc, char **argv)
164 for (i = 1; i < argc; i++) {
165 if (argv[i][0] == '-') {
179 /* 0 src: stdin; dst: stdout */
180 /* 1 src: file; dst: stdout */
181 /* 2 src: file; dst: file */
182 if (set_file_pointers((argc - 1), &in, &out, &argv[i])) {
186 subject_init(&s, in, out, NULL);
187 while (subject_next(&s)) {
196 /* $Id: uniq.c,v 1.4 2000/01/07 01:57:32 beppu Exp $ */