blob: 32327c6ce335173dceec1b0d0ae992d1a6edbd0f [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 *
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
John Beppuabb47722000-01-06 00:48:21 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
John Beppuabb47722000-01-06 00:48:21 +000014
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000015static const char uniq_opts[] ALIGN1 = "cdu" "f:s:" "cdu\0\1\2\4";
Rob Landley14efdc52005-09-07 04:18:36 +000016
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000017static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
Rob Landley14efdc52005-09-07 04:18:36 +000018{
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000019 const char *n;
Rob Landley14efdc52005-09-07 04:18:36 +000020
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000021 n = *argv;
22 if (n != NULL) {
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000023 if ((*n != '-') || n[1]) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000024 return xfopen(n, "r\0w" + read0write2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000025 }
26 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000027 return (read0write2) ? stdout : stdin;
Rob Landley14efdc52005-09-07 04:18:36 +000028}
29
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000031int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000032{
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 FILE *in, *out;
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000034 unsigned long dups, skip_fields, skip_chars, i;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000035 const char *s0, *e0, *s1, *e1, *input_filename;
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000036 unsigned opt;
John Beppuabb47722000-01-06 00:48:21 +000037
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000038 enum {
39 OPT_c = 0x1,
40 OPT_d = 0x2,
41 OPT_u = 0x4,
42 OPT_f = 0x8,
43 OPT_s = 0x10,
44 };
Manuel Novoa III cad53642003-03-19 09:13:01 +000045
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000046 skip_fields = skip_chars = 0;
Matt Kraaie0bcce02000-09-27 02:29:39 +000047
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000048 opt = getopt32(argv, "cduf:s:", &s0, &s1);
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000049 if (opt & OPT_f)
50 skip_fields = xatoul(s0);
51 if (opt & OPT_s)
52 skip_chars = xatoul(s1);
53 argv += optind;
54
55 input_filename = *argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +000056
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000057 in = xgetoptfile_uniq_s(argv, 0);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000058 if (*argv) {
59 ++argv;
60 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000061 out = xgetoptfile_uniq_s(argv, 2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000062 if (*argv && argv[1]) {
63 bb_show_usage();
64 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000065
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000066 s1 = e1 = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000068 do {
69 s0 = s1;
70 e0 = e1;
71 dups = 0;
72
73 /* gnu uniq ignores newlines */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000074 while ((s1 = xmalloc_fgetline(in)) != NULL) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000075 e1 = s1;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000076 for (i = skip_fields; i; i--) {
Rob Landleyea224be2006-06-18 20:20:07 +000077 e1 = skip_whitespace(e1);
Bernhard Reutner-Fischerde17ece2007-04-10 09:38:35 +000078 e1 = skip_non_whitespace(e1);
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000079 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000080 for (i = skip_chars; *e1 && i; i--) {
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000081 ++e1;
82 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000083
84 if (!s0 || strcmp(e0, e1)) {
85 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000087
88 ++dups; /* Note: Testing for overflow seems excessive. */
89 }
90
91 if (s0) {
Denis Vlasenko4caa09a2007-03-31 10:19:11 +000092 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */
93 fprintf(out, "\0%d " + (opt & 1), dups + 1);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000094 fprintf(out, "%s\n", s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000096 free((void *)s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000098 } while (s1);
Manuel Novoa III cad53642003-03-19 09:13:01 +000099
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000100 die_if_ferror(in, input_filename);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000102 fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000103}