* uniq.c: remove unneeded include and use short boilerplate.
[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 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 <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include "busybox.h"
19
20 static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4";
21
22 static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
23 {
24         const char *n;
25
26         if ((n = *argv) != NULL) {
27                 if ((*n != '-') || n[1]) {
28                         return bb_xfopen(n, "r\0w" + read0write2);
29                 }
30         }
31         return (read0write2) ? stdout : stdin;
32 }
33
34 int uniq_main(int argc, char **argv)
35 {
36         FILE *in, *out;
37         unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
38         const char *s0, *e0, *s1, *e1, *input_filename;
39         int opt;
40
41         uniq_flags = skip_fields = skip_chars = 0;
42
43         while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
44                 if ((opt == 'f') || (opt == 's')) {
45                         int t = bb_xgetularg10(optarg);
46                         if (opt == 'f') {
47                                 skip_fields = t;
48                         } else {
49                                 skip_chars = t;
50                         }
51                 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
52                         uniq_flags |= s0[4];
53                 } else {
54                         bb_show_usage();
55                 }
56         }
57
58         input_filename = *(argv += optind);
59
60         in = xgetoptfile_uniq_s(argv, 0);
61         if (*argv) {
62                 ++argv;
63         }
64         out = xgetoptfile_uniq_s(argv, 2);
65         if (*argv && argv[1]) {
66                 bb_show_usage();
67         }
68
69         s1 = e1 = NULL;                         /* prime the pump */
70
71         do {
72                 s0 = s1;
73                 e0 = e1;
74                 dups = 0;
75
76                 /* gnu uniq ignores newlines */
77                 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
78                         e1 = s1;
79                         for (i=skip_fields ; i ; i--) {
80                                 e1 = bb_skip_whitespace(e1);
81                                 while (*e1 && !isspace(*e1)) {
82                                         ++e1;
83                                 }
84                         }
85                         for (i = skip_chars ; *e1 && i ; i--) {
86                                 ++e1;
87                         }
88
89                         if (!s0 || strcmp(e0, e1)) {
90                                 break;
91                         }
92
93                         ++dups;          /* Note: Testing for overflow seems excessive. */
94                 }
95
96                 if (s0) {
97                         if (!(uniq_flags & (2 << !!dups))) {
98                                 bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
99                                 bb_fprintf(out, "%s\n", s0);
100                         }
101                         free((void *)s0);
102                 }
103         } while (s1);
104
105         bb_xferror(in, input_filename);
106
107         bb_fflush_stdout_and_exit(EXIT_SUCCESS);
108 }