From owner-freebsd-bugs Mon Apr 10 3:50: 8 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id 5440237B76D for ; Mon, 10 Apr 2000 03:50:05 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id DAA47116; Mon, 10 Apr 2000 03:50:04 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Date: Mon, 10 Apr 2000 03:50:04 -0700 (PDT) Message-Id: <200004101050.DAA47116@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org Cc: From: "Lachlan O'Dea" Subject: Re: bin/16924: tmpfile(3) ignores TMPDIR and always uses /tmp Reply-To: "Lachlan O'Dea" Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org The following reply was made to PR bin/16924; it has been noted by GNATS. From: "Lachlan O'Dea" To: freebsd-gnats-submit@FreeBSD.org Cc: Subject: Re: bin/16924: tmpfile(3) ignores TMPDIR and always uses /tmp Date: Mon, 10 Apr 2000 20:44:54 +1000 On Thu, Apr 06, 2000 at 09:56:33AM -0400, Daniel Hagan wrote: > The original code is incorrect anyway since it doesn't handle malloc > errors. So based on the discussion, all I have to do is return NULL if malloc returns NULL. The attached diff does this. I also added a paragraph to the man page. I didn't add anything to the ERRORS section, as it states that errno can be set to any of the errors returned by fdopen(3), which in turn can set errno to any value specified by malloc(3). Index: tmpfile.c =================================================================== RCS file: /home/ncvs/src/lib/libc/stdio/tmpfile.c,v retrieving revision 1.4 diff -u -r1.4 tmpfile.c --- tmpfile.c 2000/01/27 23:06:46 1.4 +++ tmpfile.c 2000/04/10 10:34:49 @@ -47,6 +47,7 @@ #include #include #include +#include FILE * tmpfile() @@ -55,10 +56,28 @@ FILE *fp; int fd, sverrno; #define TRAILER "tmp.XXXXXX" - char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)]; + char *buf; + char *envtmpdir; + int envtmpdirlen; - (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1); - (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); + if ((envtmpdir = getenv("TMPDIR")) != NULL) + { + envtmpdirlen = strlen(envtmpdir); + buf = malloc(envtmpdirlen + 1 + sizeof(TRAILER)); + if (buf == NULL) + return NULL; + (void)memcpy(buf, envtmpdir, envtmpdirlen); + buf[envtmpdirlen] = '/'; + (void)memcpy(buf + envtmpdirlen + 1, TRAILER, sizeof(TRAILER)); + } + else + { + buf = malloc(sizeof(_PATH_TMP) + sizeof(TRAILER) - 1); + if (buf == NULL) + return NULL; + (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1); + (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); + } sigfillset(&set); (void)sigprocmask(SIG_BLOCK, &set, &oset); @@ -66,6 +85,8 @@ fd = mkstemp(buf); if (fd != -1) (void)unlink(buf); + + free(buf); (void)sigprocmask(SIG_SETMASK, &oset, NULL); Index: tmpnam.3 =================================================================== RCS file: /home/ncvs/src/lib/libc/stdio/tmpnam.3,v retrieving revision 1.5 diff -u -r1.5 tmpnam.3 --- tmpnam.3 2000/01/09 08:54:03 1.5 +++ tmpnam.3 2000/04/10 10:34:50 @@ -66,6 +66,16 @@ The file is opened with the access value .Ql w+ . .Pp +The file is initially created in the directory specified by the environment +variable +.Ev TMPDIR +(if set), or +.Pa /tmp +by default. +As the file is immediately unlinked, the only effect of +.Ev TMPDIR +is to specify which partition the file is created on. +.Pp The .Fn tmpnam function To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message