From a7ab79fbe526eb83874b07411f65aa497640b1a7 Mon Sep 17 00:00:00 2001 From: christos Date: Mon, 15 Feb 2016 15:53:45 +0000 Subject: [PATCH] Use getline for better portability. --- lib/libedit/el.c | 15 +++++++++------ lib/libedit/history.c | 22 ++++++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/libedit/el.c b/lib/libedit/el.c index c164fba5da4a..92d0b386ab1a 100644 --- a/lib/libedit/el.c +++ b/lib/libedit/el.c @@ -1,4 +1,4 @@ -/* $NetBSD: el.c,v 1.76 2016/02/15 15:18:01 christos Exp $ */ +/* $NetBSD: el.c,v 1.77 2016/02/15 15:53:45 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94"; #else -__RCSID("$NetBSD: el.c,v 1.76 2016/02/15 15:18:01 christos Exp $"); +__RCSID("$NetBSD: el.c,v 1.77 2016/02/15 15:53:45 christos Exp $"); #endif #endif /* not lint && not SCCSID */ @@ -513,6 +513,7 @@ el_source(EditLine *el, const char *fname) { FILE *fp; size_t len; + ssize_t slen; char *ptr; char *path = NULL; const Char *dptr; @@ -549,15 +550,17 @@ el_source(EditLine *el, const char *fname) return -1; } - for (; (ptr = fparseln(fp, &len, NULL, NULL, 0)) != NULL; free(ptr)) { + ptr = NULL; + len = 0; + while ((slen = getline(&ptr, &len, fp)) != -1) { if (*ptr == '\n') continue; /* Empty line. */ + if (slen > 0 && ptr[--slen] == '\n') + ptr[slen] = '\0'; + dptr = ct_decode_string(ptr, &el->el_scratch); if (!dptr) continue; - if (len > 0 && dptr[len - 1] == '\n') - --len; - /* loop until first non-space char or EOL */ while (*dptr != '\0' && Isspace(*dptr)) dptr++; diff --git a/lib/libedit/history.c b/lib/libedit/history.c index 737ebe330644..ee3bd14737c7 100644 --- a/lib/libedit/history.c +++ b/lib/libedit/history.c @@ -1,4 +1,4 @@ -/* $NetBSD: history.c,v 1.49 2016/02/15 15:30:50 christos Exp $ */ +/* $NetBSD: history.c,v 1.50 2016/02/15 15:53:45 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: history.c,v 1.49 2016/02/15 15:30:50 christos Exp $"); +__RCSID("$NetBSD: history.c,v 1.50 2016/02/15 15:53:45 christos Exp $"); #endif #endif /* not lint && not SCCSID */ @@ -732,7 +732,9 @@ history_load(TYPE(History) *h, const char *fname) { FILE *fp; char *line; - size_t sz, max_size; + size_t llen; + ssize_t sz; + size_t max_size; char *ptr; int i = -1; TYPE(HistEvent) ev; @@ -743,20 +745,24 @@ history_load(TYPE(History) *h, const char *fname) if ((fp = fopen(fname, "r")) == NULL) return i; - if ((line = fparseln(fp, &sz, NULL, NULL, 0)) == NULL) + line = NULL; + llen = 0; + if ((sz = getline(&line, &llen, fp)) == -1) goto done; - if (strncmp(line, hist_cookie, sz) != 0) + if (strncmp(line, hist_cookie, (size_t)sz) != 0) goto done; ptr = h_malloc((max_size = 1024) * sizeof(*ptr)); if (ptr == NULL) goto done; free(line); - for (i = 0; (line = fparseln(fp, &sz, NULL, NULL, 0)) != NULL; i++) { - if (max_size < sz) { + for (i = 0; (sz = getline(&line, &llen, fp)) != -1; i++) { + if (sz > 0 && line[sz - 1] == '\n') + line[--sz] = '\0'; + if (max_size < (size_t)sz) { char *nptr; - max_size = (sz + 1024) & (size_t)~1023; + max_size = ((size_t)sz + 1024) & (size_t)~1023; nptr = h_realloc(ptr, max_size * sizeof(*ptr)); if (nptr == NULL) { i = -1;