From nobody Thu Sep 15 05:34:13 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4MSm913W35z4c4ts; Thu, 15 Sep 2022 05:34:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4MSm913048z3DJh; Thu, 15 Sep 2022 05:34:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663220053; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vF2wK+z0Dzf2KqwEYI4R7jIUTn1VloOl2mMQI8uevpA=; b=dFTihCT+A87JacqFgkyGUK0ZR1AFU5inVOltx/OTT/s4dtD1/SvZh1KBs+zQPwKPoyu1rb D8wOOtoJpTkM46dzOYzTZL2iP1yDTwwwNEPJHX2CcF37jeGFSOVgSzietXb9TbL0bkfvZx IZpN1gkUaM40+BZlVTMVH+Kp0tj/JkA1asUi6m9TKNxxcljdkvLm4YRANroMCOasJfOdSj 1rBDWaiNoFvn/6cl1Bh3kNn8MhMKdhUrOR4WNZl+1eziG5CPrdr2h6USEeKq/zqyvC2ypQ v1JrB/NdREdCz+ntDn1xBM/TL8eCB5ZnqZjOb7OLoifCxFyLIv16GIatzUWrMg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4MSm9122lJzvW1; Thu, 15 Sep 2022 05:34:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 28F5YDXK027354; Thu, 15 Sep 2022 05:34:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28F5YD8R027353; Thu, 15 Sep 2022 05:34:13 GMT (envelope-from git) Date: Thu, 15 Sep 2022 05:34:13 GMT Message-Id: <202209150534.28F5YD8R027353@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Xin LI Subject: git: 59146889b1b1 - stable/12 - MFV: cherry-pick "PR/358: Fix width for -f - (jpalus)" List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: delphij X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 59146889b1b1499ccf1769261cf8c98a85ce33db Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663220053; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vF2wK+z0Dzf2KqwEYI4R7jIUTn1VloOl2mMQI8uevpA=; b=RAoHFtD6Q3smhTiOcJp7YTWa1BzM8vrpTNeXqJdqYGHLP9pvlWqmHxK2Q7OgeDyc/IfSqO T5NQNHGyuhYl3eFCu3U3RmHBU9drh7Z34ZuyCQx7U8m5HkacqtgPeayg52kDhBPxw/GCYL cxMzIEkacR60hMZRFDLTxEq4InboTV4YSe1QUKcdJb1B8IIoFNaH3iK1V7EuMtUIhfaVsv HB3NTOnDMyfVLtOaAIiOQg9SpJJKHlDwyT90pjGzPTrmE7h6aeJeZoNjPdszYMWp440+q9 RntZmJHllVm3z7qgd6JhByWLs+/f2CdJTSiECufe5SQ0UHtCsahUoqNslnU/cA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1663220053; a=rsa-sha256; cv=none; b=XxUlJK/fRV3i4i2rx+mZEcUoFcs+iZUpDn/Ta8a2ZJKFjz2mEhmPrBcK6NmEtpZ2KZfvfo ylztzO+1BFIck31PBWU0nnZ61YG0mLPuJU9LQu1Kxn7enXUXpgUQda/vA0O304t2rpOMOd ze0eruYVv0wwGm9/nIMYxwCFyw+ipOY2eygnYXzL1HgzWP2/LUEG9SpTXZAfn5o6r//f6R uPt7On8cHlevrf2lo2iZVKP50ZR+dAdb7vHSlb3djDPs5z/K38x4hIrfx9izttwVIakZpr TL8zohtD3wYvB/oN09Fxl/LpbmCjGV2juqEL1neoctM6+/HqjhHa7dJLqghDAQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=59146889b1b1499ccf1769261cf8c98a85ce33db commit 59146889b1b1499ccf1769261cf8c98a85ce33db Author: Xin LI AuthorDate: 2022-09-07 06:31:20 +0000 Commit: Xin LI CommitDate: 2022-09-15 05:34:06 +0000 MFV: cherry-pick "PR/358: Fix width for -f - (jpalus)" MFC after: 1 week 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; }