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 | * |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */ |
| 12 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 14 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 15 | static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4"; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 16 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 17 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 18 | { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 19 | const char *n; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 20 | |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 21 | if ((n = *argv) != NULL) { |
| 22 | if ((*n != '-') || n[1]) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 23 | return xfopen(n, "r\0w" + read0write2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 24 | } |
| 25 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 26 | return (read0write2) ? stdout : stdin; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | int uniq_main(int argc, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 30 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | FILE *in, *out; |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 32 | unsigned long dups, skip_fields, skip_chars, i, uniq_flags; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 33 | const char *s0, *e0, *s1, *e1, *input_filename; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | int opt; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 35 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 36 | uniq_flags = skip_fields = skip_chars = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | |
| 38 | while ((opt = getopt(argc, argv, uniq_opts)) > 0) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 39 | if ((opt == 'f') || (opt == 's')) { |
| 40 | int t = bb_xgetularg10(optarg); |
| 41 | if (opt == 'f') { |
| 42 | skip_fields = t; |
| 43 | } else { |
| 44 | skip_chars = t; |
| 45 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 46 | } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 47 | uniq_flags |= s0[4]; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 48 | } else { |
| 49 | bb_show_usage(); |
| 50 | } |
Eric Andersen | 5b5db38 | 2000-12-09 16:37:53 +0000 | [diff] [blame] | 51 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 52 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | input_filename = *(argv += optind); |
| 54 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 55 | in = xgetoptfile_uniq_s(argv, 0); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 56 | if (*argv) { |
| 57 | ++argv; |
| 58 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 59 | out = xgetoptfile_uniq_s(argv, 2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 60 | if (*argv && argv[1]) { |
| 61 | bb_show_usage(); |
| 62 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 63 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 64 | s1 = e1 = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 66 | do { |
| 67 | s0 = s1; |
| 68 | e0 = e1; |
| 69 | dups = 0; |
| 70 | |
| 71 | /* gnu uniq ignores newlines */ |
| 72 | while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { |
| 73 | e1 = s1; |
| 74 | for (i=skip_fields ; i ; i--) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 75 | e1 = skip_whitespace(e1); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 76 | while (*e1 && !isspace(*e1)) { |
| 77 | ++e1; |
| 78 | } |
| 79 | } |
| 80 | for (i = skip_chars ; *e1 && i ; i--) { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 81 | ++e1; |
| 82 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 83 | |
| 84 | if (!s0 || strcmp(e0, e1)) { |
| 85 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 87 | |
| 88 | ++dups; /* Note: Testing for overflow seems excessive. */ |
| 89 | } |
| 90 | |
| 91 | if (s0) { |
| 92 | if (!(uniq_flags & (2 << !!dups))) { |
| 93 | bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 94 | bb_fprintf(out, "%s\n", s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 96 | free((void *)s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 98 | } while (s1); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 100 | xferror(in, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | |
| 102 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 103 | } |