Use err(3) instead of local redefinition.

This commit is contained in:
Philippe Charnier 1997-07-10 06:46:13 +00:00
parent b34f7debd8
commit c851008590

View File

@ -32,19 +32,23 @@
*/
#ifndef lint
static char copyright[] =
static const char copyright[] =
"@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -56,7 +60,6 @@ static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
* Bill Joy UCB August 24, 1977
*/
void err __P((int, const char *, ...));
void head __P((FILE *, int));
void head_bytes __P((FILE *, int));
void obsolete __P((char *[]));
@ -80,12 +83,12 @@ main(argc, argv)
case 'c':
bytecnt = strtol(optarg, &ep, 10);
if (*ep || bytecnt <= 0)
err(1, "illegal byte count -- %s", optarg);
errx(1, "illegal byte count -- %s", optarg);
break;
case 'n':
linecnt = strtol(optarg, &ep, 10);
if (*ep || linecnt <= 0)
err(1, "illegal line count -- %s", optarg);
errx(1, "illegal line count -- %s", optarg);
break;
case '?':
default:
@ -95,13 +98,13 @@ main(argc, argv)
argv += optind;
if (linecnt != -1 && bytecnt != -1)
err(1, "can't combine line and byte counts");
errx(1, "can't combine line and byte counts");
if (linecnt == -1 )
linecnt = 10;
if (*argv) {
for (first = 1; *argv; ++argv) {
if ((fp = fopen(*argv, "r")) == NULL) {
err(0, "%s: %s", *argv, strerror(errno));
warn("%s", *argv);
continue;
}
if (argc > 1) {
@ -133,7 +136,7 @@ head(fp, cnt)
while (cnt && (ch = getc(fp)) != EOF) {
if (putchar(ch) == EOF)
err(1, "stdout: %s", strerror(errno));
err(1, "stdout");
if (ch == '\n')
cnt--;
}
@ -156,7 +159,7 @@ head_bytes(fp, cnt)
if (readlen == EOF)
break;
if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
err(1, "stdout: %s", strerror(errno));
err(1, "stdout");
cnt -= readlen;
}
}
@ -172,7 +175,7 @@ obsolete(argv)
if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
return;
if ((ap = malloc(strlen(*argv) + 2)) == NULL)
err(1, "%s", strerror(errno));
err(1, NULL);
ap[0] = '-';
ap[1] = 'n';
(void)strcpy(ap + 2, *argv + 1);
@ -183,37 +186,6 @@ obsolete(argv)
void
usage()
{
(void)fprintf(stderr, "usage: head [-n lines] [file ...]\n");
(void)fprintf(stderr, "usage: head [-n lines] [-c bytes] [file ...]\n");
exit(1);
}
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
void
#if __STDC__
err(int fatal, const char *fmt, ...)
#else
err(fatal, fmt, va_alist)
int fatal;
char *fmt;
va_dcl
#endif
{
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
(void)fprintf(stderr, "head: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
if (fatal)
exit(1);
eval = 1;
}