blob: be3d75c3a4095d7003c90b8376615d315349d5d9 [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
John Beppuabb47722000-01-06 00:48:21 +000013#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000014#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#include <string.h>
16#include <ctype.h>
17#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000018#include "busybox.h"
John Beppuabb47722000-01-06 00:48:21 +000019
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000020static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4";
Rob Landley14efdc52005-09-07 04:18:36 +000021
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000022static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
Rob Landley14efdc52005-09-07 04:18:36 +000023{
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000024 const char *n;
Rob Landley14efdc52005-09-07 04:18:36 +000025
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000026 if ((n = *argv) != NULL) {
27 if ((*n != '-') || n[1]) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000028 return bb_xfopen(n, "r\0w" + read0write2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000029 }
30 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000031 return (read0write2) ? stdout : stdin;
Rob Landley14efdc52005-09-07 04:18:36 +000032}
33
Erik Andersene49d5ec2000-02-08 19:58:47 +000034int uniq_main(int argc, char **argv)
John Beppuabb47722000-01-06 00:48:21 +000035{
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 FILE *in, *out;
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000037 unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000038 const char *s0, *e0, *s1, *e1, *input_filename;
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 int opt;
John Beppuabb47722000-01-06 00:48:21 +000040
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000041 uniq_flags = skip_fields = skip_chars = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042
43 while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000044 if ((opt == 'f') || (opt == 's')) {
45 int t = bb_xgetularg10(optarg);
46 if (opt == 'f') {
47 skip_fields = t;
48 } else {
49 skip_chars = t;
50 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000051 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000052 uniq_flags |= s0[4];
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000053 } else {
54 bb_show_usage();
55 }
Eric Andersen5b5db382000-12-09 16:37:53 +000056 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000057
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 input_filename = *(argv += optind);
59
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000060 in = xgetoptfile_uniq_s(argv, 0);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000061 if (*argv) {
62 ++argv;
63 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000064 out = xgetoptfile_uniq_s(argv, 2);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000065 if (*argv && argv[1]) {
66 bb_show_usage();
67 }
Matt Kraaie0bcce02000-09-27 02:29:39 +000068
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000069 s1 = e1 = NULL; /* prime the pump */
Manuel Novoa III cad53642003-03-19 09:13:01 +000070
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000071 do {
72 s0 = s1;
73 e0 = e1;
74 dups = 0;
75
76 /* gnu uniq ignores newlines */
77 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
78 e1 = s1;
79 for (i=skip_fields ; i ; i--) {
80 e1 = bb_skip_whitespace(e1);
81 while (*e1 && !isspace(*e1)) {
82 ++e1;
83 }
84 }
85 for (i = skip_chars ; *e1 && i ; i--) {
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000086 ++e1;
87 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000088
89 if (!s0 || strcmp(e0, e1)) {
90 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +000092
93 ++dups; /* Note: Testing for overflow seems excessive. */
94 }
95
96 if (s0) {
97 if (!(uniq_flags & (2 << !!dups))) {
98 bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
Manuel Novoa III 415f6c92005-09-08 06:02:49 +000099 bb_fprintf(out, "%s\n", s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 }
Manuel Novoa III 415f6c92005-09-08 06:02:49 +0000101 free((void *)s0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 }
Manuel Novoa III 84b93f72005-09-15 08:06:42 +0000103 } while (s1);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104
105 bb_xferror(in, input_filename);
106
107 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
John Beppuabb47722000-01-06 00:48:21 +0000108}