ls: fix help text: -w N is optional
[platform/upstream/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:     "\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
31 #include "libbb.h"
32
33 int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 int uniq_main(int argc UNUSED_PARAM, char **argv)
35 {
36         const char *input_filename;
37         unsigned skip_fields, skip_chars, max_chars;
38         unsigned opt;
39         char *cur_line;
40         const char *cur_compare;
41
42         enum {
43                 OPT_c = 0x1,
44                 OPT_d = 0x2, /* print only dups */
45                 OPT_u = 0x4, /* print only uniq */
46                 OPT_f = 0x8,
47                 OPT_s = 0x10,
48                 OPT_w = 0x20,
49         };
50
51         skip_fields = skip_chars = 0;
52         max_chars = INT_MAX;
53
54         opt_complementary = "f+:s+:w+";
55         opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
56         argv += optind;
57
58         input_filename = argv[0];
59         if (input_filename) {
60                 const char *output;
61
62                 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);
73                                 //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
74                                 xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
75                         }
76                 }
77         }
78
79         cur_compare = cur_line = NULL; /* prime the pump */
80
81         do {
82                 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;
89                 dups = 0;
90
91                 /* gnu uniq ignores newlines */
92                 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
93                         cur_compare = cur_line;
94                         for (i = skip_fields; i; i--) {
95                                 cur_compare = skip_whitespace(cur_compare);
96                                 cur_compare = skip_non_whitespace(cur_compare);
97                         }
98                         for (i = skip_chars; *cur_compare && i; i--) {
99                                 ++cur_compare;
100                         }
101
102                         if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
103                                 break;
104                         }
105
106                         free(cur_line);
107                         ++dups;  /* testing for overflow seems excessive */
108                 }
109
110                 if (old_line) {
111                         if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
112                                 if (opt & OPT_c) {
113                                         /* %7lu matches GNU coreutils 6.9 */
114                                         printf("%7lu ", dups + 1);
115                                 }
116                                 printf("%s\n", old_line);
117                         }
118                         free(old_line);
119                 }
120         } while (cur_line);
121
122         die_if_ferror(stdin, input_filename);
123
124         fflush_stdout_and_exit(EXIT_SUCCESS);
125 }