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 | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 15 | int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 16 | int uniq_main(int argc UNUSED_PARAM, char **argv) |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 17 | { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 18 | const char *input_filename; |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 19 | unsigned skip_fields, skip_chars, max_chars; |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 20 | unsigned opt; |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 21 | char *cur_line; |
| 22 | const char *cur_compare; |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 24 | enum { |
| 25 | OPT_c = 0x1, |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 26 | OPT_d = 0x2, /* print only dups */ |
| 27 | OPT_u = 0x4, /* print only uniq */ |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 28 | OPT_f = 0x8, |
| 29 | OPT_s = 0x10, |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 30 | OPT_w = 0x20, |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 31 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 33 | skip_fields = skip_chars = 0; |
Denis Vlasenko | 1a6adbd | 2009-03-09 16:43:28 +0000 | [diff] [blame] | 34 | max_chars = INT_MAX; |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | 96b99b8 | 2008-05-03 07:21:27 +0000 | [diff] [blame] | 36 | opt_complementary = "f+:s+:w+"; |
| 37 | opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars); |
Denis Vlasenko | 4caa09a | 2007-03-31 10:19:11 +0000 | [diff] [blame] | 38 | argv += optind; |
| 39 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 40 | input_filename = argv[0]; |
| 41 | if (input_filename) { |
| 42 | const char *output; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 44 | if (input_filename[0] != '-' || input_filename[1]) { |
| 45 | close(STDIN_FILENO); /* == 0 */ |
| 46 | xopen(input_filename, O_RDONLY); /* fd will be 0 */ |
| 47 | } |
| 48 | output = argv[1]; |
| 49 | if (output) { |
| 50 | if (argv[2]) |
| 51 | bb_show_usage(); |
| 52 | if (output[0] != '-' || output[1]) { |
| 53 | // Won't work with "uniq - FILE" and closed stdin: |
| 54 | //close(STDOUT_FILENO); |
| 55 | //xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 56 | xmove_fd(xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666), STDOUT_FILENO); |
| 57 | } |
| 58 | } |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 59 | } |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 61 | cur_compare = cur_line = NULL; /* prime the pump */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 63 | do { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 64 | unsigned i; |
| 65 | unsigned long dups; |
| 66 | char *old_line; |
| 67 | const char *old_compare; |
| 68 | |
| 69 | old_line = cur_line; |
| 70 | old_compare = cur_compare; |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 71 | dups = 0; |
| 72 | |
| 73 | /* gnu uniq ignores newlines */ |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 74 | while ((cur_line = xmalloc_fgetline(stdin)) != NULL) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 75 | cur_compare = cur_line; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 76 | for (i = skip_fields; i; i--) { |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 77 | cur_compare = skip_whitespace(cur_compare); |
| 78 | cur_compare = skip_non_whitespace(cur_compare); |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 79 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 80 | for (i = skip_chars; *cur_compare && i; i--) { |
| 81 | ++cur_compare; |
Manuel Novoa III | 415f6c9 | 2005-09-08 06:02:49 +0000 | [diff] [blame] | 82 | } |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 84 | if (!old_line || strncmp(old_compare, cur_compare, max_chars)) { |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | a9c9bf5 | 2009-08-30 19:34:09 +0200 | [diff] [blame] | 88 | free(cur_line); |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 89 | ++dups; /* testing for overflow seems excessive */ |
Manuel Novoa III | 84b93f7 | 2005-09-15 08:06:42 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 92 | if (old_line) { |
| 93 | if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */ |
Denys Vlasenko | 44f174e | 2009-08-31 05:15:21 +0200 | [diff] [blame] | 94 | if (opt & OPT_c) { |
| 95 | /* %7lu matches GNU coreutils 6.9 */ |
| 96 | printf("%7lu ", dups + 1); |
| 97 | } |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 98 | printf("%s\n", old_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 100 | free(old_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | } |
Denys Vlasenko | 0aec9ff | 2009-08-30 19:32:45 +0200 | [diff] [blame] | 102 | } while (cur_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | |
Denys Vlasenko | 1249dbb | 2009-08-30 19:35:06 +0200 | [diff] [blame] | 104 | die_if_ferror(stdin, input_filename); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 106 | fflush_stdout_and_exit(EXIT_SUCCESS); |
John Beppu | abb4772 | 2000-01-06 00:48:21 +0000 | [diff] [blame] | 107 | } |