--- ray/src/common/paths.c 2003/06/08 12:01:52 2.1 +++ ray/src/common/paths.c 2003/07/01 21:21:40 2.4 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: paths.c,v 2.1 2003/06/08 12:01:52 schorsch Exp $"; +static const char RCSid[] = "$Id: paths.c,v 2.4 2003/07/01 21:21:40 greg Exp $"; #endif /* * Find a writeable tempfile directory. @@ -21,7 +21,7 @@ static const char *defaultpaths[] = DEFAULT_TEMPDIRS; char * temp_directory(char *s, size_t len) { - static char td[PATH_MAX] = "\0"; /* remember */ + static char td[PATH_MAX]; /* remember */ char * ts = NULL; int i = 0; @@ -75,15 +75,23 @@ append_filepath(char *s1, char *s2, size_t len) char *s; siz = strlen(s1); - /* XXX siz > len is an error */ - while (siz > 1 && ISDIRSEP(s1[siz-1])) { - s1[siz-1] = '\0'; - siz--; + if (siz > 0) { + /* XXX siz > len is an error */ + while (siz > 1 && ISDIRSEP(s1[siz-1])) { + s1[siz-1] = '\0'; + siz--; + } + if (siz+1 <= len) { + s1[siz] = DIRSEP; + siz++; + } + } else if (len >= 2) { /* first path empty */ + s1[0] = CURDIR; + s1[1] = DIRSEP; + siz = 2; + } else { + return NULL; } - if (siz+1 <= len) { - s1[siz] = DIRSEP; - siz++; - } while (ISDIRSEP(s2[0])) { s2++; } @@ -96,7 +104,7 @@ append_filepath(char *s1, char *s2, size_t len) static char * prepare_tmpname(char *s, size_t len, char *templ) { - static char lp[PATH_MAX] = "\0"; /* remember what we found last time */ + static char lp[PATH_MAX] = "\0"; char *ts = NULL; if (s == NULL) { /* return our static string */ @@ -135,7 +143,7 @@ temp_filename(char *s, size_t len, char *templ) /* WARNING: On Windows, there's a maximum of 27 unique names within one process for the same template. */ int -temp_file(char *s, size_t len, char *templ) +temp_fd(char *s, size_t len, char *templ) { char *ts = NULL; @@ -144,23 +152,33 @@ temp_file(char *s, size_t len, char *templ) #ifdef _WIN32 ts = mktemp(ts); if (ts == NULL) return -1; - return fopen(ts, "r+b"); + return open(ts, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); #else return mkstemp(ts); #endif } +/* As above, but returns a file pointer instead of a descriptor */ +FILE * +temp_fp(char *s, size_t len, char *templ) +{ + int fd = temp_fd(s, len, templ); + if (fd < 0) return NULL; + return fdopen(fd, "w+"); +} -#ifdef MODULE_TEST + +#ifdef TEST_MODULE int main() { static char p[PATH_MAX] = "\0"; char * pp, *qq = NULL; pp = temp_directory(p, sizeof(p)); - printf("%s\n", pp); + qq = temp_filename(pp, sizeof(p), "//something/else_XXXXXX"); printf("%s\n", qq); + qq = temp_filename(pp, sizeof(p), NULL); printf("%s\n", qq); }