summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-11-02 06:18:40 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-11-02 06:18:40 -0700
commita836c24de4d2488c4740c1143f252bc086a5ffbe (patch)
treed078a14acd4236619c2a53d16f1ff8153ee5d62c
parent1cf6c607eb35525d2329651e1d75f21b3a8e82b2 (diff)
downloadtxr-a836c24de4d2488c4740c1143f252bc086a5ffbe.tar.gz
txr-a836c24de4d2488c4740c1143f252bc086a5ffbe.tar.bz2
txr-a836c24de4d2488c4740c1143f252bc086a5ffbe.zip
listener: use temp file when saving history.
We don't want ot overwrite the history file in-place; if something goes wrong, we will lose half of it. * parser.c (repl): Save the history to a .tmp file, and then rename that to the target name, if the write is successful.
-rw-r--r--parser.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/parser.c b/parser.c
index 778fad12..8212fdd7 100644
--- a/parser.c
+++ b/parser.c
@@ -1376,8 +1376,14 @@ val repl(val bindings, val in_stream, val out_stream)
dyn_env = saved_dyn_env;
- if (histfile_w)
- lino_hist_save(ls, histfile_w);
+ if (histfile_w) {
+ val histfile_tmp = format(nil, lit("~a/.txr_history.tmp"), home, nao);
+ if (lino_hist_save(ls, c_str(histfile_tmp)) == 0)
+ rename_path(histfile_tmp, histfile);
+ else
+ put_line(lit("** unable to save history file"), out_stream);
+ gc_hint(histfile_tmp);
+ }
free(line_w);
lino_free(ls);