uniq: code shrink
[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 the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
12
13 #include "libbb.h"
14
15 static void xgetoptfile_uniq_s(const char *n, int fd)
16 {
17         if (n == NULL)
18                 return;
19         if ((n[0] == '-') && !n[1])
20                 return;
21         /* close(fd); - optimization */
22         xmove_fd(
23                 xopen3(n,
24                         (fd == STDIN_FILENO) ? O_RDONLY : (O_WRONLY | O_CREAT | O_TRUNC),
25                         0666),
26                 fd);
27 }
28
29 int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int uniq_main(int argc UNUSED_PARAM, char **argv)
31 {
32         const char *input_filename;
33         unsigned skip_fields, skip_chars, max_chars;
34         unsigned opt;
35         char *cur_line;
36         const char *cur_compare;
37
38         enum {
39                 OPT_c = 0x1,
40                 OPT_d = 0x2, /* print only dups */
41                 OPT_u = 0x4, /* print only uniq */
42                 OPT_f = 0x8,
43                 OPT_s = 0x10,
44                 OPT_w = 0x20,
45         };
46
47         skip_fields = skip_chars = 0;
48         max_chars = INT_MAX;
49
50         opt_complementary = "f+:s+:w+";
51         opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
52         argv += optind;
53
54         input_filename = *argv;
55
56         xgetoptfile_uniq_s(*argv, STDIN_FILENO);
57         if (*argv) {
58                 ++argv;
59         }
60         xgetoptfile_uniq_s(*argv, STDOUT_FILENO);
61         if (*argv && argv[1]) {
62                 bb_show_usage();
63         }
64
65         cur_compare = cur_line = NULL; /* prime the pump */
66
67         do {
68                 unsigned i;
69                 unsigned long dups;
70                 char *old_line;
71                 const char *old_compare;
72
73                 old_line = cur_line;
74                 old_compare = cur_compare;
75                 dups = 0;
76
77                 /* gnu uniq ignores newlines */
78                 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
79                         cur_compare = cur_line;
80                         for (i = skip_fields; i; i--) {
81                                 cur_compare = skip_whitespace(cur_compare);
82                                 cur_compare = skip_non_whitespace(cur_compare);
83                         }
84                         for (i = skip_chars; *cur_compare && i; i--) {
85                                 ++cur_compare;
86                         }
87
88                         if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
89                                 break;
90                         }
91
92                         free(cur_line);
93                         ++dups;  /* testing for overflow seems excessive */
94                 }
95
96                 if (old_line) {
97                         if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
98                                 printf("\0%lu " + (opt & 1), dups + 1); /* 1 == OPT_c */
99                                 printf("%s\n", old_line);
100                         }
101                         free(old_line);
102                 }
103         } while (cur_line);
104
105         die_if_ferror(stdin, input_filename);
106
107         fflush_stdout_and_exit(EXIT_SUCCESS);
108 }