Index: bin/sh/histedit.c =================================================================== --- bin/sh/histedit.c +++ bin/sh/histedit.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,66 @@ static char **sh_matches(const char *, int, int); static unsigned char sh_complete(EditLine *, int); +void +histsave(void) +{ + HistEvent he; + char *histtmpname = NULL; + const char *histfile; + int fd; + FILE *f; + + /* don't try to save if the history size is 0 */ + if (hist == NULL || histsizeval() == 0) + return; + histfile = expandstr("${HISTFILE-${HOME-}/.sh_history}"); + asprintf(&histtmpname, "%s.XXXXXXXXXX", histfile); + if (histtmpname == NULL) + return; + INTOFF; + fd = mkstemp(histtmpname); + if (fd < 0 || (f = fdopen(fd, "w")) == NULL) { + free(histtmpname); + INTON; + return; + } + + history(hist, &he, H_FIRST); + for (int ret = history(hist, &he, H_LAST); ret >= 0; + ret = history(hist, &he, H_PREV)) { + fwrite(he.str, 1, strlen(he.str), f); + } + + fclose(f); + if (rename(histtmpname, histfile) == -1) + unlink(histtmpname); + free(histtmpname); + INTON; +} + +static void +histload(void) +{ + const char *histfile; + char *line = NULL; + ssize_t linelen; + size_t linecap = 0; + FILE *f; + HistEvent he; + + /* don't try to save if the history size is 0 */ + if (hist == NULL || histsizeval() == 0) + return; + histfile = expandstr("${HISTFILE-${HOME-}/.sh_history}"); + if ((f = fopen(histfile, "r")) == NULL) + return; + + while ((linelen = getline(&line, &linecap, f)) > 0) + history(hist, &he, H_ENTER, line); + free(line); + fclose(f); +} + /* * Set history and editing status. Called whenever the status may * have changed (figures out what to do). @@ -101,6 +162,8 @@ sethistsize(histsizeval()); else out2fmt_flush("sh: can't initialize history\n"); + /* Load the history */ + histload(); } if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ /* Index: bin/sh/myhistedit.h =================================================================== --- bin/sh/myhistedit.h +++ bin/sh/myhistedit.h @@ -43,4 +43,4 @@ void histedit(void); void sethistsize(const char *); void setterm(const char *); - +void histsave(void); Index: bin/sh/trap.c =================================================================== --- bin/sh/trap.c +++ bin/sh/trap.c @@ -535,6 +535,9 @@ flushall(); #if JOBS setjobctl(0); +#endif +#ifndef NO_HISTORY + histsave(); #endif } if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&