Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Apr 2024 21:52:41 GMT
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 9744821765fc - main - diff: use getline() instead of fgetln()
Message-ID:  <202404192152.43JLqfel023789@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by imp:

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

commit 9744821765fccd6c9b44d47804b6913cdf616d4e
Author:     Martin Tournoij <martin@arp242.net>
AuthorDate: 2024-04-19 21:11:30 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-19 21:52:21 +0000

    diff: use getline() instead of fgetln()
    
    This replaces fgetln() with getline(). The main reason for this is
    portability, making things easier for people who want to compile these
    tools on non-FreeBSD systems.
    
    I appreciate that's probably not the top concern for FreeBSD base tools,
    but fgetln() is impossible to port to most platforms, as concurrent
    access is essentially impossible to implement fully correct without the
    line buffer on the FILE struct. Other than this, many generic FreeBSD
    tools compile fairly cleanly on Linux with a few small changes.
    
    Most uses of fgetln() pre-date getline() support (added in 2009 with
    69099ba2ec8b), and there's been some previous patches (ee3ca711a898
    8c98e6b1a7f3 1a2a4fc8ce1b) for other tools.
    
    Obtained from:  https://github.com/dcantrell/bsdutils and
                    https://github.com/chimera-linux/chimerautils
    Signed-off-by: Martin Tournoij <martin@arp242.net>
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/893
---
 usr.bin/diff/diff.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c
index 15ebbca28bd7..67aab0de91b5 100644
--- a/usr.bin/diff/diff.c
+++ b/usr.bin/diff/diff.c
@@ -517,20 +517,23 @@ static void
 read_excludes_file(char *file)
 {
 	FILE *fp;
-	char *buf, *pattern;
-	size_t len;
+	char *pattern = NULL;
+	size_t blen = 0;
+	ssize_t len;
 
 	if (strcmp(file, "-") == 0)
 		fp = stdin;
 	else if ((fp = fopen(file, "r")) == NULL)
 		err(2, "%s", file);
-	while ((buf = fgetln(fp, &len)) != NULL) {
-		if (buf[len - 1] == '\n')
-			len--;
-		if ((pattern = strndup(buf, len)) == NULL)
-			err(2, "xstrndup");
+	while ((len = getline(&pattern, &blen, fp)) >= 0) {
+		if ((len > 0) && (pattern[len - 1] == '\n'))
+			pattern[len - 1] = '\0';
 		push_excludes(pattern);
+		/* we allocate a new string per line */
+		pattern = NULL;
+		blen = 0;
 	}
+	free(pattern);
 	if (strcmp(file, "-") != 0)
 		fclose(fp);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202404192152.43JLqfel023789>