Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 0b874ed | 2000-01-06 01:14:56 +0000 | [diff] [blame] | 3 | * Mini uniq implementation for busybox |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 4 | * |
| 5 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 by Lineo, inc. |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 7 | * Written by John Beppu <beppu@lineo.com> |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 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 |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "internal.h" |
| 26 | #include <stdio.h> |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <errno.h> |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 29 | |
| 30 | static const char uniq_usage[] = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame^] | 31 | "uniq [OPTION]... [INPUT [OUTPUT]]\n" |
| 32 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 33 | "\nDiscard all but one of successive identical lines from INPUT\n" |
| 34 | "(or standard input), writing to OUTPUT (or standard output).\n" |
| 35 | #endif |
| 36 | ; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 37 | |
| 38 | /* max chars in line */ |
| 39 | #define UNIQ_MAX 4096 |
| 40 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | typedef void (Print) (FILE *, const char *); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 42 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | typedef int (Decide) (const char *, const char *); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 44 | |
| 45 | /* container for two lines to be compared */ |
| 46 | typedef struct { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | char *a; |
| 48 | char *b; |
| 49 | int recurrence; |
| 50 | FILE *in; |
| 51 | FILE *out; |
| 52 | void *func; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 53 | } Subject; |
| 54 | |
| 55 | /* set up all the variables of a uniq operation */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | static Subject *subject_init(Subject * self, FILE * in, FILE * out, |
| 57 | void *func) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 58 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | self->a = NULL; |
| 60 | self->b = NULL; |
| 61 | self->in = in; |
| 62 | self->out = out; |
| 63 | self->func = func; |
| 64 | self->recurrence = 0; |
| 65 | return self; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* point a and b to the appropriate lines; |
| 69 | * count the recurrences (if any) of a string; |
| 70 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | static Subject *subject_next(Subject * self) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 72 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | /* tmp line holders */ |
| 74 | static char line[2][UNIQ_MAX]; |
| 75 | static int alternator = 0; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | if (fgets(line[alternator], UNIQ_MAX, self->in)) { |
| 78 | self->a = self->b; |
| 79 | self->b = line[alternator]; |
| 80 | alternator ^= 1; |
| 81 | return self; |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | static Subject *subject_last(Subject * self) |
| 88 | { |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 89 | self->a = self->b; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | self->b = NULL; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 91 | return self; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | static Subject *subject_study(Subject * self) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 95 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | if (self->a == NULL) { |
| 97 | return self; |
| 98 | } |
| 99 | if (self->b == NULL) { |
| 100 | fprintf(self->out, "%s", self->a); |
| 101 | return self; |
| 102 | } |
| 103 | if (strcmp(self->a, self->b) == 0) { |
| 104 | self->recurrence++; |
| 105 | } else { |
| 106 | fprintf(self->out, "%s", self->a); |
| 107 | self->recurrence = 0; |
| 108 | } |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 109 | return self; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 110 | } |
| 111 | |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 112 | static int |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | set_file_pointers(int schema, FILE ** in, FILE ** out, char **argv) |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 114 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | switch (schema) { |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 116 | case 0: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | *in = stdin; |
| 118 | *out = stdout; |
| 119 | break; |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 120 | case 1: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | *in = fopen(argv[0], "r"); |
| 122 | *out = stdout; |
| 123 | break; |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 124 | case 2: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | *in = fopen(argv[0], "r"); |
| 126 | *out = fopen(argv[1], "w"); |
| 127 | break; |
| 128 | } |
| 129 | if (*in == NULL) { |
| 130 | fprintf(stderr, "uniq: %s: %s\n", argv[0], strerror(errno)); |
| 131 | return errno; |
| 132 | } |
| 133 | if (*out == NULL) { |
| 134 | fprintf(stderr, "uniq: %s: %s\n", argv[1], strerror(errno)); |
| 135 | return errno; |
| 136 | } |
| 137 | return 0; |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 141 | /* one variable is the decision algo */ |
| 142 | /* another variable is the printing algo */ |
| 143 | |
| 144 | /* I don't think I have to have more than a 1 line memory |
| 145 | this is the one constant */ |
| 146 | |
| 147 | /* it seems like GNU/uniq only takes one or two files as an option */ |
| 148 | |
| 149 | /* ________________________________________________________________________ */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | int uniq_main(int argc, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 151 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | int i; |
| 153 | char opt; |
| 154 | FILE *in, *out; |
| 155 | Subject s; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 156 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | /* parse argv[] */ |
| 158 | for (i = 1; i < argc; i++) { |
| 159 | if (argv[i][0] == '-') { |
| 160 | opt = argv[i][1]; |
| 161 | switch (opt) { |
| 162 | case '-': |
| 163 | case 'h': |
| 164 | usage(uniq_usage); |
| 165 | default: |
| 166 | usage(uniq_usage); |
| 167 | } |
| 168 | } else { |
| 169 | break; |
| 170 | } |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 171 | } |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | /* 0 src: stdin; dst: stdout */ |
| 174 | /* 1 src: file; dst: stdout */ |
| 175 | /* 2 src: file; dst: file */ |
| 176 | if (set_file_pointers((argc - 1), &in, &out, &argv[i])) { |
| 177 | exit(1); |
| 178 | } |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 179 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 180 | subject_init(&s, in, out, NULL); |
| 181 | while (subject_next(&s)) { |
| 182 | subject_study(&s); |
| 183 | } |
| 184 | subject_last(&s); |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 185 | subject_study(&s); |
John Beppu | 96f1f33 | 2000-01-06 23:49:21 +0000 | [diff] [blame] | 186 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | exit(0); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame^] | 190 | /* $Id: uniq.c,v 1.10 2000/05/12 19:41:47 erik Exp $ */ |