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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 15 | static const char uniq_opts[] ALIGN1 = "cdu" "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 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 21 | n = *argv; |
| 22 | if (n != NULL) { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 23 | if ((*n != '-') || n[1]) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 24 | return xfopen(n, "r\0w" + read0write2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 25 | } |
| 26 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 27 | return (read0write2) ? stdout : stdin; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 30 | int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | int uniq_main(int argc, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 32 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | FILE *in, *out; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 34 | unsigned long dups, skip_fields, skip_chars, i; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 35 | const char *s0, *e0, *s1, *e1, *input_filename; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 36 | unsigned opt; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 38 | enum { |
| 39 | OPT_c = 0x1, |
| 40 | OPT_d = 0x2, |
| 41 | OPT_u = 0x4, |
| 42 | OPT_f = 0x8, |
| 43 | OPT_s = 0x10, |
| 44 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 46 | skip_fields = skip_chars = 0; |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 48 | opt = getopt32(argv, "cduf:s:", &s0, &s1); |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 49 | if (opt & OPT_f) |
| 50 | skip_fields = xatoul(s0); |
| 51 | if (opt & OPT_s) |
| 52 | skip_chars = xatoul(s1); |
| 53 | argv += optind; |
| 54 | |
| 55 | input_filename = *argv; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 57 | in = xgetoptfile_uniq_s(argv, 0); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 58 | if (*argv) { |
| 59 | ++argv; |
| 60 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 61 | out = xgetoptfile_uniq_s(argv, 2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 62 | if (*argv && argv[1]) { |
| 63 | bb_show_usage(); |
| 64 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 65 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 66 | s1 = e1 = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 68 | do { |
| 69 | s0 = s1; |
| 70 | e0 = e1; |
| 71 | dups = 0; |
| 72 | |
| 73 | /* gnu uniq ignores newlines */ |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 74 | while ((s1 = xmalloc_getline(in)) != NULL) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 75 | e1 = s1; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 76 | for (i = skip_fields; i; i--) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 77 | e1 = skip_whitespace(e1); |
Bernhard Reutner-Fischer | de17ece | 2007-04-10 09:38:35 +0000 | [diff] [blame] | 78 | e1 = skip_non_whitespace(e1); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 79 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 92 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */ |
| 93 | fprintf(out, "\0%d " + (opt & 1), dups + 1); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 94 | 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 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 100 | die_if_ferror(in, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 102 | fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 103 | } |