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 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 15 | static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 16 | { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 17 | const char *n; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 19 | n = *argv; |
| 20 | if (n != NULL) { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 21 | if ((*n != '-') || n[1]) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 22 | return xfopen(n, "r\0w" + read0write2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 23 | } |
| 24 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 25 | return (read0write2) ? stdout : stdin; |
Rob Landley | 14efdc5 | 2005-09-07 04:18:36 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 28 | int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 29 | int uniq_main(int argc UNUSED_PARAM, 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 | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 32 | const char *s0, *e0, *s1, *e1, *input_filename; |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 33 | unsigned long dups; |
| 34 | unsigned skip_fields, skip_chars, max_chars; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 35 | unsigned opt; |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 36 | unsigned i; |
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, |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 44 | OPT_w = 0x20, |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 45 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 47 | skip_fields = skip_chars = 0; |
Denis Vlasenko | 1a6adbd | 2009-03-09 16:43:28 +0000 | [diff] [blame] | 48 | max_chars = INT_MAX; |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 50 | opt_complementary = "f+:s+:w+"; |
| 51 | opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars); |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 52 | argv += optind; |
| 53 | |
| 54 | input_filename = *argv; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 56 | in = xgetoptfile_uniq_s(argv, 0); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 57 | if (*argv) { |
| 58 | ++argv; |
| 59 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 60 | out = xgetoptfile_uniq_s(argv, 2); |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 61 | if (*argv && argv[1]) { |
| 62 | bb_show_usage(); |
| 63 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 65 | s1 = e1 = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 67 | do { |
| 68 | s0 = s1; |
| 69 | e0 = e1; |
| 70 | dups = 0; |
| 71 | |
| 72 | /* gnu uniq ignores newlines */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 73 | while ((s1 = xmalloc_fgetline(in)) != NULL) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 74 | e1 = s1; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 75 | for (i = skip_fields; i; i--) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 76 | e1 = skip_whitespace(e1); |
Bernhard Reutner-Fischer | de17ece | 2007-04-10 09:38:35 +0000 | [diff] [blame] | 77 | e1 = skip_non_whitespace(e1); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 78 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 79 | for (i = skip_chars; *e1 && i; i--) { |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 80 | ++e1; |
| 81 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 83 | if (!s0 || strncmp(e0, e1, max_chars)) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 84 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 87 | ++dups; /* note: testing for overflow seems excessive. */ |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (s0) { |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 91 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */ |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 92 | fprintf(out, "\0%ld " + (opt & 1), dups + 1); /* 1 == OPT_c */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 93 | fprintf(out, "%s\n", s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 95 | free((void *)s0); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 97 | } while (s1); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 99 | die_if_ferror(in, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 101 | fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 102 | } |