7e78be390fea5adc12870cf4659dcf68ff09f99c
[oweals/busybox.git] / coreutils / uniq.c
1 /*
2  * Mini sort implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by John Beppu <beppu@lineo.com>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26
27 static const char uniq_usage[] =
28 "haha\n"
29 ;
30
31 /* max chars in line */
32 #define UNIQ_MAX 4096
33
34 typedef void (Print)(FILE *, const char *);
35
36 typedef int (Decide)(const char *, const char *);
37
38 /* container for two lines to be compared */
39 typedef struct {
40     char    *a;
41     char    *b;
42     int     recurrence;
43     FILE    *in;
44     FILE    *out;
45     void    *func;
46 } Subject;
47
48 /* set up all the variables of a uniq operation */
49 static Subject *
50 subject_init(Subject *self, FILE *in, FILE *out, void *func)
51 {
52     self->a    = NULL;
53     self->b    = NULL;
54     self->in   = in;
55     self->out  = out;
56     self->func = func;
57     self->recurrence = 0;
58     return self;
59 }
60
61 /* point a and b to the appropriate lines;
62  * count the recurrences (if any) of a string;
63  */
64 static Subject *
65 subject_next(Subject *self)
66 {
67     /* tmp line holders */
68     static char line[2][UNIQ_MAX];
69     static int  alternator = 0;
70
71     if (fgets(line[alternator], UNIQ_MAX, self->in)) {
72         self->a = self->b;
73         self->b = line[alternator];
74         alternator ^= 1;
75         return self;
76     }
77
78     return NULL;
79 }
80
81 static Subject *
82 subject_last(Subject *self)
83 {
84     self->a = self->b;
85     self->b = NULL;
86     return self;
87 }
88
89 static Subject *
90 subject_study(Subject *self)
91 {
92     if (self->a == NULL) {
93         return self;
94     }
95     if (self->b == NULL) {
96         fprintf(self->out, "%s", self->a);
97         return self;
98     }
99     if (strcmp(self->a, self->b) == 0) {
100         self->recurrence++;
101     } else {
102         fprintf(self->out, "%s", self->a);
103         self->recurrence = 0;
104     }
105     return self;
106 }
107
108 /* one variable is the decision algo */
109 /* another variable is the printing algo */
110
111 /* I don't think I have to have more than a 1 line memory 
112    this is the one constant */
113
114 /* it seems like GNU/uniq only takes one or two files as an option */
115
116 /* ________________________________________________________________________ */
117 int
118 uniq_main(int argc, char **argv)
119 {
120     int     i;
121     char    opt;
122     FILE    *in, *out;
123     Subject s;
124
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[] */
140     for (i = 1; i < argc; i++) {
141         if (argv[i][0] == '-') {
142             opt = argv[i][1];
143             switch (opt) {
144                 case 'h':
145                     usage(uniq_usage);
146                 default:
147                     usage(uniq_usage);
148             }
149         } else {
150             break;
151         }
152     }
153
154     exit(0);
155 }
156
157 /* $Id: uniq.c,v 1.1 2000/01/06 00:48:21 beppu Exp $ */