| 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 "syscall.h" |
| 8 | |
| 9 | #define MAXTRIES 100 |
| 10 | |
| 11 | char *tmpnam(char *buf) |
| 12 | { |
| 13 | 	static char internal[L_tmpnam]; |
| 14 | 	char s[] = "/tmp/tmpnam_XXXXXX"; |
| 15 | 	int try; |
| 16 | 	int r; |
| 17 | 	for (try=0; try<MAXTRIES; try++) { |
| 18 | 		__randname(s+12); |
| 19 | #ifdef SYS_readlink |
| 20 | 		r = __syscall(SYS_readlink, s, (char[1]){0}, 1); |
| 21 | #else |
| 22 | 		r = __syscall(SYS_readlinkat, AT_FDCWD, s, (char[1]){0}, 1); |
| 23 | #endif |
| 24 | 		if (r == -ENOENT) return strcpy(buf ? buf : internal, s); |
| 25 | 	} |
| 26 | 	return 0; |
| 27 | } |