about summary refs log tree commit diff
path: root/mseq.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2018-01-06 23:26:05 +0100
committerLeah Neukirchen <leah@vuxu.org>2018-01-06 23:26:05 +0100
commited16bee191aee7b03d8d8af249102395b9ac5a7f (patch)
tree4682918594ae8cb64c98ea7a5801366125f4e379 /mseq.c
parent5f500a91340c5b2d7e8628347793249f387df7f2 (diff)
downloadmblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.tar.gz
mblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.tar.xz
mblaze-ed16bee191aee7b03d8d8af249102395b9ac5a7f.zip
mseq: create tmpfile O_EXCL to detect multiple mseq -S in a pipeline
Diffstat (limited to 'mseq.c')
-rw-r--r--mseq.c14
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) {