1a3050234Sbostic /*-
2*82f2451bSbostic * Copyright (c) 1990, 1993
3*82f2451bSbostic * The Regents of the University of California. All rights reserved.
4a3050234Sbostic *
5a3050234Sbostic * This code is derived from software contributed to Berkeley by
6a3050234Sbostic * Chris Torek.
7a3050234Sbostic *
8a3050234Sbostic * %sccs.include.redist.c%
9a3050234Sbostic */
10a3050234Sbostic
11a3050234Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*82f2451bSbostic static char sccsid[] = "@(#)tmpfile.c 8.1 (Berkeley) 06/04/93";
13a3050234Sbostic #endif /* LIBC_SCCS and not lint */
14a3050234Sbostic
1547d96270Sbostic #include <sys/types.h>
1647d96270Sbostic #include <signal.h>
17b05ba1a0Sbostic #include <unistd.h>
18a3050234Sbostic #include <errno.h>
1947d96270Sbostic #include <stdio.h>
2047d96270Sbostic #include <paths.h>
21a3050234Sbostic
22a3050234Sbostic FILE *
tmpfile()23a3050234Sbostic tmpfile()
24a3050234Sbostic {
2547d96270Sbostic sigset_t set, oset;
26a3050234Sbostic FILE *fp;
2747d96270Sbostic int fd, sverrno;
2847d96270Sbostic #define TRAILER "tmp.XXXXXX"
2947d96270Sbostic char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
30a3050234Sbostic
310c18da93Storek (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
320c18da93Storek (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
3347d96270Sbostic
34d49893b2Sbostic sigfillset(&set);
3547d96270Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset);
3647d96270Sbostic
3747d96270Sbostic fd = mkstemp(buf);
3847d96270Sbostic if (fd != -1)
3947d96270Sbostic (void)unlink(buf);
4047d96270Sbostic
41d49893b2Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL);
4247d96270Sbostic
4347d96270Sbostic if (fd == -1)
44a3050234Sbostic return (NULL);
4547d96270Sbostic
460c18da93Storek if ((fp = fdopen(fd, "w+")) == NULL) {
4747d96270Sbostic sverrno = errno;
4847d96270Sbostic (void)close(fd);
4947d96270Sbostic errno = sverrno;
5047d96270Sbostic return (NULL);
5147d96270Sbostic }
52a3050234Sbostic return (fp);
53a3050234Sbostic }
54