| 21 |
|
char * |
| 22 |
|
temp_directory(char *s, size_t len) |
| 23 |
|
{ |
| 24 |
< |
static char td[PATH_MAX] = "\0"; /* remember */ |
| 24 |
> |
static char td[PATH_MAX]; /* remember */ |
| 25 |
|
char * ts = NULL; |
| 26 |
|
int i = 0; |
| 27 |
|
|
| 75 |
|
char *s; |
| 76 |
|
|
| 77 |
|
siz = strlen(s1); |
| 78 |
< |
/* XXX siz > len is an error */ |
| 79 |
< |
while (siz > 1 && ISDIRSEP(s1[siz-1])) { |
| 80 |
< |
s1[siz-1] = '\0'; |
| 81 |
< |
siz--; |
| 78 |
> |
if (siz > 0) { |
| 79 |
> |
/* XXX siz > len is an error */ |
| 80 |
> |
while (siz > 1 && ISDIRSEP(s1[siz-1])) { |
| 81 |
> |
s1[siz-1] = '\0'; |
| 82 |
> |
siz--; |
| 83 |
> |
} |
| 84 |
> |
if (siz+1 <= len) { |
| 85 |
> |
s1[siz] = DIRSEP; |
| 86 |
> |
siz++; |
| 87 |
> |
} |
| 88 |
> |
} else if (len >= 2) { /* first path empty */ |
| 89 |
> |
s1[0] = CURDIR; |
| 90 |
> |
s1[1] = DIRSEP; |
| 91 |
> |
siz = 2; |
| 92 |
> |
} else { |
| 93 |
> |
return NULL; |
| 94 |
|
} |
| 83 |
– |
if (siz+1 <= len) { |
| 84 |
– |
s1[siz] = DIRSEP; |
| 85 |
– |
siz++; |
| 86 |
– |
} |
| 95 |
|
while (ISDIRSEP(s2[0])) { |
| 96 |
|
s2++; |
| 97 |
|
} |
| 104 |
|
static char * |
| 105 |
|
prepare_tmpname(char *s, size_t len, char *templ) |
| 106 |
|
{ |
| 107 |
< |
static char lp[PATH_MAX] = "\0"; /* remember what we found last time */ |
| 107 |
> |
static char lp[PATH_MAX] = "\0"; |
| 108 |
|
char *ts = NULL; |
| 109 |
|
|
| 110 |
|
if (s == NULL) { /* return our static string */ |
| 143 |
|
/* WARNING: On Windows, there's a maximum of 27 unique names within |
| 144 |
|
one process for the same template. */ |
| 145 |
|
int |
| 146 |
< |
temp_file(char *s, size_t len, char *templ) |
| 146 |
> |
temp_fd(char *s, size_t len, char *templ) |
| 147 |
|
{ |
| 148 |
|
char *ts = NULL; |
| 149 |
|
|
| 152 |
|
#ifdef _WIN32 |
| 153 |
|
ts = mktemp(ts); |
| 154 |
|
if (ts == NULL) return -1; |
| 155 |
< |
return fopen(ts, "r+b"); |
| 155 |
> |
return open(ts, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
| 156 |
|
#else |
| 157 |
|
return mkstemp(ts); |
| 158 |
|
#endif |
| 159 |
|
} |
| 160 |
|
|
| 161 |
+ |
/* As above, but returns a file pointer instead of a descriptor */ |
| 162 |
+ |
FILE * |
| 163 |
+ |
temp_fp(char *s, size_t len, char *templ) |
| 164 |
+ |
{ |
| 165 |
+ |
int fd = temp_fd(s, len, templ); |
| 166 |
+ |
if (fd < 0) return NULL; |
| 167 |
+ |
return fdopen(fd, "w+"); |
| 168 |
+ |
} |
| 169 |
|
|
| 170 |
< |
#ifdef MODULE_TEST |
| 170 |
> |
|
| 171 |
> |
#ifdef TEST_MODULE |
| 172 |
|
int main() |
| 173 |
|
{ |
| 174 |
|
static char p[PATH_MAX] = "\0"; |
| 175 |
|
char * pp, *qq = NULL; |
| 176 |
|
pp = temp_directory(p, sizeof(p)); |
| 160 |
– |
|
| 177 |
|
printf("%s\n", pp); |
| 178 |
+ |
|
| 179 |
|
qq = temp_filename(pp, sizeof(p), "//something/else_XXXXXX"); |
| 180 |
|
printf("%s\n", qq); |
| 181 |
+ |
|
| 182 |
|
qq = temp_filename(pp, sizeof(p), NULL); |
| 183 |
|
printf("%s\n", qq); |
| 184 |
|
} |