blob: cfe6cca5e2d23026b6766c824e8b24d37742cc70 [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 *
5 *
Erik Andersen61677fe2000-04-13 01:18:56 +00006 * Copyright (C) 1999,2000 by Lineo, inc.
John Beppuabb47722000-01-06 00:48:21 +00007 * Written by John Beppu <beppu@lineo.com>
Matt Kraaie0bcce02000-09-27 02:29:39 +00008 * Rewritten by Matt Kraai <kraai@alumni.carnegiemellon.edu>
John Beppuabb47722000-01-06 00:48:21 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
Eric Andersen3570a342000-09-25 21:45:58 +000026#include "busybox.h"
John Beppuabb47722000-01-06 00:48:21 +000027#include <stdio.h>
John Beppu96f1f332000-01-06 23:49:21 +000028#include <string.h>
29#include <errno.h>
John Beppuabb47722000-01-06 00:48:21 +000030
Erik Andersene49d5ec2000-02-08 19:58:47 +000031int uniq_main(int argc, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000032{
Matt Kraaie0bcce02000-09-27 02:29:39 +000033 FILE *in = stdin, *out = stdout;
34 char *lastline = NULL, *input;
John Beppuabb47722000-01-06 00:48:21 +000035
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 /* parse argv[] */
Matt Kraaie0bcce02000-09-27 02:29:39 +000037 if ((argc > 1 && **(argv + 1) == '-') || argc > 3)
38 usage(uniq_usage);
39
40 if (argv[1] != NULL) {
41 in = xfopen(argv[1], "r");
42 if (argv[2] != NULL)
43 out = xfopen(argv[2], "w");
44 }
45
46 while ((input = get_line_from_file(in)) != NULL) {
47 if (lastline == NULL || strcmp(input, lastline) != 0) {
48 fputs(input, out);
49 free(lastline);
50 lastline = input;
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
John Beppuabb47722000-01-06 00:48:21 +000052 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000053 free(lastline);
John Beppuabb47722000-01-06 00:48:21 +000054
Matt Kraaie0bcce02000-09-27 02:29:39 +000055 return EXIT_SUCCESS;
John Beppuabb47722000-01-06 00:48:21 +000056}