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