diff options
author | Martin Tournoij <martin@arp242.net> | 2024-04-19 21:11:31 +0000 |
---|---|---|
committer | Warner Losh <imp@FreeBSD.org> | 2024-04-19 21:52:22 +0000 |
commit | 5fbdcd65fe5cc08959a6ea692b501ec0e98f8b9d (patch) | |
tree | 7f11f7cfae78b7bb037c6ae4627a1a6d0aa371cf /usr.bin | |
parent | 25696725b65b651814799574fdfadb1b1a5e0b02 (diff) | |
download | src-5fbdcd65fe5cc08959a6ea692b501ec0e98f8b9d.tar.gz src-5fbdcd65fe5cc08959a6ea692b501ec0e98f8b9d.zip |
xargs: 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
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/xargs/xargs.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/usr.bin/xargs/xargs.c b/usr.bin/xargs/xargs.c index d2c7d087645f..237beff26504 100644 --- a/usr.bin/xargs/xargs.c +++ b/usr.bin/xargs/xargs.c @@ -770,22 +770,22 @@ static int prompt(void) { regex_t cre; - size_t rsize; + size_t rsize = 0; int match; - char *response; + char *response = NULL; FILE *ttyfp; if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) return (2); /* Indicate that the TTY failed to open. */ (void)fprintf(stderr, "?..."); (void)fflush(stderr); - if ((response = fgetln(ttyfp, &rsize)) == NULL || + if (getline(&response, &rsize, ttyfp) < 0 || regcomp(&cre, nl_langinfo(YESEXPR), REG_EXTENDED) != 0) { (void)fclose(ttyfp); return (0); } - response[rsize - 1] = '\0'; match = regexec(&cre, response, 0, NULL, 0); + free(response); (void)fclose(ttyfp); regfree(&cre); return (match == 0); |