Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #include <stdio.h> |
2 | #include <fcntl.h> | ||||
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 3 | #include "stdio_impl.h" |
4 | |||||
Rich Felker | a88edbe | 2011-03-29 08:37:57 -0400 | [diff] [blame] | 5 | #define MAXTRIES 100 |
6 | |||||
Rich Felker | 2fe6579 | 2014-05-27 00:44:23 -0400 | [diff] [blame] | 7 | char *__randname(char *); |
8 | |||||
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 9 | FILE *tmpfile(void) |
10 | { | ||||
Rich Felker | 2fe6579 | 2014-05-27 00:44:23 -0400 | [diff] [blame] | 11 | char s[] = "/tmp/tmpfile_XXXXXX"; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 12 | int fd; |
13 | FILE *f; | ||||
Rich Felker | a88edbe | 2011-03-29 08:37:57 -0400 | [diff] [blame] | 14 | int try; |
15 | for (try=0; try<MAXTRIES; try++) { | ||||
Rich Felker | 2fe6579 | 2014-05-27 00:44:23 -0400 | [diff] [blame] | 16 | __randname(s+13); |
Rich Felker | 594c827 | 2014-05-24 22:54:05 -0400 | [diff] [blame] | 17 | fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 18 | if (fd >= 0) { |
Rich Felker | dd5f50d | 2014-05-29 21:01:32 -0400 | [diff] [blame] | 19 | #ifdef SYS_unlink |
Rich Felker | eb0e8fa | 2011-04-17 16:32:15 -0400 | [diff] [blame] | 20 | __syscall(SYS_unlink, s); |
Rich Felker | dd5f50d | 2014-05-29 21:01:32 -0400 | [diff] [blame] | 21 | #else |
22 | __syscall(SYS_unlinkat, AT_FDCWD, s, 0); | ||||
23 | #endif | ||||
Rich Felker | 60158bf | 2014-06-06 03:17:47 -0400 | [diff] [blame] | 24 | f = __fdopen(fd, "w+"); |
25 | if (!f) __syscall(SYS_close, fd); | ||||
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 26 | return f; |
27 | } | ||||
28 | } | ||||
Rich Felker | a88edbe | 2011-03-29 08:37:57 -0400 | [diff] [blame] | 29 | return 0; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 30 | } |
31 | |||||
32 | LFS64(tmpfile); |