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
commit3d9158ba3fdd296234ae55678f8eea869896a53b (patch)
treed078a14acd4236619c2a53d16f1ff8153ee5d62c
parent4ac3a6eb635a27c189dd75f75a75f90d84cd5229 (diff)
downloadtxr-3d9158ba3fdd296234ae55678f8eea869896a53b.tar.gz
txr-3d9158ba3fdd296234ae55678f8eea869896a53b.tar.bz2
txr-3d9158ba3fdd296234ae55678f8eea869896a53b.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);