blob: 0cc0c83b3faa12ba3e776530cd1bc83fa3c365b6 [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
Rob Landleyea224be2006-06-18 20:20:07 +000013#include "busybox.h"
John Beppuabb47722000-01-06 00:48:21 +000014
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000015static const char uniq_opts[] = "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
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000021 if ((n = *argv) != NULL) {
22 if ((*n != '-') || n[1]) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000023 return xfopen(n, "r\0w" + read0write2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000024 }
25 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000026 return (read0write2) ? stdout : stdin;
Rob Landley14efdc52005-09-07 04:18:36 +000027}
28
Denis Vlasenko06af2162007-02-03 17:28:39 +000029int uniq_main(int argc, char **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000030int uniq_main(int argc, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000031{
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 FILE *in, *out;
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000033 unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000034 const char *s0, *e0, *s1, *e1, *input_filename;
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 int opt;
John Beppuabb47722000-01-06 00:48:21 +000036
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000037 uniq_flags = skip_fields = skip_chars = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000038
39 while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000040 if ((opt == 'f') || (opt == 's')) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000041 unsigned long t = xatoul(optarg);
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000042 if (opt == 'f') {
43 skip_fields = t;
44 } else {
45 skip_chars = t;
46 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000047 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000048 uniq_flags |= s0[4];
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000049 } else {
50 bb_show_usage();
51 }
Eric Andersen5b5db382000-12-09 16:37:53 +000052 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 input_filename = *(argv += optind);
55
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000056 in = xgetoptfile_uniq_s(argv, 0);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000057 if (*argv) {
58 ++argv;
59 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000060 out = xgetoptfile_uniq_s(argv, 2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000061 if (*argv && argv[1]) {
62 bb_show_usage();
63 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000064
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000065 s1 = e1 = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000066
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000067 do {
68 s0 = s1;
69 e0 = e1;
70 dups = 0;
71
72 /* gnu uniq ignores newlines */
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000073 while ((s1 = xmalloc_getline(in)) != NULL) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000074 e1 = s1;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000075 for (i = skip_fields; i; i--) {
Rob Landleyea224be2006-06-18 20:20:07 +000076 e1 = skip_whitespace(e1);
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000077 while (*e1 && !isspace(*e1)) {
78 ++e1;
79 }
80 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000081 for (i = skip_chars; *e1 && i; i--) {
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000082 ++e1;
83 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000084
85 if (!s0 || strcmp(e0, e1)) {
86 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000088
89 ++dups; /* Note: Testing for overflow seems excessive. */
90 }
91
92 if (s0) {
93 if (!(uniq_flags & (2 << !!dups))) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000094 fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
95 fprintf(out, "%s\n", s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000097 free((void *)s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000099 } while (s1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000101 die_if_ferror(in, input_filename);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000103 fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000104}