1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 5.4 (Berkeley) 05/27/91"; 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 * 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 bcopy(_PATH_TMP, buf, sizeof(_PATH_TMP) - 1); 32 bcopy(TRAILER, buf + sizeof(_PATH_TMP) - 1, 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+"))) { 47 sverrno = errno; 48 (void)close(fd); 49 errno = sverrno; 50 return (NULL); 51 } 52 return (fp); 53 } 54