WARNSify, fix .Nm usage, deprecate register, getopt returns -1, use <err.h>

This commit is contained in:
lukem 1997-10-20 02:27:04 +00:00
parent 0fad87214f
commit 8512105c8e
2 changed files with 25 additions and 51 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: uniq.1,v 1.5 1994/12/06 07:51:15 jtc Exp $
.\" $NetBSD: uniq.1,v 1.6 1997/10/20 02:27:04 lukem Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -43,7 +43,7 @@
.Nm uniq
.Nd report or filter out repeated lines in a file
.Sh SYNOPSIS
.Nm uniq
.Nm
.Op Fl c | Fl d | Fl u
.Op Fl f Ar fields
.Op Fl s Ar chars
@ -53,7 +53,7 @@
.Oc
.Sh DESCRIPTION
The
.Nm uniq
.Nm
utility reads the standard input comparing adjacent lines, and writes
a copy of each unique input line to the standard output.
The second and succeeding copies of identical adjacent input lines are
@ -114,7 +114,7 @@ such argument is used as the name of an input file, the second is used
as the name of an output file.
.Pp
The
.Nm uniq
.Nm
utility exits 0 on success, and >0 if an error occurs.
.Sh COMPATIBILITY
The historic
@ -126,7 +126,7 @@ options have been deprecated but are still supported in this implementation.
.Xr sort 1
.Sh STANDARDS
The
.Nm uniq
.Nm
utility is expected to be
.St -p1003.2
compatible.

View File

@ -1,4 +1,4 @@
/* $NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $ */
/* $NetBSD: uniq.c,v 1.8 1997/10/20 02:27:05 lukem Exp $ */
/*
* Copyright (c) 1989, 1993
@ -36,19 +36,20 @@
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
#endif
static char rcsid[] = "$NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $";
__RCSID("$NetBSD: uniq.c,v 1.8 1997/10/20 02:27:05 lukem Exp $");
#endif /* not lint */
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
@ -61,8 +62,8 @@ static char rcsid[] = "$NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $";
int cflag, dflag, uflag;
int numchars, numfields, repeats;
void err __P((const char *, ...));
FILE *file __P((char *, char *));
int main __P((int, char **));
void show __P((FILE *, char *));
char *skip __P((char *));
void obsolete __P((char *[]));
@ -73,13 +74,14 @@ main (argc, argv)
int argc;
char *argv[];
{
register char *t1, *t2;
char *t1, *t2;
FILE *ifp, *ofp;
int ch;
char *prevline, *thisline, *p;
ifp = ofp = NULL;
obsolete(argv);
while ((ch = getopt(argc, argv, "-cdf:s:u")) != EOF)
while ((ch = getopt(argc, argv, "-cdf:s:u")) != -1)
switch (ch) {
case '-':
--optind;
@ -93,12 +95,13 @@ main (argc, argv)
case 'f':
numfields = strtol(optarg, &p, 10);
if (numfields < 0 || *p)
err("illegal field skip value: %s", optarg);
errx(1, "illegal field skip value: %s", optarg);
break;
case 's':
numchars = strtol(optarg, &p, 10);
if (numchars < 0 || *p)
err("illegal character skip value: %s", optarg);
errx(1, "illegal character skip value: %s",
optarg);
break;
case 'u':
uflag = 1;
@ -138,7 +141,7 @@ done: argc -= optind;
prevline = malloc(MAXLINELEN);
thisline = malloc(MAXLINELEN);
if (prevline == NULL || thisline == NULL)
err("%s", strerror(errno));
err(1, "malloc");
if (fgets(prevline, MAXLINELEN, ifp) == NULL)
exit(0);
@ -180,15 +183,15 @@ show(ofp, str)
if (cflag && *str)
(void)fprintf(ofp, "%4d %s", repeats + 1, str);
if (dflag && repeats || uflag && !repeats)
if ((dflag && repeats) || (uflag && !repeats))
(void)fprintf(ofp, "%s", str);
}
char *
skip(str)
register char *str;
char *str;
{
register int infield, nchars, nfields;
int infield, nchars, nfields;
for (nfields = numfields, infield = 0; nfields && *str; ++str)
if (isspace(*str)) {
@ -209,7 +212,7 @@ file(name, mode)
FILE *fp;
if ((fp = fopen(name, mode)) == NULL)
err("%s: %s", name, strerror(errno));
err(1, "%s", name);
return(fp);
}
@ -220,7 +223,7 @@ obsolete(argv)
int len;
char *ap, *p, *start;
while (ap = *++argv) {
while ((ap = *++argv) != NULL) {
/* Return if "--" or not an option of any form. */
if (ap[0] != '-') {
if (ap[0] != '+')
@ -235,7 +238,7 @@ obsolete(argv)
*/
len = strlen(ap);
if ((start = p = malloc(len + 3)) == NULL)
err("%s", strerror(errno));
err(1, "malloc");
*p++ = '-';
*p++ = ap[0] == '+' ? 's' : 'f';
(void)strcpy(p, ap + 1);
@ -250,32 +253,3 @@ usage()
"usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
exit(1);
}
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
void
#if __STDC__
err(const char *fmt, ...)
#else
err(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
(void)fprintf(stderr, "uniq: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
exit(1);
/* NOTREACHED */
}