Back to index

cell-binutils  2.17cvs20070401
Defines | Typedefs | Functions
mkstemps.c File Reference
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include "ansidecl.h"

Go to the source code of this file.

Defines

#define TMP_MAX   16384
#define O_BINARY   0

Typedefs

typedef unsigned long gcc_uint64_t

Functions

int mkstemps (char *pattern, int suffix_len)

Define Documentation

#define O_BINARY   0

Definition at line 53 of file mkstemps.c.

#define TMP_MAX   16384

Definition at line 49 of file mkstemps.c.


Typedef Documentation

Definition at line 45 of file mkstemps.c.


Function Documentation

int mkstemps ( char *  pattern,
int  suffix_len 
)

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 call graph for this function:

Here is the caller graph for this function: