blob: 525090aa066d49cc20a4a762b17a4d54a3ec1b4f [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdio.h>
2#include <fcntl.h>
Rich Felker0b44a032011-02-12 00:22:29 -05003#include "stdio_impl.h"
4
Rich Felkera88edbe2011-03-29 08:37:57 -04005#define MAXTRIES 100
6
Rich Felker2fe65792014-05-27 00:44:23 -04007char *__randname(char *);
8
Rich Felker0b44a032011-02-12 00:22:29 -05009FILE *tmpfile(void)
10{
Rich Felker2fe65792014-05-27 00:44:23 -040011 char s[] = "/tmp/tmpfile_XXXXXX";
Rich Felker0b44a032011-02-12 00:22:29 -050012 int fd;
13 FILE *f;
Rich Felkera88edbe2011-03-29 08:37:57 -040014 int try;
15 for (try=0; try<MAXTRIES; try++) {
Rich Felker2fe65792014-05-27 00:44:23 -040016 __randname(s+13);
Rich Felker594c8272014-05-24 22:54:05 -040017 fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
Rich Felker0b44a032011-02-12 00:22:29 -050018 if (fd >= 0) {
Rich Felkerdd5f50d2014-05-29 21:01:32 -040019#ifdef SYS_unlink
Rich Felkereb0e8fa2011-04-17 16:32:15 -040020 __syscall(SYS_unlink, s);
Rich Felkerdd5f50d2014-05-29 21:01:32 -040021#else
22 __syscall(SYS_unlinkat, AT_FDCWD, s, 0);
23#endif
Rich Felker60158bf2014-06-06 03:17:47 -040024 f = __fdopen(fd, "w+");
25 if (!f) __syscall(SYS_close, fd);
Rich Felker0b44a032011-02-12 00:22:29 -050026 return f;
27 }
28 }
Rich Felkera88edbe2011-03-29 08:37:57 -040029 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -050030}
31
32LFS64(tmpfile);