From a836c24de4d2488c4740c1143f252bc086a5ffbe Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 2 Nov 2018 06:18:40 -0700 Subject: 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. --- parser.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'parser.c') 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); -- cgit v1.2.3