xref: /third_party/musl/src/stdio/tmpfile.c (revision 570af302)
1#include <stdio.h>
2#include <fcntl.h>
3#include <stdlib.h>
4#include <unsupported_api.h>
5#include "stdio_impl.h"
6
7#define MAXTRIES 100
8
9FILE *tmpfile(void)
10{
11	char s[] = "/data/local/tmp/tmpfile_XXXXXX";
12	int fd;
13	FILE *f;
14	int try;
15	UNSUPPORTED_API_VOID(LITEOS_A);
16	for (try=0; try<MAXTRIES; try++) {
17		__randname(s+13);
18		fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
19		if (fd >= 0) {
20#ifdef SYS_unlink
21			__syscall(SYS_unlink, s);
22#else
23			__syscall(SYS_unlinkat, AT_FDCWD, s, 0);
24#endif
25			f = __fdopen(fd, "w+");
26			if (!f) __syscall(SYS_close, fd);
27			return f;
28		}
29	}
30	return 0;
31}
32
33weak_alias(tmpfile, tmpfile64);
34