blob: 93ae169f4a85b46c2806ea1ed95ffe3151a79fe7 [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/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * uniq implementation for busybox
John Beppuabb47722000-01-06 00:48:21 +00004 *
Manuel Novoa III 84b93f72005-09-15 08:06:42 +00005 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
John Beppuabb47722000-01-06 00:48:21 +00006 *
Manuel Novoa III 415f6c92005-09-08 06:02:49 +00007 * 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
John Beppuabb47722000-01-06 00:48:21 +000020 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
25
John Beppuabb47722000-01-06 00:48:21 +000026#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000028#include <string.h>
29#include <ctype.h>
30#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000032#include "libcoreutils/coreutils.h"
John Beppuabb47722000-01-06 00:48:21 +000033
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000034static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4";
Rob Landley14efdc52005-09-07 04:18:36 +000035
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000036static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
Rob Landley14efdc52005-09-07 04:18:36 +000037{
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000038 const char *n;
Rob Landley14efdc52005-09-07 04:18:36 +000039
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000040 if ((n = *argv) != NULL) {
41 if ((*n != '-') || n[1]) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000042 return bb_xfopen(n, "r\0w" + read0write2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000043 }
44 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000045 return (read0write2) ? stdout : stdin;
Rob Landley14efdc52005-09-07 04:18:36 +000046}
47
Erik Andersene49d5ec2000-02-08 19:58:47 +000048int uniq_main(int argc, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000049{
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 FILE *in, *out;
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000051 unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000052 const char *s0, *e0, *s1, *e1, *input_filename;
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 int opt;
John Beppuabb47722000-01-06 00:48:21 +000054
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000055 uniq_flags = skip_fields = skip_chars = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000056
57 while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000058 if ((opt == 'f') || (opt == 's')) {
59 int t = bb_xgetularg10(optarg);
60 if (opt == 'f') {
61 skip_fields = t;
62 } else {
63 skip_chars = t;
64 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000065 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000066 uniq_flags |= s0[4];
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000067 } else {
68 bb_show_usage();
69 }
Eric Andersen5b5db382000-12-09 16:37:53 +000070 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000071
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 input_filename = *(argv += optind);
73
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000074 in = xgetoptfile_uniq_s(argv, 0);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000075 if (*argv) {
76 ++argv;
77 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000078 out = xgetoptfile_uniq_s(argv, 2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000079 if (*argv && argv[1]) {
80 bb_show_usage();
81 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000082
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000083 s1 = e1 = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000084
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000085 do {
86 s0 = s1;
87 e0 = e1;
88 dups = 0;
89
90 /* gnu uniq ignores newlines */
91 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
92 e1 = s1;
93 for (i=skip_fields ; i ; i--) {
94 e1 = bb_skip_whitespace(e1);
95 while (*e1 && !isspace(*e1)) {
96 ++e1;
97 }
98 }
99 for (i = skip_chars ; *e1 && i ; i--) {
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000100 ++e1;
101 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000102
103 if (!s0 || strcmp(e0, e1)) {
104 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000106
107 ++dups; /* Note: Testing for overflow seems excessive. */
108 }
109
110 if (s0) {
111 if (!(uniq_flags & (2 << !!dups))) {
112 bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000113 bb_fprintf(out, "%s\n", s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000115 free((void *)s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000117 } while (s1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118
119 bb_xferror(in, input_filename);
120
121 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000122}