1#include <fcntl.h>
2#include <stdio.h>
3#include <unistd.h>
4
5#include "stdio_impl.h"
6
7#define MAXTRIES 100
8
9char* __randname(char*);
10
11FILE* tmpfile(void) {
12    char s[] = "/tmp/tmpfile_XXXXXX";
13    int fd;
14    FILE* f;
15    int try
16        ;
17    for (try = 0; try < MAXTRIES; try ++) {
18        __randname(s + 13);
19        fd = open(s, O_RDWR | O_CREAT | O_EXCL, 0600);
20        if (fd >= 0) {
21            unlink(s);
22            f = __fdopen(fd, "w+");
23            if (!f)
24                close(fd);
25            return f;
26        }
27    }
28    return 0;
29}
30