Back to index

cell-binutils  2.17cvs20070401
Defines | Functions | Variables
make-temp-file.c File Reference
#include <stdio.h>
#include <sys/types.h>
#include "libiberty.h"

Go to the source code of this file.

Defines

#define R_OK   4
#define W_OK   2
#define X_OK   1
#define DIR_SEPARATOR   '/'
#define TEMP_FILE   "ccXXXXXX"
#define TEMP_FILE_LEN   (sizeof(TEMP_FILE) - 1)

Functions

int mkstemps (char *, int)
static const char * try_dir (const char *, const char *)
char * choose_tmpdir (void)
char * make_temp_file (const char *suffix)

Variables

static const char tmp [] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }
static const char usrtmp []
static const char vartmp []
static char * memoized_tmpdir

Define Documentation

#define DIR_SEPARATOR   '/'

Definition at line 50 of file make-temp-file.c.

#define R_OK   4

Definition at line 40 of file make-temp-file.c.

#define TEMP_FILE   "ccXXXXXX"

Definition at line 55 of file make-temp-file.c.

#define TEMP_FILE_LEN   (sizeof(TEMP_FILE) - 1)

Definition at line 56 of file make-temp-file.c.

#define W_OK   2

Definition at line 41 of file make-temp-file.c.

#define X_OK   1

Definition at line 42 of file make-temp-file.c.


Function Documentation

char* choose_tmpdir ( void  )

Definition at line 97 of file make-temp-file.c.

{
  const char *base = 0;
  char *tmpdir;
  unsigned int len;

  if (memoized_tmpdir)
    return memoized_tmpdir;

  base = try_dir (getenv ("TMPDIR"), base);
  base = try_dir (getenv ("TMP"), base);
  base = try_dir (getenv ("TEMP"), base);

#ifdef P_tmpdir
  base = try_dir (P_tmpdir, base);
#endif

  /* Try /var/tmp, /usr/tmp, then /tmp.  */
  base = try_dir (vartmp, base);
  base = try_dir (usrtmp, base);
  base = try_dir (tmp, base);
 
  /* If all else fails, use the current directory!  */
  if (base == 0)
    base = ".";

  /* Append DIR_SEPARATOR to the directory we've chosen
     and return it.  */
  len = strlen (base);
  tmpdir = XNEWVEC (char, len + 2);
  strcpy (tmpdir, base);
  tmpdir[len] = DIR_SEPARATOR;
  tmpdir[len+1] = '\0';

  memoized_tmpdir = tmpdir;
  return tmpdir;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* make_temp_file ( const char *  suffix)

Definition at line 148 of file make-temp-file.c.

{
  const char *base = choose_tmpdir ();
  char *temp_filename;
  int base_len, suffix_len;
  int fd;

  if (suffix == 0)
    suffix = "";

  base_len = strlen (base);
  suffix_len = strlen (suffix);

  temp_filename = XNEWVEC (char, base_len
                        + TEMP_FILE_LEN
                        + suffix_len + 1);
  strcpy (temp_filename, base);
  strcpy (temp_filename + base_len, TEMP_FILE);
  strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);

  fd = mkstemps (temp_filename, suffix_len);
  /* If mkstemps failed, then something bad is happening.  Maybe we should
     issue a message about a possible security attack in progress?  */
  if (fd == -1)
    abort ();
  /* Similarly if we can not close the file.  */
  if (close (fd))
    abort ();
  return temp_filename;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int mkstemps ( char *  ,
int   
)

Definition at line 78 of file mkstemps.c.

{
  static const char letters[]
    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  static gcc_uint64_t value;
#ifdef HAVE_GETTIMEOFDAY
  struct timeval tv;
#endif
  char *XXXXXX;
  size_t len;
  int count;

  len = strlen (pattern);

  if ((int) len < 6 + suffix_len
      || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
    {
      return -1;
    }

  XXXXXX = &pattern[len - 6 - suffix_len];

#ifdef HAVE_GETTIMEOFDAY
  /* Get some more or less random data.  */
  gettimeofday (&tv, NULL);
  value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
#else
  value += getpid ();
#endif

  for (count = 0; count < TMP_MAX; ++count)
    {
      gcc_uint64_t v = value;
      int fd;

      /* 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];

      fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
      if (fd >= 0)
       /* The file does not exist.  */
       return fd;

      /* This is a random value.  It is only necessary that the next
        TMP_MAX values generated by adding 7777 to VALUE are different
        with (module 2^32).  */
      value += 7777;
    }

  /* We return the null string if we can't find a unique file name.  */
  pattern[0] = '\0';
  return -1;
}

Here is the caller graph for this function:

static const char * try_dir ( const char *  dir,
const char *  base 
) [inline, static]

Definition at line 67 of file make-temp-file.c.

{
  if (base != 0)
    return base;
  if (dir != 0
      && access (dir, R_OK | W_OK | X_OK) == 0)
    return dir;
  return 0;
}

Here is the caller graph for this function:


Variable Documentation

char* memoized_tmpdir [static]

Definition at line 83 of file make-temp-file.c.

const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 } [static]

Definition at line 77 of file make-temp-file.c.

const char usrtmp[] [static]
Initial value:
{ DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }

Definition at line 78 of file make-temp-file.c.

const char vartmp[] [static]
Initial value:
{ DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }

Definition at line 80 of file make-temp-file.c.