diff options
author | Leah Neukirchen <leah@vuxu.org> | 2018-01-06 23:26:05 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2018-01-06 23:26:05 +0100 |
commit | ed16bee191aee7b03d8d8af249102395b9ac5a7f (patch) | |
tree | 4682918594ae8cb64c98ea7a5801366125f4e379 | |
parent | 5f500a91340c5b2d7e8628347793249f387df7f2 (diff) | |
download | mblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.tar.gz mblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.tar.xz mblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.zip |
mseq: create tmpfile O_EXCL to detect multiple mseq -S in a pipeline
-rw-r--r-- | mseq.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/mseq.c b/mseq.c index e1b1f04..e8bdd62 100644 --- a/mseq.c +++ b/mseq.c @@ -1,5 +1,9 @@ +#include <sys/types.h> +#include <sys/stat.h> + #include <dirent.h> #include <errno.h> +#include <fcntl.h> #include <limits.h> #include <search.h> #include <stdio.h> @@ -186,22 +190,22 @@ stdinmode() char *seqfile = 0; if (Sflag) { - // XXX locking? seqfile = getenv("MAILSEQ"); if (!seqfile) seqfile = blaze822_home_file("seq"); snprintf(tmpfile, sizeof tmpfile, "%s-", seqfile); snprintf(oldfile, sizeof oldfile, "%s.old", seqfile); - outfile = fopen(tmpfile, "w+"); - if (!outfile) { + int fd = open(tmpfile, O_RDWR | O_EXCL | O_CREAT, 0666); + if (fd < 0) { fprintf(stderr, - "mseq: Could not create sequence file '%s': %s.\n", - seqfile, strerror(errno)); + "mseq: Could not create temporary sequence file '%s': %s.\n", + tmpfile, strerror(errno)); fprintf(stderr, "mseq: Ensure %s exists and is writable.\n", blaze822_home_file("")); exit(2); } + outfile = fdopen(fd, "w+"); if (Aflag) { FILE *seq = fopen(seqfile, "r"); if (seq) { |