xref: /csrg-svn/lib/libc/stdio/tmpfile.c (revision 49892)
146111Sbostic /*-
246111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346111Sbostic  * All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*49892Sbostic static char sccsid[] = "@(#)tmpfile.c	5.4 (Berkeley) 05/27/91";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546559Sbostic #include <sys/types.h>
1646559Sbostic #include <signal.h>
1746611Sbostic #include <unistd.h>
1846559Sbostic #include <errno.h>
1946111Sbostic #include <stdio.h>
2046559Sbostic #include <paths.h>
2146111Sbostic 
2246111Sbostic FILE *
2346111Sbostic tmpfile()
2446111Sbostic {
2546559Sbostic 	sigset_t set, oset;
2646111Sbostic 	FILE *fp;
2746559Sbostic 	int fd, sverrno;
2846559Sbostic #define	TRAILER	"tmp.XXXXXX"
2946559Sbostic 	char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
3046111Sbostic 
3146559Sbostic 	bcopy(_PATH_TMP, buf, sizeof(_PATH_TMP) - 1);
3246559Sbostic 	bcopy(TRAILER, buf + sizeof(_PATH_TMP) - 1, sizeof(TRAILER));
3346559Sbostic 
34*49892Sbostic 	sigfillset(&set);
3546559Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
3646559Sbostic 
3746559Sbostic 	fd = mkstemp(buf);
3846559Sbostic 	if (fd != -1)
3946559Sbostic 		(void)unlink(buf);
4046559Sbostic 
41*49892Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
4246559Sbostic 
4346559Sbostic 	if (fd == -1)
44*49892Sbostic 		return (NULL);
4546559Sbostic 
4646559Sbostic 	if (!(fp = fdopen(fd, "w+"))) {
4746559Sbostic 		sverrno = errno;
4846559Sbostic 		(void)close(fd);
4946559Sbostic 		errno = sverrno;
50*49892Sbostic 		return (NULL);
5146559Sbostic 	}
52*49892Sbostic 	return (fp);
5346111Sbostic }
54