d5c6f719d3c8644c175a036916fdf6a47f6642f4
[oweals/busybox.git] / coreutils / uniq.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * uniq implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 /* BB_AUDIT SUSv3 compliant */
24 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include "busybox.h"
32 #include "libcoreutils/coreutils.h"
33
34 static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4";
35
36 static FILE *xgetoptfile_sort_uniq_s(char **argv, const char *mode)
37 {
38         const char *n;
39
40         if ((n = *argv) != NULL) {
41                 if ((*n != '-') || n[1]) {
42                         return bb_xfopen(n, mode);
43                 }
44         }
45         return (*mode == 'r') ? stdin : stdout;
46 }
47
48 int uniq_main(int argc, char **argv)
49 {
50         FILE *in, *out;
51         /* Note: Ignore the warning about dups and e0 being used uninitialized.
52          * They will be initialized on the fist pass of the loop (since s0 is NULL). */
53 #warning The dups and e0 warnings are OK, ignore them
54         unsigned long dups, skip_fields, skip_chars, i;
55         const char *s0, *e0, *s1, *e1, *input_filename;
56         int opt;
57         int uniq_flags = 6;             /* -u */
58
59         skip_fields = skip_chars = 0;
60
61         while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
62                 if (opt == 'f') {
63                         skip_fields = bb_xgetularg10(optarg);
64                 } else if (opt == 's') {
65                         skip_chars = bb_xgetularg10(optarg);
66                 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
67                         uniq_flags &= s0[4];
68                         uniq_flags |= s0[7];
69                 } else {
70                         bb_show_usage();
71                 }
72         }
73
74         input_filename = *(argv += optind);
75
76         in = xgetoptfile_sort_uniq_s(argv, "r");
77         if (*argv) {
78                 ++argv;
79         }
80         out = xgetoptfile_sort_uniq_s(argv, "w");
81         if (*argv && argv[1]) {
82                 bb_show_usage();
83         }
84
85         s0 = NULL;
86
87         /* gnu uniq ignores newlines */
88         while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
89                 e1 = s1;
90                 for (i=skip_fields ; i ; i--) {
91                         e1 = bb_skip_whitespace(e1);
92                         while (*e1 && !isspace(*e1)) {
93                                 ++e1;
94                         }
95                 }
96                 for (i = skip_chars ; *e1 && i ; i--) {
97                         ++e1;
98                 }
99                 if (s0) {
100                         if (strcmp(e0, e1) == 0) {
101                                 ++dups;         /* Note: Testing for overflow seems excessive. */
102                                 continue;
103                         }
104                 DO_LAST:
105                         if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) {
106                                 bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1);
107                                 bb_fprintf(out, "%s\n", s0);
108                         }
109                         free((void *)s0);
110                 }
111
112                 s0 = s1;
113                 e0 = e1;
114                 dups = 0;
115         }
116
117         if (s0) {
118                 e1 = NULL;
119                 goto DO_LAST;
120         }
121
122         bb_xferror(in, input_filename);
123
124         bb_fflush_stdout_and_exit(EXIT_SUCCESS);
125 }