diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2006-05-25 03:20:33 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2006-05-25 03:20:33 +0000 |
commit | ec83f0570d5b0f4a321c1b1bd3b21203db4814a5 (patch) | |
tree | 17e0bcf14bc6c42e6877dc4c70b14d15a97812e1 | |
parent | 37ddf993bccdca7708a33a82f4c3e1209431461d (diff) |
Remove xmktemp() and uses plain old mkstemp(3), avoiding race
conditions.
OK moritz@, otto@
-rw-r--r-- | usr.bin/sdiff/common.c | 52 | ||||
-rw-r--r-- | usr.bin/sdiff/common.h | 3 | ||||
-rw-r--r-- | usr.bin/sdiff/edit.c | 21 | ||||
-rw-r--r-- | usr.bin/sdiff/extern.h | 5 | ||||
-rw-r--r-- | usr.bin/sdiff/sdiff.c | 12 |
5 files changed, 32 insertions, 61 deletions
diff --git a/usr.bin/sdiff/common.c b/usr.bin/sdiff/common.c index 9bf600a2310..0b568b97227 100644 --- a/usr.bin/sdiff/common.c +++ b/usr.bin/sdiff/common.c @@ -1,4 +1,4 @@ -/* $OpenBSD: common.c,v 1.3 2006/05/10 14:32:51 ray Exp $ */ +/* $OpenBSD: common.c,v 1.4 2006/05/25 03:20:32 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -6,8 +6,6 @@ */ #include <err.h> -#include <paths.h> -#include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -20,51 +18,3 @@ cleanup(const char *filename) err(2, "could not delete: %s", filename); exit(2); } - -/* - * Creates and returns the name of a temporary file. Takes a string - * (or NULL) is written to the temporary file. The returned string - * needs to be freed. - */ -char * -xmktemp(const char *s) -{ - FILE *file; - int fd; - const char *tmpdir; - char *filename; - - /* If TMPDIR is set, use it; otherwise use _PATH_TMP. */ - if (!(tmpdir = getenv("TMPDIR"))) - tmpdir = _PATH_TMP; - if (asprintf(&filename, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1) - err(2, "xmktemp"); - - /* Create temp file. */ - if ((fd = mkstemp(filename)) == -1) - err(2, "could not create temporary file"); - - /* If we don't write anything to the file, just close. */ - if (s == NULL) { - close(fd); - - return (filename); - } - - /* Open temp file for writing. */ - if ((file = fdopen(fd, "w")) == NULL) { - warn("could not open %s", filename); - cleanup(filename); - } - - /* Write to file. */ - if (fputs(s, file)) { - warn("could not write to %s", filename); - cleanup(filename); - } - - /* Close temp file. */ - fclose(file); - - return (filename); -} diff --git a/usr.bin/sdiff/common.h b/usr.bin/sdiff/common.h index c75aef2700b..330ecf83e73 100644 --- a/usr.bin/sdiff/common.h +++ b/usr.bin/sdiff/common.h @@ -1,4 +1,4 @@ -/* $OpenBSD: common.h,v 1.1 2006/02/20 08:38:18 otto Exp $ */ +/* $OpenBSD: common.h,v 1.2 2006/05/25 03:20:32 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -6,4 +6,3 @@ */ __dead void cleanup(const char *); -char *xmktemp(const char *); diff --git a/usr.bin/sdiff/edit.c b/usr.bin/sdiff/edit.c index 58099207fbd..3a9c03c69ac 100644 --- a/usr.bin/sdiff/edit.c +++ b/usr.bin/sdiff/edit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: edit.c,v 1.13 2006/05/10 14:32:51 ray Exp $ */ +/* $OpenBSD: edit.c,v 1.14 2006/05/25 03:20:32 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -12,6 +12,7 @@ #include <err.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include "common.h" @@ -69,6 +70,7 @@ eparse(const char *cmd, const char *left, const char *right) { FILE *file; size_t nread, nwritten; + int fd; char *filename; char buf[BUFSIZ], *text; @@ -121,7 +123,21 @@ RIGHT: } /* Create temp file. */ - filename = xmktemp(text); + if (asprintf(&filename, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1) + err(2, "asprintf"); + if ((fd = mkstemp(filename)) == -1) + err(2, "mkstemp"); + if (text != NULL) { + size_t len; + + len = strlen(text); + if ((nwritten = write(fd, text, len)) == -1 || + nwritten != len) { + warn("error writing to temp file"); + cleanup(filename); + } + } + close(fd); /* text is no longer used. */ free(text); @@ -165,7 +181,6 @@ RIGHT: warn("could not delete: %s", filename); fclose(file); - /* filename was malloc()ed in xmktemp(). */ free(filename); return (0); diff --git a/usr.bin/sdiff/extern.h b/usr.bin/sdiff/extern.h index 81a5d2b9bf0..01ad27144c9 100644 --- a/usr.bin/sdiff/extern.h +++ b/usr.bin/sdiff/extern.h @@ -1,10 +1,11 @@ -/* $OpenBSD: extern.h,v 1.3 2005/12/27 04:18:07 tedu Exp $ */ +/* $OpenBSD: extern.h,v 1.4 2006/05/25 03:20:32 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. * Public domain. */ -extern FILE *outfile; /* file to save changes to */ +extern FILE *outfile; /* file to save changes to */ +extern const char *tmpdir; int eparse(const char *, const char *, const char *); diff --git a/usr.bin/sdiff/sdiff.c b/usr.bin/sdiff/sdiff.c index b0ed9b99954..a46394be6b4 100644 --- a/usr.bin/sdiff/sdiff.c +++ b/usr.bin/sdiff/sdiff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sdiff.c,v 1.18 2006/05/10 14:32:51 ray Exp $ */ +/* $OpenBSD: sdiff.c,v 1.19 2006/05/25 03:20:32 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -17,6 +17,7 @@ #include <fcntl.h> #include <getopt.h> #include <limits.h> +#include <paths.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -65,6 +66,7 @@ int Iflag = 0; /* ignore sets matching regexp */ int lflag; /* print only left column for identical lines */ int sflag; /* skip identical lines */ FILE *outfile; /* file to save changes to */ +const char *tmpdir; /* TMPDIR or /tmp */ static struct option longopts[] = { { "text", no_argument, NULL, 'a' }, @@ -117,8 +119,9 @@ mktmpcpy(const char *source_file) } /* Not a regular file, so copy input into temporary file. */ - target_file = xmktemp(NULL); - if ((ofd = open(target_file, O_WRONLY, 0)) == -1) { + if (asprintf(&target_file, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1) + err(2, "asprintf"); + if ((ofd = mkstemp(target_file)) == -1) { warn("error opening %s", target_file); goto FAIL; } @@ -244,6 +247,9 @@ main(int argc, char **argv) if (argc != 2) usage(); + if ((tmpdir = getenv("TMPDIR")) == NULL) + tmpdir = _PATH_TMP; + filename1 = argv[0]; filename2 = argv[1]; |