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 $ .\" $File: file.man,v 1.146 2022/10/26 16:56:14 christos Exp $
.Dd February 5, 2021 .Dd October 26, 2022
.Dt FILE __CSECTION__ .Dt FILE __CSECTION__
.Os .Os
.Sh NAME .Sh NAME

View File

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

View File

@ -37,7 +37,7 @@ OPT('e', "exclude", 1, 0,
" performed for file. Valid tests are:\n" " performed for file. Valid tests are:\n"
" %e\n") " %e\n")
OPT_LONGONLY("exclude-quiet", 1, 0, 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, OPT('f', "files-from", 1, 0,
" FILE read the filenames to be examined from FILE\n") " FILE read the filenames to be examined from FILE\n")
OPT('F', "separator", 1, 0, OPT('F', "separator", 1, 0,