diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2014-05-19 08:58:35 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2014-05-19 08:58:35 +0000 |
commit | b12e99ddcf61a0a6938dd02d7bd6a6e109db1aff (patch) | |
tree | 90c9f5f8ebfe902a71eb999c8276ae2e3e472e4e /lib/libedit/history.c | |
parent | 205f64a57ed21efc2f7bda9917c34b90b5a15419 (diff) |
Add a H_SAVE_FP operation to history() which lets the history be saved
to an open file pointer. From NetBSD via Eitan Adler.
ok millert
Diffstat (limited to 'lib/libedit/history.c')
-rw-r--r-- | lib/libedit/history.c | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/lib/libedit/history.c b/lib/libedit/history.c index 76b0d70bc0c..1c8290b621f 100644 --- a/lib/libedit/history.c +++ b/lib/libedit/history.c @@ -1,4 +1,4 @@ -/* $OpenBSD: history.c,v 1.17 2014/01/19 11:48:54 tobias Exp $ */ +/* $OpenBSD: history.c,v 1.18 2014/05/19 08:58:34 nicm Exp $ */ /* $NetBSD: history.c,v 1.37 2010/01/03 18:27:10 christos Exp $ */ /*- @@ -103,6 +103,7 @@ private int history_getunique(TYPE(History) *, TYPE(HistEvent) *); private int history_set_fun(TYPE(History) *, TYPE(History) *); private int history_load(TYPE(History) *, const char *); private int history_save(TYPE(History) *, const char *); +private int history_save_fp(TYPE(History) *, FILE *); private int history_prev_event(TYPE(History) *, TYPE(HistEvent) *, int); private int history_next_event(TYPE(History) *, TYPE(HistEvent) *, int); private int history_next_string(TYPE(History) *, TYPE(HistEvent) *, const Char *); @@ -784,13 +785,12 @@ done: } -/* history_save(): +/* history_save_fp(): * TYPE(History) save function */ private int -history_save(TYPE(History) *h, const char *fname) +history_save_fp(TYPE(History) *h, FILE *fp) { - FILE *fp; TYPE(HistEvent) ev; int i = -1, retval; size_t len, max_size; @@ -799,9 +799,6 @@ history_save(TYPE(History) *h, const char *fname) static ct_buffer_t conv; #endif - if ((fp = fopen(fname, "w")) == NULL) - return (-1); - if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1) goto done; if (fputs(hist_cookie, fp) == EOF) @@ -830,11 +827,29 @@ history_save(TYPE(History) *h, const char *fname) oomem: h_free((ptr_t)ptr); done: - (void) fclose(fp); return (i); } +/* history_save(): + * History save function + */ +private int +history_save(TYPE(History) *h, const char *fname) +{ + FILE *fp; + int i; + + if ((fp = fopen(fname, "w")) == NULL) + return -1; + + i = history_save_fp(h, fp); + + (void) fclose(fp); + return i; +} + + /* history_prev_event(): * Find the previous event, with number given */ @@ -1015,6 +1030,12 @@ FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...) he_seterrev(ev, _HE_HIST_WRITE); break; + case H_SAVE_FP: + retval = history_save_fp(h, va_arg(va, FILE *)); + if (retval == -1) + he_seterrev(ev, _HE_HIST_WRITE); + break; + case H_PREV_EVENT: retval = history_prev_event(h, ev, va_arg(va, int)); break; |