tmpnam.c 655 字节
Newer Older
R
Rich Felker 已提交
1
#include <stdio.h>
2 3 4
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
5
#include <string.h>
6
#include <stdlib.h>
W
w00349915 已提交
7 8
#include <unsupported_api.h>

9
#include "syscall.h"
10
#include "kstat.h"
11 12

#define MAXTRIES 100
R
Rich Felker 已提交
13

14 15 16 17 18 19
char *tmpnam(char *buf)
{
	static char internal[L_tmpnam];
	char s[] = "/tmp/tmpnam_XXXXXX";
	int try;
	int r;
W
w00349915 已提交
20
	unsupported_api(__FUNCTION__);
21 22
	for (try=0; try<MAXTRIES; try++) {
		__randname(s+12);
23
#ifdef SYS_lstat
24
		r = __syscall(SYS_lstat, s, &(struct kstat){0});
25 26
#else
		r = __syscall(SYS_fstatat, AT_FDCWD, s,
27
			&(struct kstat){0}, AT_SYMLINK_NOFOLLOW);
28
#endif
29 30 31
		if (r == -ENOENT) return strcpy(buf ? buf : internal, s);
	}
	return 0;
R
Rich Felker 已提交
32
}