From 945a40f7e69dcbd194a2a45889512c8849dce2c9 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Mon, 18 Oct 2004 19:07:30 +0000 Subject: - Improved lockhistfile() to use the new gettempname() for a unique filename instead of a .PID suffix. - Use bicat() to build the $HISTFILE.LOCK filename. - Put the $HOST value into $HISTFILE.LOCK in addition to the PID. --- Src/hist.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'Src') diff --git a/Src/hist.c b/Src/hist.c index da1e15ccc..3f7927010 100644 --- a/Src/hist.c +++ b/Src/hist.c @@ -2129,23 +2129,22 @@ lockhistfile(char *fn, int keep_trying) return 0; if (!lockhistct++) { struct stat sb; - int fd, len; + int fd; char *lockfile; #ifdef HAVE_LINK char *tmpfile; #endif - fn = unmeta(fn); - len = strlen(fn); - lockfile = zalloc(len + 5 + 1); - sprintf(lockfile, "%s.LOCK", fn); + lockfile = bicat(unmeta(fn), ".LOCK"); #ifdef HAVE_LINK - tmpfile = zalloc(len + 10 + 1); - sprintf(tmpfile, "%s.%ld", fn, (long)mypid); - unlink(tmpfile); /* NFS's O_EXCL is often broken... */ + tmpfile = gettempname(fn, 0); if ((fd = open(tmpfile, O_WRONLY|O_CREAT|O_EXCL, 0644)) >= 0) { - write(fd, tmpfile+len+1, strlen(tmpfile+len+1)); - close(fd); + FILE *out = fdopen(fd, "w"); + if (out) { + fprintf(out, "%ld %s\n", (long)getpid(), getsparam("HOST")); + fclose(out); + } else + close(fd); while (link(tmpfile, lockfile) < 0) { if (errno != EEXIST || !keep_trying) ; @@ -2183,10 +2182,12 @@ lockhistfile(char *fn, int keep_trying) if (fd < 0) lockhistct--; else { - char buf[16]; - sprintf(buf, "%ld", (long)mypid); - write(fd, buf, strlen(buf)); - close(fd); + FILE *out = fdopen(fd, "w"); + if (out) { + fprintf(out, "%ld %s\n", (long)mypid, getsparam("HOST")); + fclose(out); + } else + close(fd); } #endif /* not HAVE_LINK */ free(lockfile); -- cgit 1.4.1