getopt32: add new syntax of 'o:+' and 'o:*' for -o NUM and -o LIST
[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
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
12
13 //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:     "\n        -c      Prefix lines by the number of occurrences"
18 //usage:     "\n        -d      Only print duplicate lines"
19 //usage:     "\n        -u      Only print unique lines"
20 //usage:     "\n        -f N    Skip first N fields"
21 //usage:     "\n        -s N    Skip first N chars (after any skipped fields)"
22 //usage:     "\n        -w N    Compare N characters in line"
23 //usage:
24 //usage:#define uniq_example_usage
25 //usage:       "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
26 //usage:       "a\n"
27 //usage:       "b\n"
28 //usage:       "c\n"
29
30 #include "libbb.h"
31
32 int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33 int uniq_main(int argc UNUSED_PARAM, char **argv)
34 {
35         const char *input_filename;
36         unsigned skip_fields, skip_chars, max_chars;
37         unsigned opt;
38         char *cur_line;
39         const char *cur_compare;
40
41         enum {
42                 OPT_c = 0x1,
43                 OPT_d = 0x2, /* print only dups */
44                 OPT_u = 0x4, /* print only uniq */
45                 OPT_f = 0x8,
46                 OPT_s = 0x10,
47                 OPT_w = 0x20,
48         };
49
50         skip_fields = skip_chars = 0;
51         max_chars = INT_MAX;
52
53         opt = getopt32(argv, "cduf:+s:+w:+", &skip_fields, &skip_chars, &max_chars);
54         argv += optind;
55
56         input_filename = argv[0];
57         if (input_filename) {
58                 const char *output;
59
60                 if (input_filename[0] != '-' || input_filename[1]) {
61                         close(STDIN_FILENO); /* == 0 */
62                         xopen(input_filename, O_RDONLY); /* fd will be 0 */
63                 }
64                 output = argv[1];
65                 if (output) {
66                         if (argv[2])
67                                 bb_show_usage();
68                         if (output[0] != '-' || output[1]) {
69                                 // Won't work with "uniq - FILE" and closed stdin:
70                                 //close(STDOUT_FILENO);
71                                 //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
72                                 xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
73                         }
74                 }
75         }
76
77         cur_compare = cur_line = NULL; /* prime the pump */
78
79         do {
80                 unsigned i;
81                 unsigned long dups;
82                 char *old_line;
83                 const char *old_compare;
84
85                 old_line = cur_line;
86                 old_compare = cur_compare;
87                 dups = 0;
88
89                 /* gnu uniq ignores newlines */
90                 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
91                         cur_compare = cur_line;
92                         for (i = skip_fields; i; i--) {
93                                 cur_compare = skip_whitespace(cur_compare);
94                                 cur_compare = skip_non_whitespace(cur_compare);
95                         }
96                         for (i = skip_chars; *cur_compare && i; i--) {
97                                 ++cur_compare;
98                         }
99
100                         if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
101                                 break;
102                         }
103
104                         free(cur_line);
105                         ++dups;  /* testing for overflow seems excessive */
106                 }
107
108                 if (old_line) {
109                         if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
110                                 if (opt & OPT_c) {
111                                         /* %7lu matches GNU coreutils 6.9 */
112                                         printf("%7lu ", dups + 1);
113                                 }
114                                 puts(old_line);
115                         }
116                         free(old_line);
117                 }
118         } while (cur_line);
119
120         die_if_ferror(stdin, input_filename);
121
122         fflush_stdout_and_exit(EXIT_SUCCESS);
123 }