Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 10 Apr 2000 03:50:04 -0700 (PDT)
From:      "Lachlan O'Dea" <lodea@vet.com.au>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/16924: tmpfile(3) ignores TMPDIR and always uses /tmp
Message-ID:  <200004101050.DAA47116@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/16924; it has been noted by GNATS.

From: "Lachlan O'Dea" <lodea@vet.com.au>
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 <stdio.h>
  #include <string.h>
  #include <paths.h>
 +#include <stdlib.h>
  
  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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200004101050.DAA47116>