1/* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
2   This file is derived from mkstemp.c from the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Library General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Library General Public License for more details.
13
14   You should have received a copy of the GNU Library General Public
15   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17   Boston, MA 02111-1307, USA.  */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#ifdef HAVE_STDLIB_H
24#include <stdlib.h>
25#endif
26#ifdef HAVE_STRING_H
27#include <string.h>
28#endif
29#include <errno.h>
30#include <stdio.h>
31#include <fcntl.h>
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35#ifdef HAVE_SYS_TIME_H
36#include <sys/time.h>
37#endif
38#include "ansidecl.h"
39
40/* We need to provide a type for gcc_uint64_t.  */
41#ifdef __GNUC__
42typedef unsigned long long gcc_uint64_t;
43#else
44typedef unsigned long gcc_uint64_t;
45#endif
46
47#ifndef TMP_MAX
48#define TMP_MAX 16384
49#endif
50
51/* Generate a unique temporary file name from TEMPLATE.
52
53   TEMPLATE has the form:
54
55   <path>/ccXXXXXX<suffix>
56
57   SUFFIX_LEN tells us how long <suffix> is (it can be zero length).
58
59   The last six characters of TEMPLATE before <suffix> must be "XXXXXX";
60   they are replaced with a string that makes the filename unique.
61
62   Returns a file descriptor open on the file for reading and writing.  */
63int
64mkstemps (template, suffix_len)
65     char *template;
66     int suffix_len;
67{
68  static const char letters[]
69    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70  static gcc_uint64_t value;
71#ifdef HAVE_GETTIMEOFDAY
72  struct timeval tv;
73#endif
74  char *XXXXXX;
75  size_t len;
76  int count;
77
78  len = strlen (template);
79
80  if ((int) len < 6 + suffix_len
81      || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
82    {
83      return -1;
84    }
85
86  XXXXXX = &template[len - 6 - suffix_len];
87
88#ifdef HAVE_GETTIMEOFDAY
89  /* Get some more or less random data.  */
90  gettimeofday (&tv, NULL);
91  value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
92#else
93  value += getpid ();
94#endif
95
96  for (count = 0; count < TMP_MAX; ++count)
97    {
98      gcc_uint64_t v = value;
99      int fd;
100
101      /* Fill in the random bits.  */
102      XXXXXX[0] = letters[v % 62];
103      v /= 62;
104      XXXXXX[1] = letters[v % 62];
105      v /= 62;
106      XXXXXX[2] = letters[v % 62];
107      v /= 62;
108      XXXXXX[3] = letters[v % 62];
109      v /= 62;
110      XXXXXX[4] = letters[v % 62];
111      v /= 62;
112      XXXXXX[5] = letters[v % 62];
113
114      fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
115      if (fd >= 0)
116	/* The file does not exist.  */
117	return fd;
118
119      /* This is a random value.  It is only necessary that the next
120	 TMP_MAX values generated by adding 7777 to VALUE are different
121	 with (module 2^32).  */
122      value += 7777;
123    }
124
125  /* We return the null string if we can't find a unique file name.  */
126  template[0] = '\0';
127  return -1;
128}
129