--- Src/hist.c.new 2005-03-16 18:26:16.638813544 -0800 +++ Src/hist.c 2005-03-16 18:40:32.629680617 -0800 @@ -2049,9 +2049,14 @@ savehistfile(char *fn, int err, int writ out = fdopen(open(unmeta(fn), O_CREAT | O_WRONLY | O_TRUNC | O_NOCTTY, 0600), "w"); } else { - tmpfile = bicat(unmeta(fn), ".new"); - unlink(tmpfile); - out = fdopen(open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600), "w"); + int fd; + if ((fd = gettempfile(fn, 0, &tmpfile)) >= 0) { + if (!(out = fdopen(fd, "w"))) + close(fd); + } else { + tmpfile = bicat(unmeta(fn), ".XXXXXX"); + out = NULL; + } } if (out) { for (; he && he->histnum <= xcurhist; he = down_histent(he)) { @@ -2097,8 +2102,10 @@ savehistfile(char *fn, int err, int writ } fclose(out); if (tmpfile) { - if (rename(tmpfile, unmeta(fn)) < 0) - zerr("can't rename %s.new to $HISTFILE", fn, 0); + if (rename(tmpfile, unmeta(fn)) < 0) { + tmpfile = metafy(tmpfile, -1, META_REALLOC); + zerr("can't rename %s to $HISTFILE", tmpfile, 0); + } free(tmpfile); } @@ -2122,7 +2129,8 @@ savehistfile(char *fn, int err, int writ } } else if (err) { if (tmpfile) { - zerr("can't write history file %s.new", fn, 0); + tmpfile = metafy(tmpfile, -1, META_REALLOC); + zerr("can't write history file %s", tmpfile, 0); free(tmpfile); } else zerr("can't write history file %s", fn, 0);