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 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * uniq implementation for busybox |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* BB_AUDIT SUSv3 compliant */ |
| 24 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */ |
| 25 | |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <ctype.h> |
| 30 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 31 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | #include "libcoreutils/coreutils.h" |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 33 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4"; |
Eric Andersen | 5b5db38 | 2000-12-09 16:37:53 +0000 | [diff] [blame] | 35 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | int uniq_main(int argc, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 37 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | FILE *in, *out; |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 39 | /* Note: Ignore the warning about dups and e0 being used uninitialized. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | * They will be initialized on the fist pass of the loop (since s0 is NULL). */ |
| 41 | unsigned long dups, skip_fields, skip_chars, i; |
| 42 | const char *s0, *e0, *s1, *e1, *input_filename; |
| 43 | int opt; |
| 44 | int uniq_flags = 6; /* -u */ |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 45 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | skip_fields = skip_chars = 0; |
| 47 | |
| 48 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { |
| 49 | if (opt == 'f') { |
| 50 | skip_fields = bb_xgetularg10(optarg); |
| 51 | } else if (opt == 's') { |
| 52 | skip_chars = bb_xgetularg10(optarg); |
| 53 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { |
| 54 | uniq_flags &= s0[4]; |
| 55 | uniq_flags |= s0[7]; |
| 56 | } else { |
| 57 | bb_show_usage(); |
Eric Andersen | 5b5db38 | 2000-12-09 16:37:53 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 60 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | input_filename = *(argv += optind); |
| 62 | |
| 63 | in = xgetoptfile_sort_uniq(argv, "r"); |
| 64 | if (*argv) { |
| 65 | ++argv; |
| 66 | } |
| 67 | out = xgetoptfile_sort_uniq(argv, "w"); |
| 68 | if (*argv && argv[1]) { |
| 69 | bb_show_usage(); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | s0 = NULL; |
| 73 | |
| 74 | /* gnu uniq ignores newlines */ |
| 75 | while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { |
| 76 | e1 = s1; |
| 77 | for (i=skip_fields ; i ; i--) { |
| 78 | e1 = bb_skip_whitespace(e1); |
| 79 | while (*e1 && !isspace(*e1)) { |
| 80 | ++e1; |
| 81 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | for (i = skip_chars ; *e1 && i ; i--) { |
| 84 | ++e1; |
| 85 | } |
| 86 | if (s0) { |
| 87 | if (strcmp(e0, e1) == 0) { |
| 88 | ++dups; /* Note: Testing for overflow seems excessive. */ |
| 89 | continue; |
| 90 | } |
| 91 | DO_LAST: |
| 92 | if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) { |
| 93 | bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1); |
| 94 | bb_fprintf(out, "%s\n", s0); |
| 95 | } |
| 96 | free((void *)s0); |
| 97 | } |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 98 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | s0 = s1; |
| 100 | e0 = e1; |
| 101 | dups = 0; |
| 102 | } |
| 103 | |
| 104 | if (s0) { |
| 105 | e1 = NULL; |
| 106 | goto DO_LAST; |
| 107 | } |
| 108 | |
| 109 | bb_xferror(in, input_filename); |
| 110 | |
| 111 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 112 | } |