mirror of
https://github.com/openbsd/src.git
synced 2024-12-21 23:18:00 -08:00
head(1): check for stdio errors
- Output errors are terminal. - Input errors yield a warning and cause head(1) to fail gracefully. Tweaked by millert@. Thread: https://marc.info/?l=openbsd-tech&m=164419238123928&w=2 ok millert@
This commit is contained in:
parent
41cf59d49f
commit
d9b0e4ff01
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: head.c,v 1.23 2022/01/29 00:19:04 cheloha Exp $ */
|
||||
/* $OpenBSD: head.c,v 1.24 2022/02/07 17:19:57 cheloha Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1980, 1987 Regents of the University of California.
|
||||
@ -96,30 +96,43 @@ main(int argc, char *argv[])
|
||||
int
|
||||
head_file(const char *path, long count, int need_header)
|
||||
{
|
||||
const char *name;
|
||||
FILE *fp;
|
||||
int ch;
|
||||
int ch, status = 0;
|
||||
static int first = 1;
|
||||
|
||||
if (path != NULL) {
|
||||
fp = fopen(path, "r");
|
||||
name = path;
|
||||
fp = fopen(name, "r");
|
||||
if (fp == NULL) {
|
||||
warn("%s", path);
|
||||
warn("%s", name);
|
||||
return 1;
|
||||
}
|
||||
if (need_header) {
|
||||
printf("%s==> %s <==\n", first ? "" : "\n", path);
|
||||
printf("%s==> %s <==\n", first ? "" : "\n", name);
|
||||
if (ferror(stdout))
|
||||
err(1, "stdout");
|
||||
first = 0;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
name = "stdin";
|
||||
fp = stdin;
|
||||
}
|
||||
|
||||
while ((ch = getc(fp)) != EOF) {
|
||||
if (putchar(ch) == EOF)
|
||||
err(1, "stdout");
|
||||
if (ch == '\n' && --count == 0)
|
||||
break;
|
||||
}
|
||||
if (ferror(fp)) {
|
||||
warn("%s", name);
|
||||
status = 1;
|
||||
}
|
||||
|
||||
for (; count > 0 && !feof(fp); --count)
|
||||
while ((ch = getc(fp)) != EOF)
|
||||
if (putchar(ch) == '\n')
|
||||
break;
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user