1 /* vi: set sw=4 ts=4: */
3 * Mini uniq implementation for busybox
6 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /* max chars in line */
33 typedef void (Print) (FILE *, const char *);
35 typedef int (Decide) (const char *, const char *);
37 /* container for two lines to be compared */
47 /* set up all the variables of a uniq operation */
48 static Subject *subject_init(Subject * self, FILE * in, FILE * out,
60 /* point a and b to the appropriate lines;
61 * count the recurrences (if any) of a string;
63 static Subject *subject_next(Subject * self)
65 /* tmp line holders */
66 static char line[2][UNIQ_MAX];
67 static int alternator = 0;
69 if (fgets(line[alternator], UNIQ_MAX, self->in)) {
71 self->b = line[alternator];
79 static Subject *subject_last(Subject * self)
86 static Subject *subject_study(Subject * self)
88 if (self->a == NULL) {
91 if (self->b == NULL) {
92 fprintf(self->out, "%s", self->a);
95 if (strcmp(self->a, self->b) == 0) {
98 fprintf(self->out, "%s", self->a);
105 set_file_pointers(int schema, FILE ** in, FILE ** out, char **argv)
113 *in = fopen(argv[0], "r");
117 *in = fopen(argv[0], "r");
118 *out = fopen(argv[1], "w");
122 errorMsg("%s: %s\n", argv[0], strerror(errno));
126 errorMsg("%s: %s\n", argv[1], strerror(errno));
133 /* one variable is the decision algo */
134 /* another variable is the printing algo */
136 /* I don't think I have to have more than a 1 line memory
137 this is the one constant */
139 /* it seems like GNU/uniq only takes one or two files as an option */
141 /* ________________________________________________________________________ */
142 int uniq_main(int argc, char **argv)
150 for (i = 1; i < argc; i++) {
151 if (argv[i][0] == '-') {
165 /* 0 src: stdin; dst: stdout */
166 /* 1 src: file; dst: stdout */
167 /* 2 src: file; dst: file */
168 if (set_file_pointers((argc - 1), &in, &out, &argv[i])) {
172 subject_init(&s, in, out, NULL);
173 while (subject_next(&s)) {
182 /* $Id: uniq.c,v 1.13 2000/07/16 20:57:15 kraai Exp $ */