diff options
author | Christian Weisgerber <naddy@cvs.openbsd.org> | 2021-01-31 14:23:06 +0000 |
---|---|---|
committer | Christian Weisgerber <naddy@cvs.openbsd.org> | 2021-01-31 14:23:06 +0000 |
commit | be2f6623eb37f438bc07b3a438bd79e1083818ea (patch) | |
tree | 896c729f0d71c61c5421edc3d61f0aefc31fcbbd | |
parent | 1a517a2d199d366b34977d7d90de225956982584 (diff) |
replace fgetln(3) with getline(3) in sed
Partly from Johann Oskarsson for Illumos/FreeBSD.
ok millert@
-rw-r--r-- | usr.bin/sed/main.c | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index a3b4e02c949..42179376c0c 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.41 2020/10/13 06:07:54 martijn Exp $ */ +/* $OpenBSD: main.c,v 1.42 2021/01/31 14:23:05 naddy Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -252,15 +252,9 @@ again: goto again; } case ST_FILE: - if ((p = fgetln(f, &len)) != NULL) { + if (getline(outbuf, outsize, f) != -1) { + p = *outbuf; linenum++; - if (len >= *outsize) { - free(*outbuf); - *outsize = ROUNDLEN(len + 1); - *outbuf = xmalloc(*outsize); - } - memcpy(*outbuf, p, len); - (*outbuf)[len] = '\0'; if (linenum == 1 && p[0] == '#' && p[1] == 'n') nflag = 1; return (*outbuf); @@ -344,7 +338,8 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) struct stat sb; size_t len; char dirbuf[PATH_MAX]; - char *p; + static char *p; + static size_t psize; int c, fd; static int firstfile; @@ -429,13 +424,13 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) * We are here only when infile is open and we still have something * to read from it. * - * Use fgetln so that we can handle essentially infinite input data. - * Can't use the pointer into the stdio buffer as the process space - * because the ungetc() can cause it to move. + * Use getline() so that we can handle essentially infinite input + * data. The p and psize are static so each invocation gives + * getline() the same buffer which is expanded as needed. */ - p = fgetln(infile, &len); - if (ferror(infile)) - error(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO)); + len = getline(&p, &psize, infile); + if ((ssize_t)len == -1) + error(FATAL, "%s: %s", fname, strerror(errno)); if (len != 0 && p[len - 1] == '\n') { sp->append_newline = 1; len--; |