[net.micro.6809] uniq subset

jejones@ea.UUCP (11/22/84)

The following is a (simple to do, but worth doing) subset of the Unix
uniq [sic] filter. It's not in shar format, because there's just one
file. The contents of <bool.h> should be easy to reverse engineer...

					James Jones

------------------------------TARE HEAR------------------------------
/*
 * Unique -- a subset of the Unix "uniq" [sic] command
 *
 * usage:	Unique [-udc] [input]
 *
 * semantics:	examine the input (use the input pathname if it is
 *	specified, else standard input) for identical adjacent lines.
 *	Print some such lines, depending on the choice of flags:
 *
 *	-u	print "unique" lines (i.e. ones that don't appear next
 *		to copies of themselves)
 *	-d	print non-"unique" lines
 *	-c	prefix lines printed with a count of the number of
 *		(adjacent) occurrences of the line
 *
 * defaults:	-ud if no flags are specified; standard input if no
 *	input pathname is specified.
 *
 * Note that "unique" lines are really unique only if the file is
 * sorted.
 */

#include <stdio.h>
#include <bool.h>

#define MAXLINE	256

char	Buffer[2][MAXLINE];
int	Count;
bool	Unique,
	Repeated,
	LineSpec,
	PrintCount;

main(argc, argv)
int	argc;
char	*argv[];
{
	int	i;
	char	*ArgScan, *Master, *Slave, *Temp;
	FILE	*Input;
	bool	Done;

	LineSpec = FALSE;
	Unique = FALSE;
	Repeated = FALSE;
	PrintCount = FALSE;

	for (i = 1; i < argc; i++) {
		if (*argv[i] != '-')
			break;
		for (ArgScan = argv[i] + 1; *ArgScan; ArgScan++) {
			switch (*ArgScan) {
			case 'u':
				Unique = TRUE;
				LineSpec = TRUE;
				break;
			case 'd':
				Repeated = TRUE;
				LineSpec = TRUE;
				break;
			case 'c':
				PrintCount = TRUE;
				break;
			default:
				fprintf(stderr, "Unique: unknown option %c\n",
					*ArgScan);
				exit(1);
			}
		}
	}

	if (!LineSpec)
		Unique = Repeated = TRUE;

	if (i < argc - 1) {
		fputs("usage: Unique [-udc] [input]\n", stderr);
		exit(1);
	} else if (i == argc - 1) {
		if ((Input = fopen(argv[i], "r")) == NULL) {
			fprintf(stderr, "Unique: can't open input file\n",
				argv[i]);
			exit(1);
		}
	} else
		Input = stdin;

	Master = Buffer[0];
	Slave = Buffer[1];
	Done = fgets(Master, MAXLINE, Input) == NULL;

	while (!Done) {
		Count = 1;
		for (;;) {
			if (Done = fgets(Slave, MAXLINE, Input) == NULL)
				break;
			if (strcmp(Master, Slave) != 0)
				break;
			Count++;
		}
		if ((Count == 1) ? Unique : Repeated) {
			if (PrintCount)
				printf("%4d ", Count);
			fputs(Master, stdout);
		}
		Temp = Master;
		Master = Slave;
		Slave = Temp;
	}

	fclose(Input);

}