Back to index

glibc  2.9
Defines | Functions | Variables
tempname.c File Reference
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>

Go to the source code of this file.

Defines

#define __set_errno(Val)   errno = (Val)
#define P_tmpdir   "/tmp"
#define TMP_MAX   238328
#define __GT_FILE   0
#define __GT_DIR   1
#define __GT_NOCREATE   2
#define S_IRUSR   00400
#define S_IWUSR   00200
#define S_IXUSR   00100
#define struct_stat64   struct stat
#define __getpid   getpid
#define __gettimeofday   gettimeofday
#define __mkdir   mkdir
#define __open   open
#define __open64   open
#define __lxstat64(version, path, buf)   lstat (path, buf)
#define __xstat64(version, path, buf)   stat (path, buf)
#define __secure_getenv   getenv
#define uint64_t   uintmax_t
#define ATTEMPTS_MIN   (62 * 62 * 62)

Functions

static int direxists (const char *dir)
int __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, int try_tmpdir)
int __gen_tempname (char *tmpl, int flags, int kind)

Variables

static const char letters [] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Define Documentation

#define __getpid   getpid

Definition at line 96 of file tempname.c.

Definition at line 97 of file tempname.c.

#define __GT_DIR   1

Definition at line 40 of file tempname.c.

#define __GT_FILE   0

Definition at line 39 of file tempname.c.

#define __GT_NOCREATE   2

Definition at line 41 of file tempname.c.

#define __lxstat64 (   version,
  path,
  buf 
)    lstat (path, buf)

Definition at line 101 of file tempname.c.

#define __mkdir   mkdir

Definition at line 98 of file tempname.c.

#define __open   open

Definition at line 99 of file tempname.c.

#define __open64   open

Definition at line 100 of file tempname.c.

#define __secure_getenv   getenv

Definition at line 106 of file tempname.c.

#define __set_errno (   Val)    errno = (Val)

Definition at line 28 of file tempname.c.

#define __xstat64 (   version,
  path,
  buf 
)    stat (path, buf)

Definition at line 102 of file tempname.c.

#define ATTEMPTS_MIN   (62 * 62 * 62)
#define P_tmpdir   "/tmp"

Definition at line 33 of file tempname.c.

#define S_IRUSR   00400

Definition at line 77 of file tempname.c.

#define S_IWUSR   00200

Definition at line 83 of file tempname.c.

#define S_IXUSR   00100

Definition at line 89 of file tempname.c.

#define struct_stat64   struct stat

Definition at line 95 of file tempname.c.

#define TMP_MAX   238328

Definition at line 36 of file tempname.c.

#define uint64_t   uintmax_t

Definition at line 134 of file tempname.c.


Function Documentation

int __gen_tempname ( char *  tmpl,
int  flags,
int  kind 
)

Definition at line 226 of file tempname.c.

{
  int len;
  char *XXXXXX;
  static uint64_t value;
  uint64_t random_time_bits;
  unsigned int count;
  int fd = -1;
  int save_errno = errno;
  struct_stat64 st;

  /* A lower bound on the number of temporary files to attempt to
     generate.  The maximum total number of temporary file names that
     can exist for a given template is 62**6.  It should never be
     necessary to try all these combinations.  Instead if a reasonable
     number of names is tried (we define reasonable as 62**3) fail to
     give the system administrator the chance to remove the problems.  */
#define ATTEMPTS_MIN (62 * 62 * 62)

  /* The number of times to attempt to generate a temporary file.  To
     conform to POSIX, this must be no smaller than TMP_MAX.  */
#if ATTEMPTS_MIN < TMP_MAX
  unsigned int attempts = TMP_MAX;
#else
  unsigned int attempts = ATTEMPTS_MIN;
#endif

  len = strlen (tmpl);
  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
    {
      __set_errno (EINVAL);
      return -1;
    }

  /* This is where the Xs start.  */
  XXXXXX = &tmpl[len - 6];

  /* Get some more or less random data.  */
#ifdef RANDOM_BITS
  RANDOM_BITS (random_time_bits);
#else
# if HAVE_GETTIMEOFDAY || _LIBC
  {
    struct timeval tv;
    __gettimeofday (&tv, NULL);
    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
  }
# else
  random_time_bits = time (NULL);
# endif
#endif
  value += random_time_bits ^ __getpid ();

  for (count = 0; count < attempts; value += 7777, ++count)
    {
      uint64_t v = value;

      /* Fill in the random bits.  */
      XXXXXX[0] = letters[v % 62];
      v /= 62;
      XXXXXX[1] = letters[v % 62];
      v /= 62;
      XXXXXX[2] = letters[v % 62];
      v /= 62;
      XXXXXX[3] = letters[v % 62];
      v /= 62;
      XXXXXX[4] = letters[v % 62];
      v /= 62;
      XXXXXX[5] = letters[v % 62];

      switch (kind)
       {
       case __GT_FILE:
         fd = __open (tmpl,
                     (flags & ~O_ACCMODE)
                     | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
         break;

       case __GT_DIR:
         fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
         break;

       case __GT_NOCREATE:
         /* This case is backward from the other three.  __gen_tempname
            succeeds if __xstat fails because the name does not exist.
            Note the continue to bypass the common logic at the bottom
            of the loop.  */
         if (__lxstat64 (_STAT_VER, tmpl, &st) < 0)
           {
             if (errno == ENOENT)
              {
                __set_errno (save_errno);
                return 0;
              }
             else
              /* Give up now. */
              return -1;
           }
         continue;

       default:
         assert (! "invalid KIND in __gen_tempname");
       }

      if (fd >= 0)
       {
         __set_errno (save_errno);
         return fd;
       }
      else if (errno != EEXIST)
       return -1;
    }

  /* We got out of the loop because we ran out of combinations to try.  */
  __set_errno (EEXIST);
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int __path_search ( char *  tmpl,
size_t  tmpl_len,
const char *  dir,
const char *  pfx,
int  try_tmpdir 
)

Definition at line 152 of file tempname.c.

{
  const char *d;
  size_t dlen, plen;

  if (!pfx || !pfx[0])
    {
      pfx = "file";
      plen = 4;
    }
  else
    {
      plen = strlen (pfx);
      if (plen > 5)
       plen = 5;
    }

  if (try_tmpdir)
    {
      d = __secure_getenv ("TMPDIR");
      if (d != NULL && direxists (d))
       dir = d;
      else if (dir != NULL && direxists (dir))
       /* nothing */ ;
      else
       dir = NULL;
    }
  if (dir == NULL)
    {
      if (direxists (P_tmpdir))
       dir = P_tmpdir;
      else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
       dir = "/tmp";
      else
       {
         __set_errno (ENOENT);
         return -1;
       }
    }

  dlen = strlen (dir);
  while (dlen > 1 && dir[dlen - 1] == '/')
    dlen--;                 /* remove trailing slashes */

  /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  if (tmpl_len < dlen + 1 + plen + 6 + 1)
    {
      __set_errno (EINVAL);
      return -1;
    }

  sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
  return 0;
}

Here is the call graph for this function:

static int direxists ( const char *  dir) [static]

Definition at line 139 of file tempname.c.

{
  struct_stat64 buf;
  return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
}

Here is the caller graph for this function:


Variable Documentation

const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" [static]

Definition at line 209 of file tempname.c.