diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-03-20 22:09:25 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-03-20 22:09:25 +0000 |
commit | f578ad5b3c8333858e917dabb2a8cf9c64f1e2d4 (patch) | |
tree | 3c66fb8cb6d7a62bdb6a769f96692e79b44f8b5a /lib | |
parent | d9adba9588aca2eacdaa26f99571b35de820f924 (diff) |
Use getline(3) rather than fgetln(3) because it is standardized
and simpler and safer to use. Implemented by Christos Zoulas
following my suggestion, with a bug fix by me.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libedit/config.h | 6 | ||||
-rw-r--r-- | lib/libedit/el.c | 29 | ||||
-rw-r--r-- | lib/libedit/history.c | 33 | ||||
-rw-r--r-- | lib/libedit/sys.h | 8 |
4 files changed, 34 insertions, 42 deletions
diff --git a/lib/libedit/config.h b/lib/libedit/config.h index b8d838656f3..e46383cdf70 100644 --- a/lib/libedit/config.h +++ b/lib/libedit/config.h @@ -1,10 +1,14 @@ -/* $OpenBSD: config.h,v 1.2 2010/06/30 00:05:35 nicm Exp $ */ +/* $OpenBSD: config.h,v 1.3 2016/03/20 22:09:24 schwarze Exp $ */ /* config.h. Generated automatically by configure. */ /* #undef SUNOS */ #define HAVE_SYS_CDEFS_H 1 #define HAVE_TERMCAP_H 1 /* #undef HAVE_CURSES_H */ + +/* Define to 1 if you have the `getline' function. */ +#define HAVE_GETLINE 1 + /* #undef HAVE_NCURSES_H */ /* #undef HAVE_TERM_H */ #define HAVE_VIS_H 1 diff --git a/lib/libedit/el.c b/lib/libedit/el.c index d557a9ae6fc..a304399b080 100644 --- a/lib/libedit/el.c +++ b/lib/libedit/el.c @@ -1,4 +1,4 @@ -/* $OpenBSD: el.c,v 1.25 2016/02/02 00:43:12 schwarze Exp $ */ +/* $OpenBSD: el.c,v 1.26 2016/03/20 22:09:24 schwarze Exp $ */ /* $NetBSD: el.c,v 1.61 2011/01/27 23:11:40 christos Exp $ */ /*- @@ -500,7 +500,8 @@ el_source(EditLine *el, const char *fname) { FILE *fp; size_t len; - char *ptr, *lptr = NULL; + ssize_t slen; + char *ptr; #ifdef HAVE_ISSETUGID char path[PATH_MAX]; #endif @@ -534,35 +535,29 @@ el_source(EditLine *el, const char *fname) if (fp == NULL) return -1; - while ((ptr = fgetln(fp, &len)) != NULL) { - if (ptr[len - 1] == '\n') - ptr[len - 1] = '\0'; - else { - if ((lptr = (char *)malloc(len + 1)) == NULL) { - (void) fclose(fp); - return -1; - } - memcpy(lptr, ptr, len); - lptr[len] = '\0'; - ptr = lptr; - } + 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; - /* loop until first non-space char or EOL */ while (*dptr != '\0' && Isspace(*dptr)) dptr++; if (*dptr == '#') continue; /* ignore, this is a comment line */ if (parse_line(el, dptr) == -1) { - free(lptr); + free(ptr); (void) fclose(fp); return -1; } } - free(lptr); + free(ptr); (void) fclose(fp); return 0; } diff --git a/lib/libedit/history.c b/lib/libedit/history.c index cbe9d0ce33b..2131fb518e0 100644 --- a/lib/libedit/history.c +++ b/lib/libedit/history.c @@ -1,4 +1,4 @@ -/* $OpenBSD: history.c,v 1.21 2016/01/30 17:32:52 schwarze Exp $ */ +/* $OpenBSD: history.c,v 1.22 2016/03/20 22:09:24 schwarze Exp $ */ /* $NetBSD: history.c,v 1.37 2010/01/03 18:27:10 christos Exp $ */ /*- @@ -722,8 +722,10 @@ private int history_load(TYPE(History) *h, const char *fname) { FILE *fp; - char *line, *lbuf; - size_t sz, max_size; + char *line; + size_t llen; + ssize_t sz; + size_t max_size; char *ptr; int i = -1; TYPE(HistEvent) ev; @@ -731,11 +733,12 @@ history_load(TYPE(History) *h, const char *fname) static ct_buffer_t conv; #endif - lbuf = NULL; if ((fp = fopen(fname, "r")) == NULL) return i; - if ((line = fgetln(fp, &sz)) == NULL) + line = NULL; + llen = 0; + if ((sz = getline(&line, &llen, fp)) == -1) goto done; if (strncmp(line, hist_cookie, sz) != 0) @@ -744,20 +747,10 @@ history_load(TYPE(History) *h, const char *fname) ptr = malloc(max_size = 1024); if (ptr == NULL) goto done; - for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) { - if (line[sz - 1] == '\n') - line[sz - 1] = '\0'; - else { - lbuf = malloc(sz + 1); - if (lbuf == NULL) { - i = -1; - goto oomem; - } - memcpy(lbuf, line, sz); - lbuf[sz++] = '\0'; - line = lbuf; - } - if (sz > max_size) { + for (i = 0; (sz = getline(&line, &llen, fp)) != -1; i++) { + if (sz > 0 && line[sz - 1] == '\n') + line[--sz] = '\0'; + if (max_size < sz) { char *nptr; max_size = (sz + 1024) & ~1023; nptr = realloc(ptr, max_size); @@ -776,7 +769,7 @@ history_load(TYPE(History) *h, const char *fname) oomem: free(ptr); done: - free(lbuf); + free(line); (void) fclose(fp); return i; } diff --git a/lib/libedit/sys.h b/lib/libedit/sys.h index 4c6aa970001..d8681ea2aee 100644 --- a/lib/libedit/sys.h +++ b/lib/libedit/sys.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sys.h,v 1.13 2016/03/20 21:25:27 schwarze Exp $ */ +/* $OpenBSD: sys.h,v 1.14 2016/03/20 22:09:24 schwarze Exp $ */ /* $NetBSD: sys.h,v 1.13 2009/12/30 22:37:40 christos Exp $ */ /*- @@ -88,9 +88,9 @@ size_t strlcat(char *dst, const char *src, size_t size); size_t strlcpy(char *dst, const char *src, size_t size); #endif -#ifndef HAVE_FGETLN -#define fgetln libedit_fgetln -char *fgetln(FILE *fp, size_t *len); +#ifndef HAVE_GETLINE +#define getline libedit_getline +ssize_t getline(char **line, size_t *len, FILE *fp); #endif #define REGEX /* Use POSIX.2 regular expression functions */ |