Attempt to use the environment variable TMPDIR for the temporary

directory, defaulting to /tmp.

PR:		bin/16924
Reviewed by:	dd
MFC after:	2 weeks
This commit is contained in:
Mike Heffner 2001-07-07 04:08:32 +00:00
parent 3b2b0a59d0
commit 0bada8603d
2 changed files with 20 additions and 3 deletions

View File

@ -46,6 +46,7 @@ static char sccsid[] = "@(#)tmpfile.c 8.1 (Berkeley) 6/4/93";
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <paths.h> #include <paths.h>
#include "un-namespace.h" #include "un-namespace.h"
@ -57,10 +58,17 @@ tmpfile()
FILE *fp; FILE *fp;
int fd, sverrno; int fd, sverrno;
#define TRAILER "tmp.XXXXXX" #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); tmpdir = getenv("TMPDIR");
(void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); 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); sigfillset(&set);
(void)_sigprocmask(SIG_BLOCK, &set, &oset); (void)_sigprocmask(SIG_BLOCK, &set, &oset);
@ -69,6 +77,8 @@ tmpfile()
if (fd != -1) if (fd != -1)
(void)unlink(buf); (void)unlink(buf);
free(buf);
(void)_sigprocmask(SIG_SETMASK, &oset, NULL); (void)_sigprocmask(SIG_SETMASK, &oset, NULL);
if (fd == -1) if (fd == -1)

View File

@ -67,6 +67,13 @@ returns, causing the file to be automatically deleted when the last
reference to it is closed. reference to it is closed.
The file is opened with the access value The file is opened with the access value
.Ql w+ . .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 .Pp
The The
.Fn tmpnam .Fn tmpnam