blob: 6e764d8a4d34fb07afc846abdd8ca5e1916a9ecd [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppuabb47722000-01-06 00:48:21 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * uniq implementation for busybox
John Beppuabb47722000-01-06 00:48:21 +00004 *
Manuel Novoa III 84b93f72005-09-15 08:06:42 +00005 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
John Beppuabb47722000-01-06 00:48:21 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppuabb47722000-01-06 00:48:21 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
12
Pere Orga34425382011-03-31 14:43:25 +020013//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"
17//usage: "\nOptions:"
18//usage: "\n -c Prefix lines by the number of occurrences"
19//usage: "\n -d Only print duplicate lines"
20//usage: "\n -u Only print unique lines"
21//usage: "\n -f N Skip first N fields"
22//usage: "\n -s N Skip first N chars (after any skipped fields)"
23//usage: "\n -w N Compare N characters in line"
24//usage:
25//usage:#define uniq_example_usage
26//usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
27//usage: "a\n"
28//usage: "b\n"
29//usage: "c\n"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
John Beppuabb47722000-01-06 00:48:21 +000032
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000033int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000034int uniq_main(int argc UNUSED_PARAM, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000035{
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020036 const char *input_filename;
Denis Vlasenko96b99b82008-05-03 07:21:27 +000037 unsigned skip_fields, skip_chars, max_chars;
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000038 unsigned opt;
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020039 char *cur_line;
40 const char *cur_compare;
John Beppuabb47722000-01-06 00:48:21 +000041
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000042 enum {
43 OPT_c = 0x1,
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020044 OPT_d = 0x2, /* print only dups */
45 OPT_u = 0x4, /* print only uniq */
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000046 OPT_f = 0x8,
47 OPT_s = 0x10,
Denis Vlasenko96b99b82008-05-03 07:21:27 +000048 OPT_w = 0x20,
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000049 };
Manuel Novoa III cad53642003-03-19 09:13:01 +000050
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000051 skip_fields = skip_chars = 0;
Denis Vlasenko1a6adbd2009-03-09 16:43:28 +000052 max_chars = INT_MAX;
Matt Kraaie0bcce02000-09-27 02:29:39 +000053
Denis Vlasenko96b99b82008-05-03 07:21:27 +000054 opt_complementary = "f+:s+:w+";
55 opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000056 argv += optind;
57
Denys Vlasenko44f174e2009-08-31 05:15:21 +020058 input_filename = argv[0];
59 if (input_filename) {
60 const char *output;
Manuel Novoa III cad53642003-03-19 09:13:01 +000061
Denys Vlasenko44f174e2009-08-31 05:15:21 +020062 if (input_filename[0] != '-' || input_filename[1]) {
63 close(STDIN_FILENO); /* == 0 */
64 xopen(input_filename, O_RDONLY); /* fd will be 0 */
65 }
66 output = argv[1];
67 if (output) {
68 if (argv[2])
69 bb_show_usage();
70 if (output[0] != '-' || output[1]) {
71 // Won't work with "uniq - FILE" and closed stdin:
72 //close(STDOUT_FILENO);
Denys Vlasenkoc05387d2010-10-18 02:38:27 +020073 //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
74 xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
Denys Vlasenko44f174e2009-08-31 05:15:21 +020075 }
76 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000077 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000078
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020079 cur_compare = cur_line = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000081 do {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020082 unsigned i;
83 unsigned long dups;
84 char *old_line;
85 const char *old_compare;
86
87 old_line = cur_line;
88 old_compare = cur_compare;
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000089 dups = 0;
90
91 /* gnu uniq ignores newlines */
Denys Vlasenko1249dbb2009-08-30 19:35:06 +020092 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020093 cur_compare = cur_line;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000094 for (i = skip_fields; i; i--) {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020095 cur_compare = skip_whitespace(cur_compare);
96 cur_compare = skip_non_whitespace(cur_compare);
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000097 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020098 for (i = skip_chars; *cur_compare && i; i--) {
99 ++cur_compare;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000100 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000101
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200102 if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000103 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000105
Denys Vlasenkoa9c9bf52009-08-30 19:34:09 +0200106 free(cur_line);
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200107 ++dups; /* testing for overflow seems excessive */
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000108 }
109
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200110 if (old_line) {
111 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
Denys Vlasenko44f174e2009-08-31 05:15:21 +0200112 if (opt & OPT_c) {
113 /* %7lu matches GNU coreutils 6.9 */
114 printf("%7lu ", dups + 1);
115 }
Denys Vlasenko1249dbb2009-08-30 19:35:06 +0200116 printf("%s\n", old_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200118 free(old_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200120 } while (cur_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121
Denys Vlasenko1249dbb2009-08-30 19:35:06 +0200122 die_if_ferror(stdin, input_filename);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000124 fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000125}