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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 13 | //usage:#define uniq_trivial_usage |
| 14 | //usage: "[-cdu][-f,s,w N] [INPUT [OUTPUT]]" |
| 15 | //usage:#define uniq_full_usage "\n\n" |
| 16 | //usage: "Discard duplicate lines\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 17 | //usage: "\n -c Prefix lines by the number of occurrences" |
| 18 | //usage: "\n -d Only print duplicate lines" |
| 19 | //usage: "\n -u Only print unique lines" |
| 20 | //usage: "\n -f N Skip first N fields" |
| 21 | //usage: "\n -s N Skip first N chars (after any skipped fields)" |
| 22 | //usage: "\n -w N Compare N characters in line" |
| 23 | //usage: |
| 24 | //usage:#define uniq_example_usage |
| 25 | //usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" |
| 26 | //usage: "a\n" |
| 27 | //usage: "b\n" |
| 28 | //usage: "c\n" |
| 29 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 32 | int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 33 | int uniq_main(int argc UNUSED_PARAM, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 34 | { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 35 | const char *input_filename; |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 36 | unsigned skip_fields, skip_chars, max_chars; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 37 | unsigned opt; |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 38 | char *cur_line; |
| 39 | const char *cur_compare; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 41 | enum { |
| 42 | OPT_c = 0x1, |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 43 | OPT_d = 0x2, /* print only dups */ |
| 44 | OPT_u = 0x4, /* print only uniq */ |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 45 | OPT_f = 0x8, |
| 46 | OPT_s = 0x10, |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 47 | OPT_w = 0x20, |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 48 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 50 | skip_fields = skip_chars = 0; |
Denis Vlasenko | 1a6adbd | 2009-03-09 16:43:28 +0000 | [diff] [blame] | 51 | max_chars = INT_MAX; |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 53 | opt_complementary = "f+:s+:w+"; |
| 54 | opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars); |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 55 | argv += optind; |
| 56 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 57 | input_filename = argv[0]; |
| 58 | if (input_filename) { |
| 59 | const char *output; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 61 | if (input_filename[0] != '-' || input_filename[1]) { |
| 62 | close(STDIN_FILENO); /* == 0 */ |
| 63 | xopen(input_filename, O_RDONLY); /* fd will be 0 */ |
| 64 | } |
| 65 | output = argv[1]; |
| 66 | if (output) { |
| 67 | if (argv[2]) |
| 68 | bb_show_usage(); |
| 69 | if (output[0] != '-' || output[1]) { |
| 70 | // Won't work with "uniq - FILE" and closed stdin: |
| 71 | //close(STDOUT_FILENO); |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 72 | //xopen(output, O_WRONLY | O_CREAT | O_TRUNC); |
| 73 | xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 74 | } |
| 75 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 76 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 77 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 78 | cur_compare = cur_line = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 80 | do { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 81 | unsigned i; |
| 82 | unsigned long dups; |
| 83 | char *old_line; |
| 84 | const char *old_compare; |
| 85 | |
| 86 | old_line = cur_line; |
| 87 | old_compare = cur_compare; |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 88 | dups = 0; |
| 89 | |
| 90 | /* gnu uniq ignores newlines */ |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 91 | while ((cur_line = xmalloc_fgetline(stdin)) != NULL) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 92 | cur_compare = cur_line; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 93 | for (i = skip_fields; i; i--) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 94 | cur_compare = skip_whitespace(cur_compare); |
| 95 | cur_compare = skip_non_whitespace(cur_compare); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 96 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 97 | for (i = skip_chars; *cur_compare && i; i--) { |
| 98 | ++cur_compare; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 99 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 100 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 101 | if (!old_line || strncmp(old_compare, cur_compare, max_chars)) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 102 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 104 | |
Denys Vlasenko | a9c9bf5 | 2009-08-30 19:34:09 +0200 | [diff] [blame] | 105 | free(cur_line); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 106 | ++dups; /* testing for overflow seems excessive */ |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 109 | if (old_line) { |
| 110 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */ |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 111 | if (opt & OPT_c) { |
| 112 | /* %7lu matches GNU coreutils 6.9 */ |
| 113 | printf("%7lu ", dups + 1); |
| 114 | } |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 115 | printf("%s\n", old_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 117 | free(old_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 119 | } while (cur_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 121 | die_if_ferror(stdin, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 123 | fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 124 | } |