--- configure.ac 9 Dec 2005 19:20:02 -0000 1.46 +++ configure.ac 16 Dec 2005 18:29:59 -0000 @@ -1098,7 +1098,7 @@ dnl AC_FUNC_STRFTIME AC_CHECK_FUNCS(strftime difftime gettimeofday \ select poll \ readlink faccessx fchdir ftruncate \ - fstat lstat lchown \ + fstat lstat lchown fchown fchmod \ fseeko ftello \ mkfifo _mktemp mkstemp \ waitpid wait3 \ --- Src/hist.c 4 Nov 2005 16:20:34 -0000 1.61 +++ Src/hist.c 16 Dec 2005 18:29:59 -0000 @@ -2080,8 +2080,25 @@ savehistfile(char *fn, int err, int writ tmpfile = bicat(unmeta(fn), ".new"); if (unlink(tmpfile) < 0 && errno != ENOENT) out = NULL; - else - out = fdopen(open(tmpfile, O_CREAT | O_WRONLY | O_EXCL, 0600), "w"); + else { + struct stat sb; + int old_exists = stat(unmeta(fn), &sb) == 0; + + if (old_exists && sb.st_uid != geteuid()) { + tmpfile = NULL; /* Avoid an error about HISTFILE.new */ + out = NULL; + } else + out = fdopen(open(tmpfile, O_CREAT | O_WRONLY | O_EXCL, 0600), "w"); + +#ifdef HAVE_FCHMOD + if (old_exists && out) { +#ifdef HAVE_FCHOWN + fchown(fileno(out), sb.st_uid, sb.st_gid); +#endif + fchmod(fileno(out), sb.st_mode); + } +#endif + } } if (out) { for (; he && he->histnum <= xcurhist; he = down_histent(he)) {