xref: /third_party/musl/src/stdio/tmpnam.c (revision 570af302)
1#include <stdio.h>
2#include <fcntl.h>
3#include <errno.h>
4#include <sys/stat.h>
5#include <string.h>
6#include <stdlib.h>
7#include <unsupported_api.h>
8#include "syscall.h"
9
10#define MAXTRIES 100
11
12char *tmpnam(char *buf)
13{
14	static char internal[L_tmpnam];
15	char s[] = "/tmp/tmpnam_XXXXXX";
16	int try;
17	int r;
18	UNSUPPORTED_API_VOID(LITEOS_A);
19	for (try=0; try<MAXTRIES; try++) {
20		__randname(s+12);
21#ifdef SYS_readlink
22		r = __syscall(SYS_readlink, s, (char[1]){0}, 1);
23#else
24		r = __syscall(SYS_readlinkat, AT_FDCWD, s, (char[1]){0}, 1);
25#endif
26		if (r == -ENOENT) return strcpy(buf ? buf : internal, s);
27	}
28	return 0;
29}
30