From 5204b700b33b78f83d519efca9cee435911ebf9d Mon Sep 17 00:00:00 2001 From: "Tim J. Robbins" Date: Wed, 29 May 2002 14:23:10 +0000 Subject: [PATCH] Use mkstemp(3) to avoid /tmp race. --- usr.bin/gencat/gencat.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/usr.bin/gencat/gencat.c b/usr.bin/gencat/gencat.c index 7826dfe61bd9..1d6c41e84401 100644 --- a/usr.bin/gencat/gencat.c +++ b/usr.bin/gencat/gencat.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -136,7 +137,7 @@ main(int argc, char *argv[]) static void writeIfChanged(char *fname, int lang, int orConsts) { - char tmpname[32]; + char tmpname[] = _PATH_TMP"/gencat.XXXXXX"; char buf[BUFSIZ], tbuf[BUFSIZ], *cptr, *tptr; int fd, tfd; int diff = FALSE; @@ -153,9 +154,8 @@ writeIfChanged(char *fname, int lang, int orConsts) } /* If it does exist, create a temp file for now */ - sprintf(tmpname, "/tmp/gencat.%d", (int) getpid()); - if ((tfd = open(tmpname, O_RDWR|O_CREAT, 0666)) < 0) - errx(1, "unable to open temporary file: %s", tmpname); + if ((tfd = mkstemp(tmpname)) < 0) + err(1, "mkstemp"); unlink(tmpname); /* Write to the temp file and rewind */