summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2000-09-27 02:29:39 +0000
committerMatt Kraai <kraai@debian.org>2000-09-27 02:29:39 +0000
commite0bcce09baff576b1b16b3ffe780b6d91c7710c2 (patch)
tree69c78c69eaf5ad8e69f54f7c374f5ac3b2c5a950 /coreutils
parent8ce85ce4e3595ac15d4746adc3b5c920fe4db1e8 (diff)
downloadbusybox-w32-e0bcce09baff576b1b16b3ffe780b6d91c7710c2.tar.gz
busybox-w32-e0bcce09baff576b1b16b3ffe780b6d91c7710c2.tar.bz2
busybox-w32-e0bcce09baff576b1b16b3ffe780b6d91c7710c2.zip
Rewrote uniq to be less than a third of the size, and fixed some other
minor problems.
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/uniq.c160
1 files changed, 17 insertions, 143 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index ef38587bd..cfe6cca5e 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -5,6 +5,7 @@
5 * 5 *
6 * Copyright (C) 1999,2000 by Lineo, inc. 6 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com> 7 * Written by John Beppu <beppu@lineo.com>
8 * Rewritten by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * 9 *
9 * This program is free software; you can redistribute it and/or modify 10 * 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 * it under the terms of the GNU General Public License as published by
@@ -27,156 +28,29 @@
27#include <string.h> 28#include <string.h>
28#include <errno.h> 29#include <errno.h>
29 30
30/* max chars in line */
31#define UNIQ_MAX 4096
32
33typedef void (Print) (FILE *, const char *);
34
35typedef int (Decide) (const char *, const char *);
36
37/* container for two lines to be compared */
38typedef struct {
39 char *a;
40 char *b;
41 int recurrence;
42 FILE *in;
43 FILE *out;
44 void *func;
45} Subject;
46
47/* set up all the variables of a uniq operation */
48static Subject *subject_init(Subject * self, FILE * in, FILE * out,
49 void *func)
50{
51 self->a = NULL;
52 self->b = NULL;
53 self->in = in;
54 self->out = out;
55 self->func = func;
56 self->recurrence = 0;
57 return self;
58}
59
60/* point a and b to the appropriate lines;
61 * count the recurrences (if any) of a string;
62 */
63static Subject *subject_next(Subject * self)
64{
65 /* tmp line holders */
66 static char line[2][UNIQ_MAX];
67 static int alternator = 0;
68
69 if (fgets(line[alternator], UNIQ_MAX, self->in)) {
70 self->a = self->b;
71 self->b = line[alternator];
72 alternator ^= 1;
73 return self;
74 }
75
76 return NULL;
77}
78
79static Subject *subject_last(Subject * self)
80{
81 self->a = self->b;
82 self->b = NULL;
83 return self;
84}
85
86static Subject *subject_study(Subject * self)
87{
88 if (self->a == NULL) {
89 return self;
90 }
91 if (self->b == NULL) {
92 fprintf(self->out, "%s", self->a);
93 return self;
94 }
95 if (strcmp(self->a, self->b) == 0) {
96 self->recurrence++;
97 } else {
98 fprintf(self->out, "%s", self->a);
99 self->recurrence = 0;
100 }
101 return self;
102}
103
104static int
105set_file_pointers(int schema, FILE ** in, FILE ** out, char **argv)
106{
107 switch (schema) {
108 case 0:
109 *in = stdin;
110 *out = stdout;
111 break;
112 case 1:
113 *in = fopen(argv[0], "r");
114 *out = stdout;
115 break;
116 case 2:
117 *in = fopen(argv[0], "r");
118 *out = fopen(argv[1], "w");
119 break;
120 }
121 if (*in == NULL) {
122 errorMsg("%s: %s\n", argv[0], strerror(errno));
123 return errno;
124 }
125 if (*out == NULL) {
126 errorMsg("%s: %s\n", argv[1], strerror(errno));
127 return errno;
128 }
129 return 0;
130}
131
132
133/* one variable is the decision algo */
134/* another variable is the printing algo */
135
136/* I don't think I have to have more than a 1 line memory
137 this is the one constant */
138
139/* it seems like GNU/uniq only takes one or two files as an option */
140
141/* ________________________________________________________________________ */
142int uniq_main(int argc, char **argv) 31int uniq_main(int argc, char **argv)
143{ 32{
144 int i; 33 FILE *in = stdin, *out = stdout;
145 char opt; 34 char *lastline = NULL, *input;
146 FILE *in, *out;
147 Subject s;
148 35
149 /* parse argv[] */ 36 /* parse argv[] */
150 for (i = 1; i < argc; i++) { 37 if ((argc > 1 && **(argv + 1) == '-') || argc > 3)
151 if (argv[i][0] == '-') { 38 usage(uniq_usage);
152 opt = argv[i][1];
153 switch (opt) {
154 case '-':
155 case 'h':
156 usage(uniq_usage);
157 default:
158 usage(uniq_usage);
159 }
160 } else {
161 break;
162 }
163 }
164 39
165 /* 0 src: stdin; dst: stdout */ 40 if (argv[1] != NULL) {
166 /* 1 src: file; dst: stdout */ 41 in = xfopen(argv[1], "r");
167 /* 2 src: file; dst: file */ 42 if (argv[2] != NULL)
168 if (set_file_pointers((argc - 1), &in, &out, &argv[i])) { 43 out = xfopen(argv[2], "w");
169 exit(1);
170 } 44 }
171 45
172 subject_init(&s, in, out, NULL); 46 while ((input = get_line_from_file(in)) != NULL) {
173 while (subject_next(&s)) { 47 if (lastline == NULL || strcmp(input, lastline) != 0) {
174 subject_study(&s); 48 fputs(input, out);
49 free(lastline);
50 lastline = input;
51 }
175 } 52 }
176 subject_last(&s); 53 free(lastline);
177 subject_study(&s);
178 54
179 return(0); 55 return EXIT_SUCCESS;
180} 56}
181
182/* $Id: uniq.c,v 1.14 2000/09/25 21:45:58 andersen Exp $ */