Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit Use getline for better portability.



details:   https://anonhg.NetBSD.org/src/rev/5abf5f3033ed
branches:  trunk
changeset: 343581:5abf5f3033ed
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Feb 15 15:53:45 2016 +0000

description:
Use getline for better portability.

diffstat:

 lib/libedit/el.c      |  15 +++++++++------
 lib/libedit/history.c |  22 ++++++++++++++--------
 2 files changed, 23 insertions(+), 14 deletions(-)

diffs (107 lines):

diff -r b0ff1153c1e7 -r 5abf5f3033ed lib/libedit/el.c
--- a/lib/libedit/el.c  Mon Feb 15 15:37:20 2016 +0000
+++ b/lib/libedit/el.c  Mon Feb 15 15:53:45 2016 +0000
@@ -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 @@
 {
        FILE *fp;
        size_t len;
+       ssize_t slen;
        char *ptr;
        char *path = NULL;
        const Char *dptr;
@@ -549,15 +550,17 @@
                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 -r b0ff1153c1e7 -r 5abf5f3033ed lib/libedit/history.c
--- a/lib/libedit/history.c     Mon Feb 15 15:37:20 2016 +0000
+++ b/lib/libedit/history.c     Mon Feb 15 15:53:45 2016 +0000
@@ -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 @@
 {
        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 @@
        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;



Home | Main Index | Thread Index | Old Index