aboutsummaryrefslogtreecommitdiff
path: root/bin/sh
diff options
context:
space:
mode:
authorPiotr Pawel Stefaniak <pstef@FreeBSD.org>2021-09-22 16:42:41 +0000
committerPiotr Pawel Stefaniak <pstef@FreeBSD.org>2021-09-22 20:23:32 +0000
commit1f82fb3834105fd8f1186b1c6d719d8a24738180 (patch)
tree80d10ce17345b0ea87b5438bad370f04e05c489a /bin/sh
parentc866d0c798a20b8f0a92df524f4ddd0d81511c88 (diff)
downloadsrc-1f82fb3834105fd8f1186b1c6d719d8a24738180.tar.gz
src-1f82fb3834105fd8f1186b1c6d719d8a24738180.zip
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.
Diffstat (limited to 'bin/sh')
-rw-r--r--bin/sh/histedit.c8
1 files 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 <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
+#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
@@ -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;
}
/*