-/* $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.
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);
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;
* 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--;