patch: 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
69099ba2ec), and there's been some previous patches (ee3ca711a8
8c98e6b1a7 1a2a4fc8ce) 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
This commit is contained in:
Martin Tournoij 2024-04-19 15:11:30 -06:00 committed by Warner Losh
parent d3643c9efe
commit 25696725b6
2 changed files with 17 additions and 19 deletions

View file

@ -283,8 +283,9 @@ static void
plan_b(const char *filename)
{
FILE *ifp;
size_t i, j, len, maxlen;
char *lbuf = NULL, *p;
size_t i = 0, j, blen = 0, maxlen = 1;
ssize_t len;
char *p = NULL;
bool found_revision = (revision == NULL);
using_plan_a = false;
@ -295,26 +296,20 @@ plan_b(const char *filename)
pfatal("can't open file %s", TMPINNAME);
len = 0;
maxlen = 1;
while ((p = fgetln(ifp, &len)) != NULL) {
while ((len = getline(&p, &blen, ifp)) >= 0) {
if (p[len - 1] == '\n')
p[len - 1] = '\0';
else {
/* EOF without EOL, copy and add the NUL */
if ((lbuf = malloc(len + 1)) == NULL)
fatal("out of memory\n");
memcpy(lbuf, p, len);
lbuf[len] = '\0';
p = lbuf;
/* EOF without EOL */
last_line_missing_eol = true;
len++;
}
if (revision != NULL && !found_revision && rev_in_string(p))
found_revision = true;
if (len > maxlen)
if ((size_t)len > maxlen)
maxlen = len; /* find longest line */
}
free(lbuf);
free(p);
if (ferror(ifp))
pfatal("can't read file %s", filename);

View file

@ -1213,14 +1213,15 @@ another_hunk(void)
size_t
pgets(bool do_indent)
{
char *line;
size_t len = 0;
char *line = NULL;
ssize_t len = 0;
size_t buflen = 0;
int indent = 0, skipped = 0;
line = fgetln(pfp, &len);
if (line != NULL) {
if (len + 1 > buf_size) {
while (len + 1 > buf_size)
if ((len = getline(&line, &buflen, pfp)) >= 0) {
char *linep = line;
if ((size_t)(len + 1) > buf_size) {
while ((size_t)(len + 1) > buf_size)
buf_size *= 2;
free(buf);
buf = malloc(buf_size);
@ -1239,8 +1240,10 @@ pgets(bool do_indent)
}
memcpy(buf, line, len - skipped);
buf[len - skipped] = '\0';
line = linep;
}
return len;
free(line);
return (len > 0) ? len : 0;
}