git: df4d33ecaac4 - stable/13 - MFV: cherry-pick "PR/358: Fix width for -f - (jpalus)"

From: Xin LI <delphij_at_FreeBSD.org>
Date: Thu, 15 Sep 2022 05:31:31 UTC
The branch stable/13 has been updated by delphij:

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

commit df4d33ecaac46330250472c096714f717950879c
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-09-07 06:31:20 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2022-09-15 05:31:13 +0000

    MFV: cherry-pick "PR/358: Fix width for -f - (jpalus)"
    
    PR:             bin/266264
    (cherry picked from commit bced4d8b3e99efd46b5496c83e11755b25913c90)
---
 contrib/file/src/file.c | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/contrib/file/src/file.c b/contrib/file/src/file.c
index 5300e5af8edf..bb058ce1e008 100644
--- a/contrib/file/src/file.c
+++ b/contrib/file/src/file.c
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef	lint
-FILE_RCSID("@(#)$File: file.c,v 1.195 2022/06/02 15:45:43 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.196 2022/07/04 17:00:51 christos Exp $")
 #endif	/* lint */
 
 #include "magic.h"
@@ -506,35 +506,47 @@ 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 = malloc(sizeof(*flist) * fimax);
 
-	if (strcmp("-", fn) == 0) {
+	if (flist == NULL)
+out:		file_err(EXIT_FAILURE, "Cannot allocate memory for file list");
+
+	if (strcmp("-", fn) == 0)
 		f = stdin;
-		wid = 1;
-	} else {
+	else {
 		if ((f = fopen(fn, "r")) == NULL) {
 			file_warn("Cannot open `%s'", fn);
 			return 1;
 		}
-
-		while ((len = getline(&line, &llen, f)) > 0) {
-			if (line[len - 1] == '\n')
-				line[len - 1] = '\0';
-			cwid = file_mbswidth(ms, line);
-			if (cwid > wid)
-				wid = cwid;
-		}
-
-		rewind(f);
 	}
 
 	while ((len = getline(&line, &llen, f)) > 0) {
 		if (line[len - 1] == '\n')
 			line[len - 1] = '\0';
-		e |= process(ms, line, wid);
+		if (fi >= fimax) {
+			fimax += 100;
+			char **nf = realloc(flist, fimax * sizeof(*flist));
+			if (nf == NULL)
+				goto out;
+		}
+		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]);
 	}
+	free(flist);
 
-	free(line);
-	(void)fclose(f);
+	if (f != stdin)
+		(void)fclose(f);
 	return e;
 }