From 0bada8603d59cf4f15ff6b20a27c4137b9c0abe6 Mon Sep 17 00:00:00 2001 From: Mike Heffner Date: Sat, 7 Jul 2001 04:08:32 +0000 Subject: [PATCH] Attempt to use the environment variable TMPDIR for the temporary directory, defaulting to /tmp. PR: bin/16924 Reviewed by: dd MFC after: 2 weeks --- lib/libc/stdio/tmpfile.c | 16 +++++++++++++--- lib/libc/stdio/tmpnam.3 | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/libc/stdio/tmpfile.c b/lib/libc/stdio/tmpfile.c index e3e296b63487..5aabe035fc7e 100644 --- a/lib/libc/stdio/tmpfile.c +++ b/lib/libc/stdio/tmpfile.c @@ -46,6 +46,7 @@ static char sccsid[] = "@(#)tmpfile.c 8.1 (Berkeley) 6/4/93"; #include #include #include +#include #include #include #include "un-namespace.h" @@ -57,10 +58,17 @@ tmpfile() FILE *fp; int fd, sverrno; #define TRAILER "tmp.XXXXXX" - char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)]; + char *buf; + const char *tmpdir; - (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1); - (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); + tmpdir = getenv("TMPDIR"); + if (tmpdir == NULL) + tmpdir = _PATH_TMP; + + (void)asprintf(&buf, "%s%s%s", tmpdir, + (tmpdir[strlen(tmpdir) - 1] == '/') ? "" : "/", TRAILER); + if (buf == NULL) + return (NULL); sigfillset(&set); (void)_sigprocmask(SIG_BLOCK, &set, &oset); @@ -69,6 +77,8 @@ tmpfile() if (fd != -1) (void)unlink(buf); + free(buf); + (void)_sigprocmask(SIG_SETMASK, &oset, NULL); if (fd == -1) diff --git a/lib/libc/stdio/tmpnam.3 b/lib/libc/stdio/tmpnam.3 index ed71236223de..0c4e861c25ed 100644 --- a/lib/libc/stdio/tmpnam.3 +++ b/lib/libc/stdio/tmpnam.3 @@ -67,6 +67,13 @@ returns, causing the file to be automatically deleted when the last reference to it is closed. The file is opened with the access value .Ql w+ . +The file is created in the directory determined by the environment variable +.Ev TMPDIR +if set. +The default location if +.Ev TMPDIR +is not set is +.Pa /tmp . .Pp The .Fn tmpnam