Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 05 Jul 2001 23:02:37 -0400 (EDT)
From:      Mike Heffner <mheffner@novacoxmail.com>
To:        FreeBSD-audit <FreeBSD-audit@freebsd.org>
Subject:   Teach tmpfile(3) about TMPDIR
Message-ID:  <XFMail.20010705230237.mheffner@novacoxmail.com>

next in thread | raw e-mail | index | archive | help
This message is in MIME format
--_=XFMail.1.5.0.FreeBSD:20010705230237:33483=_
Content-Type: text/plain; charset=us-ascii

This patch was inspired by bin/16924. It makes tmpfile(3) attempt to use the
TMPDIR environment variable, falling back to '/tmp'. The patch is slightly
different from the one in the PR, but I think this might be a little cleaner
way of doing it. Note, the patch adds an extra failure point--the case of
malloc(3) failing. However, since fdopen(3) is specified as a fail point for
tmpfile(3) and fdopen(3) can also fail due to malloc(3), I don't think this is a
problem.

Please review, and see the PR for discussion.

Mike

-- 
  Mike Heffner         <mheffner@[acm.]vt.edu>
  Fredericksburg, VA       <mikeh@FreeBSD.org>


Also at: http://people.freebsd.org/~mikeh/diffs/tmpfile.diff


Index: tmpfile.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/tmpfile.c,v
retrieving revision 1.5
diff -u -r1.5 tmpfile.c
--- tmpfile.c   2001/01/24 13:00:47     1.5
+++ tmpfile.c   2001/07/06 02:37:52
@@ -46,6 +46,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <paths.h>
 #include "un-namespace.h"
@@ -57,17 +58,30 @@
        FILE *fp;
        int fd, sverrno;
 #define        TRAILER "tmp.XXXXXX"
-       char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
+       char *buf;
+       const char *tmpdir;
+       size_t tmpdirlen;
 
-       (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;
+       tmpdirlen = strlen(tmpdir);
 
+       buf = malloc(tmpdirlen + (tmpdir[tmpdirlen - 1] == '/') ? 0 : 1 +
+           sizeof(TRAILER));
+       if (buf == NULL)
+               return NULL;
+       (void)sprintf(buf, "%s%s%s", tmpdir,
+           (tmpdir[tmpdirlen - 1] == '/') ? "" : "/", TRAILER);
+
        sigfillset(&set);
        (void)_sigprocmask(SIG_BLOCK, &set, &oset);
 
        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.7
diff -u -r1.7 tmpnam.3
--- tmpnam.3    2001/02/26 13:21:16     1.7
+++ tmpnam.3    2001/07/06 02:37:52
@@ -67,6 +67,12 @@
 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


--_=XFMail.1.5.0.FreeBSD:20010705230237:33483=_
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (FreeBSD)
Comment: For info see http://www.gnupg.org

iD8DBQE7RSpMFokZQs3sv5kRAkdJAJwO4j6b4vgJH9DsLvnIwuhA9Ec/8wCgmXIF
FDH4AKEpTYg9Wy7AOl9fnrk=
=ziRi
-----END PGP SIGNATURE-----

--_=XFMail.1.5.0.FreeBSD:20010705230237:33483=_--
End of MIME message

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




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