head: 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 with69099ba2ec), and there's been some previous patches (ee3ca711a88c98e6b1a71a2a4fc8ce) 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:
committed by
Warner Losh
parent
9744821765
commit
3abcc79c6a
+5
-4
@@ -156,12 +156,13 @@ main(int argc, char *argv[])
|
||||
static void
|
||||
head(FILE *fp, intmax_t cnt)
|
||||
{
|
||||
char *cp;
|
||||
size_t error, readlen;
|
||||
char *cp = NULL;
|
||||
size_t error, bufsize = 0;
|
||||
ssize_t readlen;
|
||||
|
||||
while (cnt != 0 && (cp = fgetln(fp, &readlen)) != NULL) {
|
||||
while (cnt != 0 && (readlen = getline(&cp, &bufsize, fp)) >= 0) {
|
||||
error = fwrite(cp, sizeof(char), readlen, stdout);
|
||||
if (error != readlen)
|
||||
if ((ssize_t)error != readlen)
|
||||
err(1, "stdout");
|
||||
cnt--;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user