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) | |
594acd1e | 12 | static char sccsid[] = "@(#)tmpfile.c 5.5 (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 | ||
594acd1e CT |
31 | (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1); |
32 | (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); | |
96ea859a | 33 | |
24a28b45 | 34 | sigfillset(&set); |
96ea859a KB |
35 | (void)sigprocmask(SIG_BLOCK, &set, &oset); |
36 | ||
37 | fd = mkstemp(buf); | |
38 | if (fd != -1) | |
39 | (void)unlink(buf); | |
40 | ||
24a28b45 | 41 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); |
96ea859a KB |
42 | |
43 | if (fd == -1) | |
24a28b45 | 44 | return (NULL); |
96ea859a | 45 | |
594acd1e | 46 | if ((fp = fdopen(fd, "w+")) == NULL) { |
96ea859a KB |
47 | sverrno = errno; |
48 | (void)close(fd); | |
49 | errno = sverrno; | |
24a28b45 | 50 | return (NULL); |
96ea859a | 51 | } |
24a28b45 | 52 | return (fp); |
c9be6cfe | 53 | } |