Back to index

radiance  4R0+20100331
Defines | Functions | Variables
paths.c File Reference
#include "copyright.h"
#include "paths.h"

Go to the source code of this file.

Defines

#define TEMPFILE_TEMPLATE   "rtXXXXXX"

Functions

char * temp_directory (char *s, size_t len)
char * append_filepath (char *s1, char *s2, size_t len)
static char * prepare_tmpname (char *s, size_t len, char *templ)
char * temp_filename (char *s, size_t len, char *templ)
int temp_fd (char *s, size_t len, char *templ)
FILE * temp_fp (char *s, size_t len, char *templ)

Variables

static const char RCSid [] = "$Id: paths.c,v 2.4 2003/07/01 21:21:40 greg Exp $"
static const char * defaultpaths [] = DEFAULT_TEMPDIRS

Define Documentation

#define TEMPFILE_TEMPLATE   "rtXXXXXX"

Definition at line 15 of file paths.c.


Function Documentation

char* append_filepath ( char *  s1,
char *  s2,
size_t  len 
)

Definition at line 72 of file paths.c.

{
       size_t siz;
       char *s;

       siz = strlen(s1);
       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;
       }
       while (ISDIRSEP(s2[0])) {
              s2++;
       }
       s = strncat(s1, s2, len-siz);
       return s;
}

Here is the caller graph for this function:

static char* prepare_tmpname ( char *  s,
size_t  len,
char *  templ 
) [static]

Definition at line 105 of file paths.c.

{
       static char lp[PATH_MAX] = "\0";
       char *ts = NULL;

       if (s == NULL) { /* return our static string */
              s = lp;
              len = sizeof(lp);
       }

       ts = temp_directory(s, len);
       if (ts == NULL) { return NULL; }
       ts = append_filepath(ts, templ != NULL ? templ : TEMPFILE_TEMPLATE, len);
       return ts;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* temp_directory ( char *  s,
size_t  len 
)

Definition at line 22 of file paths.c.

{
       static char td[PATH_MAX]; /* remember */
       char * ts = NULL;
       int i = 0;
       
       if (td[0] != '\0') { /* we already have one */
              if (s == NULL) { return td; }
              strncpy(s, td, len);
              s[len-1] = '\0';
              return s;
       }
       /* check where TMP and TEMP point to */
       ts = getenv("TMP");
       if (ts != NULL && access(ts, W_OK) == 0) {
              strncpy(td, ts, sizeof(td));
              td[sizeof(td)-1] = '\0';
       }
       if (ts == NULL) {
              ts = getenv("TEMP");
              if (ts != NULL && access(ts, W_OK) == 0) {
                     strncpy(td, ts, sizeof(td));
                     td[sizeof(td)-1] = '\0';
              }
       }
       /* check the platform specific default paths in order */
       while (ts == NULL) {
              if (defaultpaths[i] == NULL) {
                     break;
              }
              if (access(defaultpaths[i], W_OK) == 0) {
                     ts = strncpy(td, defaultpaths[i], sizeof(td));
                     td[sizeof(td)-1] = '\0';
                     break;
              }
              i++;
       }
       /* we found something */
       if (ts != NULL) {
              if (s == NULL) { return td; }
              strncpy(s, ts, len);
              s[len-1] = '\0';
              return s;
       }
       return NULL;
}

Here is the caller graph for this function:

int temp_fd ( char *  s,
size_t  len,
char *  templ 
)

Definition at line 146 of file paths.c.

{
       char *ts = NULL;

       ts = prepare_tmpname(s, len, templ);
       if (ts == NULL) return -1;
#ifdef _WIN32
       ts = mktemp(ts);
       if (ts == NULL) return -1;
       return open(ts, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
#else
       return mkstemp(ts);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* temp_filename ( char *  s,
size_t  len,
char *  templ 
)

Definition at line 128 of file paths.c.

{
       char *ts = NULL;

       ts = prepare_tmpname(s, len, templ);
       if (ts == NULL) { return NULL; }
       return mktemp(ts);
}

Here is the call graph for this function:

FILE* temp_fp ( char *  s,
size_t  len,
char *  templ 
)

Definition at line 163 of file paths.c.

{
       int fd = temp_fd(s, len, templ);
       if (fd < 0) return NULL;
       return fdopen(fd, "w+");
}

Here is the call graph for this function:


Variable Documentation

const char* defaultpaths[] = DEFAULT_TEMPDIRS [static]

Definition at line 16 of file paths.c.

const char RCSid[] = "$Id: paths.c,v 2.4 2003/07/01 21:21:40 greg Exp $" [static]

Definition at line 2 of file paths.c.