MFV: Restore the ability to process files from stdin immediately.

PR:		bin/267221
MFC after:	3 days
This commit is contained in:
Xin LI 2022-10-27 00:12:53 -07:00
commit 07dfb236c8
3 changed files with 25 additions and 17 deletions

View File

@ -1,5 +1,5 @@
.\" $File: file.man,v 1.144 2021/02/05 22:08:31 christos Exp $
.Dd February 5, 2021
.\" $File: file.man,v 1.146 2022/10/26 16:56:14 christos Exp $
.Dd October 26, 2022
.Dt FILE __CSECTION__
.Os
.Sh NAME

View File

@ -512,11 +512,8 @@ unwrap(struct magic_set *ms, const char *fn)
size_t llen = 0;
int wid = 0, cwid;
int e = 0;
size_t fi = 0, fimax = 100;
char **flist = CAST(char **, malloc(sizeof(*flist) * fimax));
if (flist == NULL)
out: file_err(EXIT_FAILURE, "Cannot allocate memory for file list");
size_t fi = 0, fimax = 0;
char **flist = NULL;
if (strcmp("-", fn) == 0)
f = stdin;
@ -530,26 +527,37 @@ out: file_err(EXIT_FAILURE, "Cannot allocate memory for file list");
while ((len = getline(&line, &llen, f)) > 0) {
if (line[len - 1] == '\n')
line[len - 1] = '\0';
cwid = file_mbswidth(ms, line);
if (nobuffer) {
e |= process(ms, line, cwid);
free(line);
line = NULL;
llen = 0;
continue;
}
if (cwid > wid)
wid = cwid;
if (fi >= fimax) {
fimax += 100;
char **nf = CAST(char **,
realloc(flist, fimax * sizeof(*flist)));
if (nf == NULL)
goto out;
if (nf == NULL) {
file_err(EXIT_FAILURE,
"Cannot allocate memory for file list");
}
flist = nf;
}
flist[fi++] = line;
cwid = file_mbswidth(ms, line);
if (cwid > wid)
wid = cwid;
line = NULL;
llen = 0;
}
fimax = fi;
for (fi = 0; fi < fimax; fi++) {
e |= process(ms, flist[fi], wid);
free(flist[fi]);
if (!nobuffer) {
fimax = fi;
for (fi = 0; fi < fimax; fi++) {
e |= process(ms, flist[fi], wid);
free(flist[fi]);
}
}
free(flist);

View File

@ -37,7 +37,7 @@ OPT('e', "exclude", 1, 0,
" performed for file. Valid tests are:\n"
" %e\n")
OPT_LONGONLY("exclude-quiet", 1, 0,
" TEST like exclude, but ignore unknown tests\n", OPT_EXCLUDE_QUIET)
" TEST like exclude, but ignore unknown tests\n", OPT_EXCLUDE_QUIET)
OPT('f', "files-from", 1, 0,
" FILE read the filenames to be examined from FILE\n")
OPT('F', "separator", 1, 0,