git: 14b1657ef3f3 - stable/13 - MFV: Restore the ability to process files from stdin immediately.

From: Xin LI <delphij_at_FreeBSD.org>
Date: Sun, 30 Oct 2022 04:42:00 UTC
The branch stable/13 has been updated by delphij:

URL: https://cgit.FreeBSD.org/src/commit/?id=14b1657ef3f3a4bf6ddb4df94e17697ecdbccd13

commit 14b1657ef3f3a4bf6ddb4df94e17697ecdbccd13
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-10-27 07:12:53 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2022-10-30 04:41:20 +0000

    MFV: Restore the ability to process files from stdin immediately.
    
    PR:             bin/267221
    MFC after:      3 days
    
    (cherry picked from commit 07dfb236c81483c2f08a3976f6d44ce307f4ceaf)
---
 contrib/file/doc/file.man    |  4 ++--
 contrib/file/src/file.c      | 36 ++++++++++++++++++++++--------------
 contrib/file/src/file_opts.h |  2 +-
 3 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/contrib/file/doc/file.man b/contrib/file/doc/file.man
index 5be5e9b093e4..91470b8e199d 100644
--- a/contrib/file/doc/file.man
+++ b/contrib/file/doc/file.man
@@ -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
diff --git a/contrib/file/src/file.c b/contrib/file/src/file.c
index 0efbb92cd520..46b4256a0c24 100644
--- a/contrib/file/src/file.c
+++ b/contrib/file/src/file.c
@@ -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);
 
diff --git a/contrib/file/src/file_opts.h b/contrib/file/src/file_opts.h
index 978c8b64434c..c78a8df2a800 100644
--- a/contrib/file/src/file_opts.h
+++ b/contrib/file/src/file_opts.h
@@ -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,