blob: 2b62ad3aed93349a4842e05c37b644bbb48d944a [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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config UNIQ
10//config: bool "uniq"
11//config: default y
12//config: help
13//config: uniq is used to remove duplicate lines from a sorted file.
14
15//applet:IF_UNIQ(APPLET(uniq, BB_DIR_USR_BIN, BB_SUID_DROP))
16
17//kbuild:lib-$(CONFIG_UNIQ) += uniq.o
John Beppuabb47722000-01-06 00:48:21 +000018
Manuel Novoa III cad53642003-03-19 09:13:01 +000019/* BB_AUDIT SUSv3 compliant */
20/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
21
Pere Orga34425382011-03-31 14:43:25 +020022//usage:#define uniq_trivial_usage
23//usage: "[-cdu][-f,s,w N] [INPUT [OUTPUT]]"
24//usage:#define uniq_full_usage "\n\n"
25//usage: "Discard duplicate lines\n"
Pere Orga34425382011-03-31 14:43:25 +020026//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 Bruchon19578b42017-03-30 12:24:56 -040029//usage: "\n -i Ignore case"
Pere Orga34425382011-03-31 14:43:25 +020030//usage: "\n -f N Skip first N fields"
31//usage: "\n -s N Skip first N chars (after any skipped fields)"
32//usage: "\n -w N Compare N characters in line"
33//usage:
34//usage:#define uniq_example_usage
35//usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
36//usage: "a\n"
37//usage: "b\n"
38//usage: "c\n"
39
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
John Beppuabb47722000-01-06 00:48:21 +000041
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000043int uniq_main(int argc UNUSED_PARAM, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000044{
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020045 const char *input_filename;
Denis Vlasenko96b99b82008-05-03 07:21:27 +000046 unsigned skip_fields, skip_chars, max_chars;
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000047 unsigned opt;
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020048 char *cur_line;
49 const char *cur_compare;
John Beppuabb47722000-01-06 00:48:21 +000050
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000051 enum {
52 OPT_c = 0x1,
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020053 OPT_d = 0x2, /* print only dups */
54 OPT_u = 0x4, /* print only uniq */
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000055 OPT_f = 0x8,
56 OPT_s = 0x10,
Denis Vlasenko96b99b82008-05-03 07:21:27 +000057 OPT_w = 0x20,
Jody Bruchon8808f7f2017-03-30 18:15:54 +020058 OPT_i = 0x40,
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000059 };
Manuel Novoa III cad53642003-03-19 09:13:01 +000060
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000061 skip_fields = skip_chars = 0;
Denis Vlasenko1a6adbd2009-03-09 16:43:28 +000062 max_chars = INT_MAX;
Matt Kraaie0bcce02000-09-27 02:29:39 +000063
Jody Bruchon8808f7f2017-03-30 18:15:54 +020064 opt = getopt32(argv, "cduf:+s:+w:+i", &skip_fields, &skip_chars, &max_chars);
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000065 argv += optind;
66
Denys Vlasenko44f174e2009-08-31 05:15:21 +020067 input_filename = argv[0];
68 if (input_filename) {
69 const char *output;
Manuel Novoa III cad53642003-03-19 09:13:01 +000070
Denys Vlasenko44f174e2009-08-31 05:15:21 +020071 if (input_filename[0] != '-' || input_filename[1]) {
72 close(STDIN_FILENO); /* == 0 */
73 xopen(input_filename, O_RDONLY); /* fd will be 0 */
74 }
75 output = argv[1];
76 if (output) {
77 if (argv[2])
78 bb_show_usage();
79 if (output[0] != '-' || output[1]) {
80 // Won't work with "uniq - FILE" and closed stdin:
81 //close(STDOUT_FILENO);
Denys Vlasenkoc05387d2010-10-18 02:38:27 +020082 //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
83 xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
Denys Vlasenko44f174e2009-08-31 05:15:21 +020084 }
85 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000086 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000087
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020088 cur_compare = cur_line = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000089
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000090 do {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +020091 unsigned i;
92 unsigned long dups;
93 char *old_line;
94 const char *old_compare;
95
96 old_line = cur_line;
97 old_compare = cur_compare;
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000098 dups = 0;
99
100 /* gnu uniq ignores newlines */
Denys Vlasenko1249dbb2009-08-30 19:35:06 +0200101 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200102 cur_compare = cur_line;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000103 for (i = skip_fields; i; i--) {
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200104 cur_compare = skip_whitespace(cur_compare);
105 cur_compare = skip_non_whitespace(cur_compare);
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000106 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200107 for (i = skip_chars; *cur_compare && i; i--) {
108 ++cur_compare;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000109 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000110
Jody Bruchon8808f7f2017-03-30 18:15:54 +0200111 if (!old_line)
112 break;
113 if ((opt & OPT_i)
114 ? strncasecmp(old_compare, cur_compare, max_chars)
115 : strncmp(old_compare, cur_compare, max_chars)
116 ) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000117 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000119
Denys Vlasenkoa9c9bf52009-08-30 19:34:09 +0200120 free(cur_line);
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200121 ++dups; /* testing for overflow seems excessive */
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000122 }
123
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200124 if (old_line) {
125 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
Denys Vlasenko44f174e2009-08-31 05:15:21 +0200126 if (opt & OPT_c) {
127 /* %7lu matches GNU coreutils 6.9 */
128 printf("%7lu ", dups + 1);
129 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200130 puts(old_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200132 free(old_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 }
Denys Vlasenko0aec9ff2009-08-30 19:32:45 +0200134 } while (cur_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135
Denys Vlasenko1249dbb2009-08-30 19:35:06 +0200136 die_if_ferror(stdin, input_filename);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000138 fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000139}