2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-01-06 00:48:21 +00:00
|
|
|
/*
|
2003-03-19 09:13:01 +00:00
|
|
|
* uniq implementation for busybox
|
2000-01-06 00:48:21 +00:00
|
|
|
*
|
2003-03-19 09:13:01 +00:00
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
2000-01-06 00:48:21 +00:00
|
|
|
*
|
2005-09-07 04:18:36 +00:00
|
|
|
* Licensed under GPL v2, see file LICENSE in this tarball for details.
|
2000-01-06 00:48:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* BB_AUDIT SUSv3 compliant */
|
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
|
|
|
|
|
2000-01-06 00:48:21 +00:00
|
|
|
#include <stdio.h>
|
2001-01-27 08:24:39 +00:00
|
|
|
#include <stdlib.h>
|
2003-03-19 09:13:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
2001-02-20 06:14:08 +00:00
|
|
|
#include "busybox.h"
|
2003-03-19 09:13:01 +00:00
|
|
|
#include "libcoreutils/coreutils.h"
|
2000-01-06 00:48:21 +00:00
|
|
|
|
2005-09-07 04:18:36 +00:00
|
|
|
/* The extra data is flags to make -d and -u switch each other off */
|
|
|
|
static const char uniq_opts[] = "cudf:s:\0\7\3\5\1\2\4";
|
|
|
|
|
|
|
|
#define SHOW_COUNT 1
|
|
|
|
#define SHOW_UNIQUE 2
|
|
|
|
#define SHOW_DUPLICATE 4
|
|
|
|
|
|
|
|
static FILE *open_arg(char **argv, char *mode)
|
|
|
|
{
|
|
|
|
char *n=*argv;
|
|
|
|
|
|
|
|
return (n && *n != '-' && n[1]) ? bb_xfopen(n, mode) :
|
|
|
|
*mode=='r' ? stdin : stdout;
|
|
|
|
}
|
|
|
|
|
2000-12-09 16:37:53 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
int uniq_main(int argc, char **argv)
|
2000-01-06 00:48:21 +00:00
|
|
|
{
|
2003-03-19 09:13:01 +00:00
|
|
|
FILE *in, *out;
|
|
|
|
unsigned long dups, skip_fields, skip_chars, i;
|
2005-09-06 01:57:11 +00:00
|
|
|
const char *oldline, *oldskipped, *line, *skipped, *input_filename;
|
2003-03-19 09:13:01 +00:00
|
|
|
int opt;
|
2005-09-07 04:18:36 +00:00
|
|
|
int uniq_flags = SHOW_UNIQUE | SHOW_DUPLICATE;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
skip_fields = skip_chars = 0;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
|
2005-09-06 01:57:11 +00:00
|
|
|
if (opt == 'f') skip_fields = bb_xgetularg10(optarg);
|
|
|
|
else if (opt == 's') skip_chars = bb_xgetularg10(optarg);
|
2005-09-07 04:18:36 +00:00
|
|
|
|
|
|
|
/* This bit uses the extra data at the end of uniq_opts to make
|
|
|
|
* -d and -u switch each other off in a very small amount of space */
|
|
|
|
|
2005-09-06 01:57:11 +00:00
|
|
|
else if ((line = strchr(uniq_opts, opt)) != NULL) {
|
2005-09-07 04:18:36 +00:00
|
|
|
uniq_flags &= line[8];
|
|
|
|
uniq_flags |= line[11];
|
2005-09-06 01:57:11 +00:00
|
|
|
} else bb_show_usage();
|
2000-12-09 16:37:53 +00:00
|
|
|
}
|
2000-01-06 00:48:21 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
input_filename = *(argv += optind);
|
|
|
|
|
2005-09-07 04:18:36 +00:00
|
|
|
in = open_arg(argv, "r");
|
2005-09-06 01:57:11 +00:00
|
|
|
if (*argv) ++argv;
|
2005-09-07 04:18:36 +00:00
|
|
|
out = open_arg(argv, "w");
|
2005-09-06 01:57:11 +00:00
|
|
|
if (*argv && argv[1]) bb_show_usage();
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2005-09-07 04:18:36 +00:00
|
|
|
line = skipped = NULL;
|
2000-01-06 23:49:21 +00:00
|
|
|
|
2005-09-07 04:18:36 +00:00
|
|
|
NOT_DUPLICATE:
|
|
|
|
oldline = line;
|
|
|
|
oldskipped = skipped;
|
|
|
|
dups = 0;
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* gnu uniq ignores newlines */
|
2005-09-06 01:57:11 +00:00
|
|
|
while ((line = bb_get_chomped_line_from_file(in)) != NULL) {
|
|
|
|
skipped = line;
|
2003-03-19 09:13:01 +00:00
|
|
|
for (i=skip_fields ; i ; i--) {
|
2005-09-06 01:57:11 +00:00
|
|
|
skipped = bb_skip_whitespace(skipped);
|
|
|
|
while (*skipped && !isspace(*skipped)) ++skipped;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2005-09-06 01:57:11 +00:00
|
|
|
for (i = skip_chars ; *skipped && i ; i--) ++skipped;
|
|
|
|
if (oldline) {
|
2005-09-07 04:18:36 +00:00
|
|
|
if (!strcmp(oldskipped, skipped)) {
|
2003-03-19 09:13:01 +00:00
|
|
|
++dups; /* Note: Testing for overflow seems excessive. */
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-06 01:57:11 +00:00
|
|
|
DO_LAST:
|
2005-09-07 04:18:36 +00:00
|
|
|
if (uniq_flags & (dups ? SHOW_DUPLICATE : SHOW_UNIQUE)) {
|
|
|
|
bb_fprintf(out, "\0%7d " + (uniq_flags & SHOW_COUNT), dups + 1);
|
2005-09-06 01:57:11 +00:00
|
|
|
bb_fprintf(out, "%s\n", oldline);
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2005-09-06 01:57:11 +00:00
|
|
|
free((void *)oldline);
|
2000-09-27 02:29:39 +00:00
|
|
|
}
|
2005-09-07 04:18:36 +00:00
|
|
|
goto NOT_DUPLICATE;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
|
|
|
|
2005-09-07 04:18:36 +00:00
|
|
|
if (oldline) goto DO_LAST;
|
2000-01-06 23:49:21 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_xferror(in, input_filename);
|
|
|
|
|
|
|
|
bb_fflush_stdout_and_exit(EXIT_SUCCESS);
|
2000-01-06 00:48:21 +00:00
|
|
|
}
|