1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)tmpfile.c 8.1 (Berkeley) 06/04/93";
13 #endif /* LIBC_SCCS and not lint */
14
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <paths.h>
21
22 FILE *
tmpfile()23 tmpfile()
24 {
25 sigset_t set, oset;
26 FILE *fp;
27 int fd, sverrno;
28 #define TRAILER "tmp.XXXXXX"
29 char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
30
31 (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
32 (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
33
34 sigfillset(&set);
35 (void)sigprocmask(SIG_BLOCK, &set, &oset);
36
37 fd = mkstemp(buf);
38 if (fd != -1)
39 (void)unlink(buf);
40
41 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
42
43 if (fd == -1)
44 return (NULL);
45
46 if ((fp = fdopen(fd, "w+")) == NULL) {
47 sverrno = errno;
48 (void)close(fd);
49 errno = sverrno;
50 return (NULL);
51 }
52 return (fp);
53 }
54