2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-05-21 20:05:03 -04:00
|
|
|
#include <string.h>
|
2015-02-20 11:00:39 -05:00
|
|
|
#include <strings.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2014-11-16 07:37:43 -05:00
|
|
|
#include "queue.h"
|
2011-06-18 01:42:24 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum { Match = 0, NoMatch = 1, Error = 2 };
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-03-27 10:54:41 -04:00
|
|
|
static void addpattern(const char *, size_t);
|
2014-11-20 14:56:18 -05:00
|
|
|
static void addpatternfile(FILE *);
|
2014-06-01 08:03:10 -04:00
|
|
|
static int grep(FILE *, const char *);
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-01-22 12:07:57 -05:00
|
|
|
static int Eflag;
|
2014-11-20 09:35:23 -05:00
|
|
|
static int Fflag;
|
2014-11-20 09:14:26 -05:00
|
|
|
static int Hflag;
|
|
|
|
static int eflag;
|
2014-11-20 11:57:49 -05:00
|
|
|
static int fflag;
|
2014-11-20 09:14:26 -05:00
|
|
|
static int hflag;
|
2014-11-20 12:38:31 -05:00
|
|
|
static int iflag;
|
2014-11-20 09:14:26 -05:00
|
|
|
static int sflag;
|
|
|
|
static int vflag;
|
2015-01-22 12:07:57 -05:00
|
|
|
static int wflag;
|
2014-11-20 09:47:26 -05:00
|
|
|
static int xflag;
|
2014-11-13 15:24:47 -05:00
|
|
|
static int many;
|
2014-11-20 12:26:47 -05:00
|
|
|
static int mode;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern {
|
2013-09-27 11:26:22 -04:00
|
|
|
char *pattern;
|
2014-05-12 06:59:39 -04:00
|
|
|
regex_t preg;
|
2014-11-17 05:59:11 -05:00
|
|
|
SLIST_ENTRY(pattern) entry;
|
2014-11-16 07:37:43 -05:00
|
|
|
};
|
|
|
|
|
2014-11-17 05:59:11 -05:00
|
|
|
static SLIST_HEAD(phead, pattern) phead;
|
2013-09-27 11:26:22 -04:00
|
|
|
|
2014-06-01 08:03:10 -04:00
|
|
|
static void
|
2015-03-27 10:54:41 -04:00
|
|
|
addpattern(const char *pattern, size_t patlen)
|
2013-09-27 11:26:22 -04:00
|
|
|
{
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern *pnode;
|
2014-11-20 09:47:26 -05:00
|
|
|
char *tmp;
|
2015-01-22 12:07:57 -05:00
|
|
|
int bol, eol;
|
|
|
|
size_t len;
|
2013-09-27 11:26:22 -04:00
|
|
|
|
2015-03-27 10:54:41 -04:00
|
|
|
if (!patlen)
|
|
|
|
return;
|
|
|
|
|
2014-11-20 11:45:22 -05:00
|
|
|
/* a null BRE/ERE matches every line */
|
|
|
|
if (!Fflag)
|
|
|
|
if (pattern[0] == '\0')
|
|
|
|
pattern = ".";
|
|
|
|
|
2014-11-20 09:47:26 -05:00
|
|
|
if (!Fflag && xflag) {
|
2015-03-27 10:54:41 -04:00
|
|
|
tmp = enmalloc(Error, patlen + 3);
|
|
|
|
snprintf(tmp, patlen + 3, "%s%s%s",
|
2014-11-20 09:47:26 -05:00
|
|
|
pattern[0] == '^' ? "" : "^",
|
|
|
|
pattern,
|
2015-03-27 10:54:41 -04:00
|
|
|
pattern[patlen - 1] == '$' ? "" : "$");
|
2015-01-22 12:07:57 -05:00
|
|
|
} else if (!Fflag && wflag) {
|
2015-03-27 10:54:41 -04:00
|
|
|
len = patlen + 5 + (Eflag ? 2 : 4);
|
2015-02-10 20:08:17 -05:00
|
|
|
tmp = enmalloc(Error, len);
|
2015-01-22 12:07:57 -05:00
|
|
|
|
|
|
|
bol = eol = 0;
|
|
|
|
if (pattern[0] == '^')
|
|
|
|
bol = 1;
|
2015-03-27 10:54:41 -04:00
|
|
|
if (pattern[patlen - 1] == '$')
|
2015-01-22 12:07:57 -05:00
|
|
|
eol = 1;
|
|
|
|
|
2015-01-22 12:37:52 -05:00
|
|
|
snprintf(tmp, len, "%s\\<%s%.*s%s\\>%s",
|
2015-01-22 12:07:57 -05:00
|
|
|
bol ? "^" : "",
|
|
|
|
Eflag ? "(" : "\\(",
|
2015-03-27 10:54:41 -04:00
|
|
|
(int)patlen - bol - eol, pattern + bol,
|
2015-01-22 12:07:57 -05:00
|
|
|
Eflag ? ")" : "\\)",
|
|
|
|
eol ? "$" : "");
|
2014-11-20 09:47:26 -05:00
|
|
|
} else {
|
2015-02-10 20:08:17 -05:00
|
|
|
tmp = enstrdup(Error, pattern);
|
2014-11-20 11:45:22 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 20:08:17 -05:00
|
|
|
pnode = enmalloc(Error, sizeof(*pnode));
|
2014-11-20 11:45:22 -05:00
|
|
|
pnode->pattern = tmp;
|
2014-11-17 05:59:11 -05:00
|
|
|
SLIST_INSERT_HEAD(&phead, pnode, entry);
|
2013-09-27 11:26:22 -04:00
|
|
|
}
|
|
|
|
|
2014-11-20 11:57:49 -05:00
|
|
|
static void
|
2014-11-20 14:56:18 -05:00
|
|
|
addpatternfile(FILE *fp)
|
2014-11-20 11:57:49 -05:00
|
|
|
{
|
2014-12-16 15:20:41 -05:00
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t size = 0;
|
2015-01-31 09:19:42 -05:00
|
|
|
ssize_t len = 0;
|
2014-11-20 11:57:49 -05:00
|
|
|
|
2015-03-27 09:49:48 -04:00
|
|
|
while ((len = getline(&buf, &size, fp)) > 0) {
|
2015-01-31 09:19:42 -05:00
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
2014-11-20 11:57:49 -05:00
|
|
|
buf[len - 1] = '\0';
|
2015-03-27 10:54:41 -04:00
|
|
|
addpattern(buf, (size_t)len);
|
2014-11-20 11:57:49 -05:00
|
|
|
}
|
2014-11-21 06:43:53 -05:00
|
|
|
if (ferror(fp))
|
|
|
|
enprintf(Error, "read error:");
|
2014-11-20 11:57:49 -05:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:03:10 -04:00
|
|
|
static int
|
2014-05-12 06:59:39 -04:00
|
|
|
grep(FILE *fp, const char *str)
|
2011-05-22 21:36:34 -04:00
|
|
|
{
|
2014-12-16 15:20:41 -05:00
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t size = 0;
|
2015-01-31 09:19:42 -05:00
|
|
|
ssize_t len = 0;
|
2014-06-01 08:03:10 -04:00
|
|
|
long c = 0, n;
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern *pnode;
|
2014-06-01 08:03:10 -04:00
|
|
|
int match = NoMatch;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-03-27 09:49:48 -04:00
|
|
|
for (n = 1; (len = getline(&buf, &size, fp)) > 0; n++) {
|
2014-11-01 23:08:12 -04:00
|
|
|
/* Remove the trailing newline if one is present. */
|
|
|
|
if (len && buf[len - 1] == '\n')
|
|
|
|
buf[len - 1] = '\0';
|
2014-11-17 05:59:11 -05:00
|
|
|
SLIST_FOREACH(pnode, &phead, entry) {
|
2014-11-20 09:35:23 -05:00
|
|
|
if (!Fflag) {
|
2014-11-20 13:19:36 -05:00
|
|
|
if (regexec(&pnode->preg, buf[0] == '\0' ? "\n" : buf, 0, NULL, 0) ^ vflag)
|
2014-11-20 09:35:23 -05:00
|
|
|
continue;
|
|
|
|
} else {
|
2014-11-20 12:38:31 -05:00
|
|
|
if (!xflag) {
|
|
|
|
if ((iflag ? strcasestr : strstr)(buf, pnode->pattern))
|
|
|
|
match = Match;
|
|
|
|
else
|
|
|
|
match = NoMatch;
|
|
|
|
} else {
|
|
|
|
if (!(iflag ? strcasecmp : strcmp)(buf, pnode->pattern))
|
|
|
|
match = Match;
|
|
|
|
else
|
|
|
|
match = NoMatch;
|
|
|
|
}
|
2014-11-20 09:37:45 -05:00
|
|
|
if (match ^ vflag)
|
2014-11-20 09:35:23 -05:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
switch (mode) {
|
2013-09-27 11:26:22 -04:00
|
|
|
case 'c':
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
puts(str);
|
|
|
|
goto end;
|
|
|
|
case 'q':
|
|
|
|
exit(Match);
|
|
|
|
default:
|
2014-11-16 14:03:25 -05:00
|
|
|
if (!hflag && (many || Hflag))
|
2013-09-27 11:26:22 -04:00
|
|
|
printf("%s:", str);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (mode == 'n')
|
2013-09-27 11:26:22 -04:00
|
|
|
printf("%ld:", n);
|
2014-11-01 23:08:12 -04:00
|
|
|
puts(buf);
|
2013-09-27 11:26:22 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-06-01 08:03:10 -04:00
|
|
|
match = Match;
|
2014-11-20 12:02:48 -05:00
|
|
|
break;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
if (mode == 'c')
|
2011-05-25 15:40:47 -04:00
|
|
|
printf("%ld\n", c);
|
|
|
|
end:
|
2014-11-13 12:29:30 -05:00
|
|
|
if (ferror(fp)) {
|
2014-06-01 08:03:10 -04:00
|
|
|
weprintf("%s: read error:", str);
|
|
|
|
match = Error;
|
|
|
|
}
|
2013-09-27 11:26:22 -04:00
|
|
|
return match;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
2015-03-07 09:39:39 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
enprintf(Error, "usage: %s [-EFHchilnqsvwx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct pattern *pnode;
|
2015-05-15 07:37:33 -04:00
|
|
|
int m, flags = REG_NOSUB, match = NoMatch;
|
2015-03-07 09:39:39 -05:00
|
|
|
FILE *fp;
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
SLIST_INIT(&phead);
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'E':
|
|
|
|
Eflag = 1;
|
|
|
|
flags |= REG_EXTENDED;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
Fflag = 1;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
|
|
|
hflag = 0;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
arg = EARGF(usage());
|
2015-03-27 17:47:15 -04:00
|
|
|
if (!(fp = fmemopen(arg, strlen(arg) + 1, "r")))
|
2015-03-27 10:54:41 -04:00
|
|
|
eprintf("fmemopen:");
|
2015-03-07 09:39:39 -05:00
|
|
|
addpatternfile(fp);
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
efshut(fp, arg);
|
2015-03-07 09:39:39 -05:00
|
|
|
eflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
arg = EARGF(usage());
|
|
|
|
fp = fopen(arg, "r");
|
|
|
|
if (!fp)
|
|
|
|
enprintf(Error, "fopen %s:", arg);
|
|
|
|
addpatternfile(fp);
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
efshut(fp, arg);
|
2015-03-07 09:39:39 -05:00
|
|
|
fflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
Hflag = 0;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
case 'l':
|
|
|
|
case 'n':
|
|
|
|
case 'q':
|
|
|
|
mode = ARGC();
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
flags |= REG_ICASE;
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2015-03-07 09:39:39 -05:00
|
|
|
|
|
|
|
if (argc == 0 && !eflag && !fflag)
|
|
|
|
usage(); /* no pattern */
|
|
|
|
|
|
|
|
/* just add literal pattern to list */
|
|
|
|
if (!eflag && !fflag) {
|
2015-03-27 17:47:15 -04:00
|
|
|
if (!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r")))
|
2015-03-27 10:54:41 -04:00
|
|
|
eprintf("fmemopen:");
|
2015-03-07 09:39:39 -05:00
|
|
|
addpatternfile(fp);
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
efshut(fp, argv[0]);
|
2015-03-07 09:39:39 -05:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Fflag)
|
|
|
|
/* Compile regex for all search patterns */
|
|
|
|
SLIST_FOREACH(pnode, &phead, entry)
|
|
|
|
enregcomp(Error, &pnode->preg, pnode->pattern, flags);
|
|
|
|
many = (argc > 1);
|
|
|
|
if (argc == 0) {
|
|
|
|
match = grep(stdin, "<stdin>");
|
|
|
|
} else {
|
2015-05-15 07:28:39 -04:00
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-19 11:44:15 -04:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 07:28:39 -04:00
|
|
|
*argv = "<stdin>";
|
|
|
|
fp = stdin;
|
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
2015-03-07 09:39:39 -05:00
|
|
|
if (!sflag)
|
2015-05-15 07:28:39 -04:00
|
|
|
weprintf("fopen %s:", *argv);
|
2015-03-07 09:39:39 -05:00
|
|
|
match = Error;
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-15 07:28:39 -04:00
|
|
|
m = grep(fp, *argv);
|
2015-03-07 09:39:39 -05:00
|
|
|
if (m == Error || (match != Error && m == Match))
|
|
|
|
match = m;
|
2015-05-15 07:28:39 -04:00
|
|
|
if (fp != stdin && fshut(fp, *argv))
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
match = Error;
|
2015-03-07 09:39:39 -05:00
|
|
|
}
|
|
|
|
}
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
|
2015-05-24 19:33:19 -04:00
|
|
|
if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
|
|
|
|
match = Error;
|
|
|
|
|
2015-03-07 09:39:39 -05:00
|
|
|
return match;
|
|
|
|
}
|