* archival/bunzip2.c (bunzip2_main): Do not remove files if writing to standard
[oweals/busybox.git] / coreutils / uniq.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini uniq implementation for busybox
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  * Rewritten by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <getopt.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include "busybox.h"
31
32 static int print_count;
33 static int print_uniq = 1;
34 static int print_duplicates = 1;
35
36 static void print_line(char *line, int count, FILE *fp)
37 {
38         if ((print_duplicates && count > 1) || (print_uniq && count == 1)) {
39                 if (print_count)
40                         fprintf(fp, "%7d\t%s", count, line);
41                 else
42                         fputs(line, fp);
43         }
44 }
45
46 int uniq_main(int argc, char **argv)
47 {
48         FILE *in = stdin, *out = stdout;
49         char *lastline = NULL, *input;
50         int opt, count = 0;
51
52         /* parse argv[] */
53         while ((opt = getopt(argc, argv, "cdu")) > 0) {
54                 switch (opt) {
55                         case 'c':
56                                 print_count = 1;
57                                 break;
58                         case 'd':
59                                 print_duplicates = 1;
60                                 print_uniq = 0;
61                                 break;
62                         case 'u':
63                                 print_duplicates = 0;
64                                 print_uniq = 1;
65                                 break;
66                 }
67         }
68
69         if (argv[optind] != NULL) {
70                 in = xfopen(argv[optind], "r");
71                 if (argv[optind+1] != NULL)
72                         out = xfopen(argv[optind+1], "w");
73         }
74
75         while ((input = get_line_from_file(in)) != NULL) {
76                 if (lastline == NULL || strcmp(input, lastline) != 0) {
77                         print_line(lastline, count, out);
78                         free(lastline);
79                         lastline = input;
80                         count = 0;
81                 }
82                 count++;
83         }
84         print_line(lastline, count, out);
85         free(lastline);
86
87         return EXIT_SUCCESS;
88 }