uniq: add -i option to ignore case
[oweals/busybox.git] / coreutils / uniq.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * uniq implementation for busybox
4  *
5  * Copyright (C) 2005  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 //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
18
19 /* BB_AUDIT SUSv3 compliant */
20 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
21
22 //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"
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"
29 //usage:     "\n        -f N    Skip first N fields"
30 //usage:     "\n        -s N    Skip first N chars (after any skipped fields)"
31 //usage:     "\n        -w N    Compare N characters in line"
32 //usage:
33 //usage:#define uniq_example_usage
34 //usage:       "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
35 //usage:       "a\n"
36 //usage:       "b\n"
37 //usage:       "c\n"
38
39 #include "libbb.h"
40
41 int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int uniq_main(int argc UNUSED_PARAM, char **argv)
43 {
44         const char *input_filename;
45         unsigned skip_fields, skip_chars, max_chars;
46         unsigned opt;
47         char *cur_line;
48         const char *cur_compare;
49
50         enum {
51                 OPT_c = 0x1,
52                 OPT_d = 0x2, /* print only dups */
53                 OPT_u = 0x4, /* print only uniq */
54                 OPT_f = 0x8,
55                 OPT_s = 0x10,
56                 OPT_w = 0x20,
57                 OPT_i = 0x40,
58         };
59
60         skip_fields = skip_chars = 0;
61         max_chars = INT_MAX;
62
63         opt = getopt32(argv, "cduf:+s:+w:+i", &skip_fields, &skip_chars, &max_chars);
64         argv += optind;
65
66         input_filename = argv[0];
67         if (input_filename) {
68                 const char *output;
69
70                 if (input_filename[0] != '-' || input_filename[1]) {
71                         close(STDIN_FILENO); /* == 0 */
72                         xopen(input_filename, O_RDONLY); /* fd will be 0 */
73                 }
74                 output = argv[1];
75                 if (output) {
76                         if (argv[2])
77                                 bb_show_usage();
78                         if (output[0] != '-' || output[1]) {
79                                 // Won't work with "uniq - FILE" and closed stdin:
80                                 //close(STDOUT_FILENO);
81                                 //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
82                                 xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
83                         }
84                 }
85         }
86
87         cur_compare = cur_line = NULL; /* prime the pump */
88
89         do {
90                 unsigned i;
91                 unsigned long dups;
92                 char *old_line;
93                 const char *old_compare;
94
95                 old_line = cur_line;
96                 old_compare = cur_compare;
97                 dups = 0;
98
99                 /* gnu uniq ignores newlines */
100                 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
101                         cur_compare = cur_line;
102                         for (i = skip_fields; i; i--) {
103                                 cur_compare = skip_whitespace(cur_compare);
104                                 cur_compare = skip_non_whitespace(cur_compare);
105                         }
106                         for (i = skip_chars; *cur_compare && i; i--) {
107                                 ++cur_compare;
108                         }
109
110                         if (!old_line)
111                                 break;
112                         if ((opt & OPT_i)
113                                 ? strncasecmp(old_compare, cur_compare, max_chars)
114                                 : strncmp(old_compare, cur_compare, max_chars)
115                         ) {
116                                 break;
117                         }
118
119                         free(cur_line);
120                         ++dups;  /* testing for overflow seems excessive */
121                 }
122
123                 if (old_line) {
124                         if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
125                                 if (opt & OPT_c) {
126                                         /* %7lu matches GNU coreutils 6.9 */
127                                         printf("%7lu ", dups + 1);
128                                 }
129                                 puts(old_line);
130                         }
131                         free(old_line);
132                 }
133         } while (cur_line);
134
135         die_if_ferror(stdin, input_filename);
136
137         fflush_stdout_and_exit(EXIT_SUCCESS);
138 }