diff --git a/NEWS b/NEWS index 49d0da746c1b..108427cbb5b8 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,9 @@ Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library. 2023-09-15, that occurred on some systems when running an external command with a redirection from a command substitution. +- Fixed an init-time crash on failure to trim the shell command history file + due to a non-writable parent directory; ksh now prints a warning instead. + 2023-12-25: - Fixed a regression in the behavior of 'exit' in a trap action, introduced diff --git a/src/cmd/ksh93/edit/history.c b/src/cmd/ksh93/edit/history.c index c49c23244d00..f3672bf6e220 100644 --- a/src/cmd/ksh93/edit/history.c +++ b/src/cmd/ksh93/edit/history.c @@ -419,34 +419,13 @@ static History_t* hist_trim(History_t *hp, int n) char *cp; int incmd=1, c=0; History_t *hist_new, *hist_old = hp; - char *buff, *endbuff, *tmpname=0; + char *buff, *endbuff; off_t oldp,newp; struct stat statb; - unlink(hist_old->histname); - if(access(hist_old->histname,F_OK) >= 0) + if(unlink(hist_old->histname) < 0) { - /* The unlink can fail on Windows 95 */ - int fd; - char *last, *name=hist_old->histname; - sh_close(sffileno(hist_old->histfp)); - tmpname = (char*)sh_malloc(strlen(name)+14); - if(last = strrchr(name,'/')) - { - *last = 0; - pathtmp(tmpname,name,"hist",NULL); - *last = '/'; - } - else - pathtmp(tmpname,e_dot,"hist",NULL); - if(rename(name,tmpname) < 0) - { - free(tmpname); - tmpname = name; - } - fd = open(tmpname,O_RDONLY|O_cloexec); - sfsetfd(hist_old->histfp,fd); - if(tmpname==name) - tmpname = 0; + errormsg(SH_DICT,ERROR_warn(0),"cannot trim history file %s; make sure parent directory is writable",hist_old->histname); + return hist_ptr = hist_old; } hist_ptr = 0; if(fstat(sffileno(hist_old->histfp),&statb)>=0) @@ -501,11 +480,6 @@ static History_t* hist_trim(History_t *hp, int n) } hist_cancel(hist_new); sfclose(hist_old->histfp); - if(tmpname) - { - unlink(tmpname); - free(tmpname); - } free((char*)hist_old); return hist_ptr = hist_new; }