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 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config UNIQ |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "uniq (4.9 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: uniq is used to remove duplicate lines from a sorted file. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | |
| 15 | //applet:IF_UNIQ(APPLET(uniq, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 16 | |
| 17 | //kbuild:lib-$(CONFIG_UNIQ) += uniq.o |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | /* BB_AUDIT SUSv3 compliant */ |
| 20 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */ |
| 21 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | //usage:#define uniq_trivial_usage |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 23 | //usage: "[-cduiz] [-f,s,w N] [FILE [OUTFILE]]" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 24 | //usage:#define uniq_full_usage "\n\n" |
| 25 | //usage: "Discard duplicate lines\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 26 | //usage: "\n -c Prefix lines by the number of occurrences" |
| 27 | //usage: "\n -d Only print duplicate lines" |
| 28 | //usage: "\n -u Only print unique lines" |
Jody Bruchon | 19578b4 | 2017-03-30 12:24:56 -0400 | [diff] [blame] | 29 | //usage: "\n -i Ignore case" |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 30 | //usage: "\n -z NUL terminated output" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 31 | //usage: "\n -f N Skip first N fields" |
| 32 | //usage: "\n -s N Skip first N chars (after any skipped fields)" |
| 33 | //usage: "\n -w N Compare N characters in line" |
| 34 | //usage: |
| 35 | //usage:#define uniq_example_usage |
| 36 | //usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" |
| 37 | //usage: "a\n" |
| 38 | //usage: "b\n" |
| 39 | //usage: "c\n" |
| 40 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 41 | #include "libbb.h" |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 43 | int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 44 | int uniq_main(int argc UNUSED_PARAM, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 45 | { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 46 | const char *input_filename; |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 47 | unsigned skip_fields, skip_chars, max_chars; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 48 | unsigned opt; |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 49 | char eol; |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 50 | char *cur_line; |
| 51 | const char *cur_compare; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 53 | enum { |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 54 | OPT_c = 1 << 0, |
| 55 | OPT_d = 1 << 1, /* print only dups */ |
| 56 | OPT_u = 1 << 2, /* print only uniq */ |
| 57 | OPT_f = 1 << 3, |
| 58 | OPT_s = 1 << 4, |
| 59 | OPT_w = 1 << 5, |
| 60 | OPT_i = 1 << 6, |
| 61 | OPT_z = 1 << 7, |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 62 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 64 | skip_fields = skip_chars = 0; |
Denis Vlasenko | 1a6adbd | 2009-03-09 16:43:28 +0000 | [diff] [blame] | 65 | max_chars = INT_MAX; |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 66 | |
Ron Yorston | 2dade4f | 2021-06-24 15:07:37 +0100 | [diff] [blame] | 67 | opt = getopt32(argv, "cduf:+s:+w:+iz", &skip_fields, &skip_chars, &max_chars); |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 68 | argv += optind; |
| 69 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 70 | input_filename = argv[0]; |
| 71 | if (input_filename) { |
| 72 | const char *output; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 74 | if (input_filename[0] != '-' || input_filename[1]) { |
| 75 | close(STDIN_FILENO); /* == 0 */ |
| 76 | xopen(input_filename, O_RDONLY); /* fd will be 0 */ |
| 77 | } |
| 78 | output = argv[1]; |
| 79 | if (output) { |
| 80 | if (argv[2]) |
| 81 | bb_show_usage(); |
| 82 | if (output[0] != '-' || output[1]) { |
| 83 | // Won't work with "uniq - FILE" and closed stdin: |
| 84 | //close(STDOUT_FILENO); |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 85 | //xopen(output, O_WRONLY | O_CREAT | O_TRUNC); |
| 86 | xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 89 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 91 | cur_compare = cur_line = NULL; /* prime the pump */ |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 92 | eol = (opt & OPT_z) ? 0 : '\n'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 94 | do { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 95 | unsigned i; |
| 96 | unsigned long dups; |
| 97 | char *old_line; |
| 98 | const char *old_compare; |
| 99 | |
| 100 | old_line = cur_line; |
| 101 | old_compare = cur_compare; |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 102 | dups = 0; |
| 103 | |
| 104 | /* gnu uniq ignores newlines */ |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 105 | while ((cur_line = xmalloc_fgetline(stdin)) != NULL) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 106 | cur_compare = cur_line; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 107 | for (i = skip_fields; i; i--) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 108 | cur_compare = skip_whitespace(cur_compare); |
| 109 | cur_compare = skip_non_whitespace(cur_compare); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 110 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 111 | for (i = skip_chars; *cur_compare && i; i--) { |
| 112 | ++cur_compare; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 113 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 114 | |
Jody Bruchon | 8808f7f | 2017-03-30 18:15:54 +0200 | [diff] [blame] | 115 | if (!old_line) |
| 116 | break; |
| 117 | if ((opt & OPT_i) |
| 118 | ? strncasecmp(old_compare, cur_compare, max_chars) |
| 119 | : strncmp(old_compare, cur_compare, max_chars) |
| 120 | ) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 121 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 123 | |
Denys Vlasenko | a9c9bf5 | 2009-08-30 19:34:09 +0200 | [diff] [blame] | 124 | free(cur_line); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 125 | ++dups; /* testing for overflow seems excessive */ |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 128 | if (old_line) { |
| 129 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */ |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 130 | if (opt & OPT_c) { |
| 131 | /* %7lu matches GNU coreutils 6.9 */ |
| 132 | printf("%7lu ", dups + 1); |
| 133 | } |
Denys Vlasenko | 894466c | 2021-06-17 00:36:13 +0200 | [diff] [blame] | 134 | printf("%s%c", old_line, eol); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 136 | free(old_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 138 | } while (cur_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 140 | die_if_ferror(stdin, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 142 | fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 143 | } |