Commit | Line | Data |
---|---|---|
c9be6cfe KB |
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) | |
cb631443 | 12 | static char sccsid[] = "@(#)tmpfile.c 5.3 (Berkeley) %G%"; |
c9be6cfe KB |
13 | #endif /* LIBC_SCCS and not lint */ |
14 | ||
96ea859a KB |
15 | #include <sys/types.h> |
16 | #include <signal.h> | |
cb631443 | 17 | #include <unistd.h> |
c9be6cfe | 18 | #include <errno.h> |
96ea859a KB |
19 | #include <stdio.h> |
20 | #include <paths.h> | |
c9be6cfe KB |
21 | |
22 | FILE * | |
23 | tmpfile() | |
24 | { | |
96ea859a | 25 | sigset_t set, oset; |
c9be6cfe | 26 | FILE *fp; |
96ea859a KB |
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 | sigemptyset(&set); | |
35 | sigaddset(&set, SIGHUP); | |
36 | sigaddset(&set, SIGINT); | |
37 | sigaddset(&set, SIGQUIT); | |
38 | sigaddset(&set, SIGTERM); | |
39 | (void)sigprocmask(SIG_BLOCK, &set, &oset); | |
40 | ||
41 | fd = mkstemp(buf); | |
42 | if (fd != -1) | |
43 | (void)unlink(buf); | |
44 | ||
45 | (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); | |
46 | ||
47 | if (fd == -1) | |
48 | return(NULL); | |
49 | ||
50 | if (!(fp = fdopen(fd, "w+"))) { | |
51 | sverrno = errno; | |
52 | (void)close(fd); | |
53 | errno = sverrno; | |
54 | return(NULL); | |
55 | } | |
56 | return(fp); | |
c9be6cfe | 57 | } |