OLD | NEW |
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <fcntl.h> | 2 #include <fcntl.h> |
3 #include <errno.h> | 3 #include <errno.h> |
4 #include <sys/stat.h> | 4 #include <sys/stat.h> |
5 #include <limits.h> | 5 #include <limits.h> |
6 #include <string.h> | 6 #include <string.h> |
7 #include "syscall.h" | 7 #include "syscall.h" |
8 | 8 |
9 #define MAXTRIES 100 | 9 #define MAXTRIES 100 |
10 | 10 |
11 char *__randname(char *); | 11 char* __randname(char*); |
12 | 12 |
13 char *tempnam(const char *dir, const char *pfx) | 13 char* tempnam(const char* dir, const char* pfx) { |
14 { | 14 char s[PATH_MAX]; |
15 » char s[PATH_MAX]; | 15 size_t l, dl, pl; |
16 » size_t l, dl, pl; | 16 int try |
17 » int try; | 17 ; |
18 » int r; | 18 int r; |
19 | 19 |
20 » if (!dir) dir = P_tmpdir; | 20 if (!dir) |
21 » if (!pfx) pfx = "temp"; | 21 dir = P_tmpdir; |
| 22 if (!pfx) |
| 23 pfx = "temp"; |
22 | 24 |
23 » dl = strlen(dir); | 25 dl = strlen(dir); |
24 » pl = strlen(pfx); | 26 pl = strlen(pfx); |
25 » l = dl + 1 + pl + 1 + 6; | 27 l = dl + 1 + pl + 1 + 6; |
26 | 28 |
27 » if (l >= PATH_MAX) { | 29 if (l >= PATH_MAX) { |
28 » » errno = ENAMETOOLONG; | 30 errno = ENAMETOOLONG; |
29 » » return 0; | 31 return 0; |
30 » } | 32 } |
31 | 33 |
32 » memcpy(s, dir, dl); | 34 memcpy(s, dir, dl); |
33 » s[dl] = '/'; | 35 s[dl] = '/'; |
34 » memcpy(s+dl+1, pfx, pl); | 36 memcpy(s + dl + 1, pfx, pl); |
35 » s[dl+1+pl] = '_'; | 37 s[dl + 1 + pl] = '_'; |
36 » s[l] = 0; | 38 s[l] = 0; |
37 | 39 |
38 » for (try=0; try<MAXTRIES; try++) { | 40 for (try = 0; try < MAXTRIES; try ++) { |
39 » » __randname(s+l-6); | 41 __randname(s + l - 6); |
40 #ifdef SYS_lstat | 42 #ifdef SYS_lstat |
41 » » r = __syscall(SYS_lstat, s, &(struct stat){0}); | 43 r = __syscall(SYS_lstat, s, &(struct stat){0}); |
42 #else | 44 #else |
43 » » r = __syscall(SYS_fstatat, AT_FDCWD, s, | 45 r = __syscall(SYS_fstatat, AT_FDCWD, s, &(struct stat){0}, |
44 » » » &(struct stat){0}, AT_SYMLINK_NOFOLLOW); | 46 AT_SYMLINK_NOFOLLOW); |
45 #endif | 47 #endif |
46 » » if (r == -ENOENT) return strdup(s); | 48 if (r == -ENOENT) |
47 » } | 49 return strdup(s); |
48 » return 0; | 50 } |
| 51 return 0; |
49 } | 52 } |
OLD | NEW |