diff options
author | Matt Kraai <kraai@debian.org> | 2000-09-27 02:29:39 +0000 |
---|---|---|
committer | Matt Kraai <kraai@debian.org> | 2000-09-27 02:29:39 +0000 |
commit | e0bcce09baff576b1b16b3ffe780b6d91c7710c2 (patch) | |
tree | 69c78c69eaf5ad8e69f54f7c374f5ac3b2c5a950 | |
parent | 8ce85ce4e3595ac15d4746adc3b5c920fe4db1e8 (diff) | |
download | busybox-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.
-rw-r--r-- | busybox.h | 1 | ||||
-rw-r--r-- | coreutils/uniq.c | 160 | ||||
-rw-r--r-- | include/busybox.h | 1 | ||||
-rw-r--r-- | uniq.c | 160 | ||||
-rw-r--r-- | utility.c | 22 |
5 files changed, 52 insertions, 292 deletions
@@ -395,6 +395,7 @@ extern int print_file_by_name(char *filename); | |||
395 | extern char process_escape_sequence(char **ptr); | 395 | extern char process_escape_sequence(char **ptr); |
396 | extern char *get_last_path_component(char *path); | 396 | extern char *get_last_path_component(char *path); |
397 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); | 397 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); |
398 | extern FILE *xfopen(const char *path, const char *mode); | ||
398 | 399 | ||
399 | #ifndef DMALLOC | 400 | #ifndef DMALLOC |
400 | extern void *xmalloc (size_t size); | 401 | extern void *xmalloc (size_t size); |
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 | |||
33 | typedef void (Print) (FILE *, const char *); | ||
34 | |||
35 | typedef int (Decide) (const char *, const char *); | ||
36 | |||
37 | /* container for two lines to be compared */ | ||
38 | typedef 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 */ | ||
48 | static 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 | */ | ||
63 | static 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 | |||
79 | static Subject *subject_last(Subject * self) | ||
80 | { | ||
81 | self->a = self->b; | ||
82 | self->b = NULL; | ||
83 | return self; | ||
84 | } | ||
85 | |||
86 | static 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 | |||
104 | static int | ||
105 | set_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 | /* ________________________________________________________________________ */ | ||
142 | int uniq_main(int argc, char **argv) | 31 | int 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 $ */ | ||
diff --git a/include/busybox.h b/include/busybox.h index 69f455435..faad206ee 100644 --- a/include/busybox.h +++ b/include/busybox.h | |||
@@ -395,6 +395,7 @@ extern int print_file_by_name(char *filename); | |||
395 | extern char process_escape_sequence(char **ptr); | 395 | extern char process_escape_sequence(char **ptr); |
396 | extern char *get_last_path_component(char *path); | 396 | extern char *get_last_path_component(char *path); |
397 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); | 397 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); |
398 | extern FILE *xfopen(const char *path, const char *mode); | ||
398 | 399 | ||
399 | #ifndef DMALLOC | 400 | #ifndef DMALLOC |
400 | extern void *xmalloc (size_t size); | 401 | extern void *xmalloc (size_t size); |
@@ -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 | |||
33 | typedef void (Print) (FILE *, const char *); | ||
34 | |||
35 | typedef int (Decide) (const char *, const char *); | ||
36 | |||
37 | /* container for two lines to be compared */ | ||
38 | typedef 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 */ | ||
48 | static 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 | */ | ||
63 | static 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 | |||
79 | static Subject *subject_last(Subject * self) | ||
80 | { | ||
81 | self->a = self->b; | ||
82 | self->b = NULL; | ||
83 | return self; | ||
84 | } | ||
85 | |||
86 | static 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 | |||
104 | static int | ||
105 | set_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 | /* ________________________________________________________________________ */ | ||
142 | int uniq_main(int argc, char **argv) | 31 | int 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 $ */ | ||
@@ -81,7 +81,7 @@ extern void usage(const char *usage) | |||
81 | { | 81 | { |
82 | fprintf(stderr, "%s\n\n", full_version); | 82 | fprintf(stderr, "%s\n\n", full_version); |
83 | fprintf(stderr, "Usage: %s\n", usage); | 83 | fprintf(stderr, "Usage: %s\n", usage); |
84 | exit FALSE; | 84 | exit(EXIT_FAILURE); |
85 | } | 85 | } |
86 | 86 | ||
87 | extern void errorMsg(const char *s, ...) | 87 | extern void errorMsg(const char *s, ...) |
@@ -106,7 +106,7 @@ extern void fatalError(const char *s, ...) | |||
106 | vfprintf(stderr, s, p); | 106 | vfprintf(stderr, s, p); |
107 | va_end(p); | 107 | va_end(p); |
108 | fflush(stderr); | 108 | fflush(stderr); |
109 | exit( FALSE); | 109 | exit(EXIT_FAILURE); |
110 | } | 110 | } |
111 | 111 | ||
112 | #if defined BB_INIT | 112 | #if defined BB_INIT |
@@ -401,17 +401,17 @@ copyFile(const char *srcName, const char *destName, | |||
401 | /* This is fine, since symlinks never get here */ | 401 | /* This is fine, since symlinks never get here */ |
402 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) { | 402 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) { |
403 | perror(destName); | 403 | perror(destName); |
404 | exit FALSE; | 404 | exit(EXIT_FAILURE); |
405 | } | 405 | } |
406 | if (chmod(destName, srcStatBuf.st_mode) < 0) { | 406 | if (chmod(destName, srcStatBuf.st_mode) < 0) { |
407 | perror(destName); | 407 | perror(destName); |
408 | exit FALSE; | 408 | exit(EXIT_FAILURE); |
409 | } | 409 | } |
410 | times.actime = srcStatBuf.st_atime; | 410 | times.actime = srcStatBuf.st_atime; |
411 | times.modtime = srcStatBuf.st_mtime; | 411 | times.modtime = srcStatBuf.st_mtime; |
412 | if (utime(destName, ×) < 0) { | 412 | if (utime(destName, ×) < 0) { |
413 | perror(destName); | 413 | perror(destName); |
414 | exit FALSE; | 414 | exit(EXIT_FAILURE); |
415 | } | 415 | } |
416 | } | 416 | } |
417 | 417 | ||
@@ -1713,11 +1713,21 @@ void xregcomp(regex_t *preg, const char *regex, int cflags) | |||
1713 | int errmsgsz = regerror(ret, preg, NULL, 0); | 1713 | int errmsgsz = regerror(ret, preg, NULL, 0); |
1714 | char *errmsg = xmalloc(errmsgsz); | 1714 | char *errmsg = xmalloc(errmsgsz); |
1715 | regerror(ret, preg, errmsg, errmsgsz); | 1715 | regerror(ret, preg, errmsg, errmsgsz); |
1716 | fatalError("bb_regcomp: %s\n", errmsg); | 1716 | fatalError("xregcomp: %s\n", errmsg); |
1717 | } | 1717 | } |
1718 | } | 1718 | } |
1719 | #endif | 1719 | #endif |
1720 | 1720 | ||
1721 | #if defined BB_UNIQ | ||
1722 | FILE *xfopen(const char *path, const char *mode) | ||
1723 | { | ||
1724 | FILE *fp; | ||
1725 | if ((fp = fopen(path, mode)) == NULL) | ||
1726 | fatalError("%s: %s\n", path, strerror(errno)); | ||
1727 | return fp; | ||
1728 | } | ||
1729 | #endif | ||
1730 | |||
1721 | /* END CODE */ | 1731 | /* END CODE */ |
1722 | /* | 1732 | /* |
1723 | Local Variables: | 1733 | Local Variables: |