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 | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org> |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 6 | * |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 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 |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 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 | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 34 | static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4"; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 35 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 36 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 37 | { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 38 | const char *n; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 39 | |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 40 | if ((n = *argv) != NULL) { |
| 41 | if ((*n != '-') || n[1]) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 42 | return bb_xfopen(n, "r\0w" + read0write2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 45 | return (read0write2) ? stdout : stdin; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | int uniq_main(int argc, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 49 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | FILE *in, *out; |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 51 | unsigned long dups, skip_fields, skip_chars, i, uniq_flags; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 52 | const char *s0, *e0, *s1, *e1, *input_filename; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | int opt; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 54 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 55 | uniq_flags = skip_fields = skip_chars = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | |
| 57 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 58 | if ((opt == 'f') || (opt == 's')) { |
| 59 | int t = bb_xgetularg10(optarg); |
| 60 | if (opt == 'f') { |
| 61 | skip_fields = t; |
| 62 | } else { |
| 63 | skip_chars = t; |
| 64 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 65 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 66 | uniq_flags |= s0[4]; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 67 | } else { |
| 68 | bb_show_usage(); |
| 69 | } |
Eric Andersen | 5b5db38 | 2000-12-09 16:37:53 +0000 | [diff] [blame] | 70 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 71 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | input_filename = *(argv += optind); |
| 73 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 74 | in = xgetoptfile_uniq_s(argv, 0); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 75 | if (*argv) { |
| 76 | ++argv; |
| 77 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 78 | out = xgetoptfile_uniq_s(argv, 2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 79 | if (*argv && argv[1]) { |
| 80 | bb_show_usage(); |
| 81 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 82 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 83 | s1 = e1 = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 85 | do { |
| 86 | s0 = s1; |
| 87 | e0 = e1; |
| 88 | dups = 0; |
| 89 | |
| 90 | /* gnu uniq ignores newlines */ |
| 91 | while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { |
| 92 | e1 = s1; |
| 93 | for (i=skip_fields ; i ; i--) { |
| 94 | e1 = bb_skip_whitespace(e1); |
| 95 | while (*e1 && !isspace(*e1)) { |
| 96 | ++e1; |
| 97 | } |
| 98 | } |
| 99 | for (i = skip_chars ; *e1 && i ; i--) { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 100 | ++e1; |
| 101 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 102 | |
| 103 | if (!s0 || strcmp(e0, e1)) { |
| 104 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 106 | |
| 107 | ++dups; /* Note: Testing for overflow seems excessive. */ |
| 108 | } |
| 109 | |
| 110 | if (s0) { |
| 111 | if (!(uniq_flags & (2 << !!dups))) { |
| 112 | bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 113 | bb_fprintf(out, "%s\n", s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 115 | free((void *)s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 117 | } while (s1); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | |
| 119 | bb_xferror(in, input_filename); |
| 120 | |
| 121 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 122 | } |