[BACK]Return to uniq.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / uniq

File: [local] / src / usr.bin / uniq / uniq.c (download)

Revision 1.29, Wed Nov 17 23:09:38 2021 UTC (2 years, 6 months ago) by cheloha
Branch: MAIN
Changes since 1.28: +11 -5 lines

uniq(1): ignore trailing newlines when comparing lines

POSIX.1-2008 tweaked the uniq definition in light of AGI 1003.1-2001
#133.  uniq must now *ignore* the trailing newline when comparing
lines from the input.

In practice this means that if the last line in the input is missing a
trailing newline it isn't necessarily different from the line
preceding it.

So, uniq(1) now stubs the trailing newline before doing any line
comparisons.

For sake of simplicity, this patch introduces a second change: if the
last line in the input is missing a trailing newline and we choose to
print the line, a newline is appended when we print it.

Adopting the newline change aligns our implementation with with
POSIX.1-2008 (which we already claim in the manpage).  Adopting both
changes aligns our behavior with that of FreeBSD and GNU uniq.  For
better or worse, OpenBSD's uniq no longer behaves like NetBSD's uniq
in this corner case.

References:

POSIX.1-2001 uniq:
https://pubs.opengroup.org/onlinepubs/009695399/utilities/uniq.html

Austin Group Interpretation 1003.1-2001 #133:
https://collaboration.opengroup.org/austin/interps/documents/14355/AI-133.txt

POSIX.1-2008 uniq:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/uniq.html

--

Discussed with millert@.  With input from schwarze@.  Positive feedback
from bcallah@.

Thread: https://marc.info/?l=openbsd-tech&m=163581613829524&w=2

ok millert@

/*	$OpenBSD: uniq.c,v 1.29 2021/11/17 23:09:38 cheloha Exp $	*/
/*	$NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $	*/

/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Case Larsen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>

int cflag, dflag, iflag, uflag;
int numchars, numfields, repeats;

FILE	*file(char *, char *);
void	 show(FILE *, char *);
char	*skip(char *);
void	 obsolete(char *[]);
__dead void	usage(void);

int
main(int argc, char *argv[])
{
	char *prevline, *t1, *t2, *thisline;
	FILE *ifp = NULL, *ofp = NULL;
	size_t prevsize, thissize, tmpsize;
	ssize_t len;
	int ch;

	setlocale(LC_CTYPE, "");

	if (pledge("stdio rpath wpath cpath", NULL) == -1)
		err(1, "pledge");

	obsolete(argv);
	while ((ch = getopt(argc, argv, "cdf:is:u")) != -1) {
		const char *errstr;

		switch (ch) {
		case 'c':
			cflag = 1;
			break;
		case 'd':
			dflag = 1;
			break;
		case 'f':
			numfields = (int)strtonum(optarg, 0, INT_MAX,
			    &errstr);
			if (errstr)
				errx(1, "field skip value is %s: %s",
				    errstr, optarg);
			break;
		case 'i':
			iflag = 1;
			break;
		case 's':
			numchars = (int)strtonum(optarg, 0, INT_MAX,
			    &errstr);
			if (errstr)
				errx(1,
				    "character skip value is %s: %s",
				    errstr, optarg);
			break;
		case 'u':
			uflag = 1;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	/* If neither -d nor -u are set, default is -d -u. */
	if (!dflag && !uflag)
		dflag = uflag = 1;

	switch (argc) {
	case 0:
		ifp = stdin;
		ofp = stdout;
		break;
	case 1:
		ifp = file(argv[0], "r");
		ofp = stdout;
		break;
	case 2:
		ifp = file(argv[0], "r");
		ofp = file(argv[1], "w");
		break;
	default:
		usage();
	}

	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	prevsize = 0;
	prevline = NULL;
	if ((len = getline(&prevline, &prevsize, ifp)) == -1) {
		free(prevline);
		if (ferror(ifp))
			err(1, "getline");
		exit(0);
	}
	if (prevline[len - 1] == '\n')
		prevline[len - 1] = '\0';
	
	thissize = 0;
	thisline = NULL;
	while ((len = getline(&thisline, &thissize, ifp)) != -1) {
		if (thisline[len - 1] == '\n')
			thisline[len - 1] = '\0';

		/* If requested get the chosen fields + character offsets. */
		if (numfields || numchars) {
			t1 = skip(thisline);
			t2 = skip(prevline);
		} else {
			t1 = thisline;
			t2 = prevline;
		}

		/* If different, print; set previous to new value. */
		if ((iflag ? strcasecmp : strcmp)(t1, t2)) {
			show(ofp, prevline);
			t1 = prevline;
			prevline = thisline;
			thisline = t1;
			tmpsize = prevsize;
			prevsize = thissize;
			thissize = tmpsize;
			repeats = 0;
		} else
			++repeats;
	}
	free(thisline);
	if (ferror(ifp))
		err(1, "getline");

	show(ofp, prevline);
	free(prevline);

	exit(0);
}

/*
 * show --
 *	Output a line depending on the flags and number of repetitions
 *	of the line.
 */
void
show(FILE *ofp, char *str)
{
	if ((dflag && repeats) || (uflag && !repeats)) {
		if (cflag)
			fprintf(ofp, "%4d %s\n", repeats + 1, str);
		else
			fprintf(ofp, "%s\n", str);
	}
}

char *
skip(char *str)
{
	wchar_t wc;
	int nchars, nfields;
	int len;
	int field_started;

	for (nfields = numfields; nfields && *str; nfields--) {
		/* Skip one field, including preceding blanks. */
		for (field_started = 0; *str != '\0'; str += len) {
			if ((len = mbtowc(&wc, str, MB_CUR_MAX)) == -1) {
				(void)mbtowc(NULL, NULL, MB_CUR_MAX);
				wc = L'?';
				len = 1;
			}
			if (iswblank(wc)) {
				if (field_started)
					break;
			} else
				field_started = 1;
		}
	}

	/* Skip some additional characters. */
	for (nchars = numchars; nchars-- && *str != '\0'; str += len)
		if ((len = mblen(str, MB_CUR_MAX)) == -1)
			len = 1;

	return (str);
}

FILE *
file(char *name, char *mode)
{
	FILE *fp;

	if (strcmp(name, "-") == 0)
		return(*mode == 'r' ? stdin : stdout);
	if ((fp = fopen(name, mode)) == NULL)
		err(1, "%s", name);
	return (fp);
}

void
obsolete(char *argv[])
{
	size_t len;
	char *ap, *p, *start;

	while ((ap = *++argv)) {
		/* Return if "--" or not an option of any form. */
		if (ap[0] != '-') {
			if (ap[0] != '+')
				return;
		} else if (ap[1] == '-')
			return;
		if (!isdigit((unsigned char)ap[1]))
			continue;
		/*
		 * Digit signifies an old-style option.  Malloc space for dash,
		 * new option and argument.
		 */
		len = strlen(ap) + 3;
		if ((start = p = malloc(len)) == NULL)
			err(1, "malloc");
		*p++ = '-';
		*p++ = ap[0] == '+' ? 's' : 'f';
		(void)strlcpy(p, ap + 1, len - 2);
		*argv = start;
	}
}

__dead void
usage(void)
{
	extern char *__progname;

	(void)fprintf(stderr,
	    "usage: %s [-ci] [-d | -u] [-f fields] [-s chars] [input_file [output_file]]\n",
	    __progname);
	exit(1);
}