src/util/tmpfile.c File Reference

#include <sys/types.h>
#include "util.h"
Include dependency graph for tmpfile.c:

Go to the source code of this file.

Functions

static char check_directory (char const *dir)
char * util_tempnam (char const *dir, char const *pfx)
FILE * util_tmpfile (void)

Function Documentation

static char check_directory ( char const *  dir  )  [static]

Definition at line 32 of file tmpfile.c.

00033 {
00034      struct stat statbuf;
00035 
00036      if (! dir)
00037          return 0;
00038      else if (stat(dir, &statbuf) < 0)
00039          return 0;
00040      else if (S_ISDIR(statbuf.st_mode))
00041          return 0;
00042      else if (access(dir, W_OK | X_OK) < 0)
00043          return 0;
00044      else
00045          return 1;
00046 }

char* util_tempnam ( char const *  dir,
char const *  pfx 
)

Definition at line 49 of file tmpfile.c.

00050 {
00051      extern char *getenv(const char *);
00052      char const *tmpdir = NULL, *env;
00053      char *filename;
00054      static char unique_letters[4] = "AAA";
00055      char addslash = 0;
00056 
00057      /*
00058       * If a directory is passed in, verify that it exists and is a
00059       * directory and is writeable by this process.  If no directory
00060       * is passed in, or if the directory that is passed in does not
00061       * exist, check the environment variable TMPDIR.  If it isn't
00062       * set, check the predefined constant P_tmpdir.  If that isn't
00063       * set, use "/tmp/".
00064       */
00065 
00066      if ((env = getenv ("TMPDIR")) && check_directory(env))
00067          tmpdir = env;
00068      else if (dir && check_directory(dir))
00069          tmpdir = dir;
00070 #ifdef P_tmpdir
00071      else if (check_directory(P_tmpdir))
00072          tmpdir = P_tmpdir;
00073 #endif
00074      else
00075          tmpdir = "/tmp/";
00076 
00077      /*
00078       * OK, now that we've got a directory, figure out whether or not
00079       * there's a slash at the end of it.
00080       */
00081      if (tmpdir[strlen(tmpdir) - 1] != '/')
00082          addslash = 1;
00083 
00084      /*
00085       * Now figure out the set of unique letters.
00086       */
00087      unique_letters[0]++;
00088      if (unique_letters[0] > 'Z') {
00089          unique_letters[0] = 'A';
00090          unique_letters[1]++;
00091          if (unique_letters[1] > 'Z') {
00092              unique_letters[1] = 'A';
00093              unique_letters[2]++;
00094              if (unique_letters[2] > 'Z') {
00095                  unique_letters[2]++;
00096              }
00097          }
00098      }
00099 
00100      /*
00101       * Allocate a string of sufficient length.
00102       */
00103      if (pfx) {
00104          filename = (char *) malloc(strlen(tmpdir) + addslash + strlen(pfx) + 10
00105 );
00106      } else {
00107          filename = (char *) malloc(strlen(tmpdir) + addslash + 10);
00108      }
00109 
00110      /*
00111       * And create the string.
00112       */
00113      (void) sprintf(filename, "%s%s%s%sa%05d", tmpdir, addslash ? "/" : "",
00114                     pfx ? pfx : "", unique_letters, (int)getpid());
00115 
00116      return filename;
00117 }

FILE* util_tmpfile ( void   ) 

Definition at line 141 of file tmpfile.c.

00142 {
00143     FILE *fp;
00144 
00145     if ((fp = fopen("utiltmp", "w+")) == NULL) {
00146         return NULL;
00147     }
00148     (void) unlink("utiltmp");
00149     return fp;
00150 }


Generated on Tue Jan 12 13:57:29 2010 for glu-2.2 by  doxygen 1.6.1