=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/sed/main.c,v retrieving revision 1.41 retrieving revision 1.42 diff -u -r1.41 -r1.42 --- src/usr.bin/sed/main.c 2020/10/13 06:07:54 1.41 +++ src/usr.bin/sed/main.c 2021/01/31 14:23:05 1.42 @@ -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 @@ 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 @@ 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 @@ * 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--;