blob: cb63c42772ed3945cf9636a12c68860a5b7ada1d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppuabb47722000-01-06 00:48:21 +00002/*
Erik Andersen0b874ed2000-01-06 01:14:56 +00003 * Mini uniq implementation for busybox
John Beppuabb47722000-01-06 00:48:21 +00004 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
Matt Kraaie0bcce02000-09-27 02:29:39 +00007 * Rewritten by Matt Kraai <kraai@alumni.carnegiemellon.edu>
John Beppuabb47722000-01-06 00:48:21 +00008 *
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
John Beppuabb47722000-01-06 00:48:21 +000025#include <stdio.h>
John Beppu96f1f332000-01-06 23:49:21 +000026#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <getopt.h>
John Beppu96f1f332000-01-06 23:49:21 +000028#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
John Beppuabb47722000-01-06 00:48:21 +000031
Eric Andersen5b5db382000-12-09 16:37:53 +000032static int print_count;
33static int print_uniq = 1;
34static int print_duplicates = 1;
35
36static 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
Erik Andersene49d5ec2000-02-08 19:58:47 +000046int uniq_main(int argc, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000047{
Matt Kraaie0bcce02000-09-27 02:29:39 +000048 FILE *in = stdin, *out = stdout;
49 char *lastline = NULL, *input;
Eric Andersen5b5db382000-12-09 16:37:53 +000050 int opt, count = 0;
John Beppuabb47722000-01-06 00:48:21 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 /* parse argv[] */
Eric Andersen5b5db382000-12-09 16:37:53 +000053 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 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000068
Eric Andersen5b5db382000-12-09 16:37:53 +000069 if (argv[optind] != NULL) {
70 in = xfopen(argv[optind], "r");
71 if (argv[optind+1] != NULL)
72 out = xfopen(argv[optind+1], "w");
Matt Kraaie0bcce02000-09-27 02:29:39 +000073 }
74
75 while ((input = get_line_from_file(in)) != NULL) {
76 if (lastline == NULL || strcmp(input, lastline) != 0) {
Eric Andersen5b5db382000-12-09 16:37:53 +000077 print_line(lastline, count, out);
Matt Kraaie0bcce02000-09-27 02:29:39 +000078 free(lastline);
79 lastline = input;
Eric Andersen5b5db382000-12-09 16:37:53 +000080 count = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 }
Eric Andersen5b5db382000-12-09 16:37:53 +000082 count++;
John Beppuabb47722000-01-06 00:48:21 +000083 }
Eric Andersen5b5db382000-12-09 16:37:53 +000084 print_line(lastline, count, out);
Matt Kraaie0bcce02000-09-27 02:29:39 +000085 free(lastline);
John Beppuabb47722000-01-06 00:48:21 +000086
Matt Kraaie0bcce02000-09-27 02:29:39 +000087 return EXIT_SUCCESS;
John Beppuabb47722000-01-06 00:48:21 +000088}