From 1f82fb3834105fd8f1186b1c6d719d8a24738180 Mon Sep 17 00:00:00 2001 From: Piotr Pawel Stefaniak Date: Wed, 22 Sep 2021 18:42:41 +0200 Subject: [PATCH] sh: try to avoid overwriting HISTFILE produced by other shells If an attempt to load history from an existing history file was unsuccessful, do not try to save command history to that file on exit. --- bin/sh/histedit.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c index 4e82f7497850..96511b87b451 100644 --- a/bin/sh/histedit.c +++ b/bin/sh/histedit.c @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -70,6 +71,7 @@ __FBSDID("$FreeBSD$"); History *hist; /* history cookie */ EditLine *el; /* editline cookie */ int displayhist; +static int savehist; static FILE *el_in, *el_out; static char *fc_replace(const char *, char *, char *); @@ -103,7 +105,7 @@ histsave(void) int fd; FILE *f; - if ((histfile = get_histfile()) == NULL) + if (!savehist || (histfile = get_histfile()) == NULL) return; INTOFF; asprintf(&histtmpname, "%s.XXXXXXXXXX", histfile); @@ -134,7 +136,9 @@ histload(void) if ((histfile = get_histfile()) == NULL) return; - history(hist, &he, H_LOAD, histfile); + errno = 0; + if (history(hist, &he, H_LOAD, histfile) != -1 || errno == ENOENT) + savehist = 1; } /*