1 /* Copyright (C) 1991, 1992, 1996, 1998, 2001 Free Software Foundation, Inc.
2 This file is derived from mkstemps.c from the GNU Libiberty Library
3 which in turn is derived from the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 As a special exception, the Free Software Foundation gives permission
21 for additional uses of the text contained in its release of GUILE.
23 The exception is that, if you link the GUILE library with other files
24 to produce an executable, this does not by itself cause the
25 resulting executable to be covered by the GNU General Public License.
26 Your use of that executable is in no way restricted on account of
27 linking the GUILE library code into it.
29 This exception does not however invalidate any other reasons why
30 the executable file might be covered by the GNU General Public License.
32 This exception applies only to the code released by the
33 Free Software Foundation under the name GUILE. If you copy
34 code from other Free Software Foundation releases into a copy of
35 GUILE, as the General Public License permits, the exception does
36 not apply to the code that you add in this way. To avoid misleading
37 anyone as to the status of such modified files, you must delete
38 this exception notice from them.
40 If you write modifications of your own for GUILE, it is your choice
41 whether to permit this exception to apply to your modifications.
42 If you do not wish that, delete this exception notice. */
44 #include "libguile/scmconfig.h"
58 #ifdef HAVE_SYS_TIME_H
65 /* We need to provide a type for gcc_uint64_t. */
67 typedef unsigned long long gcc_uint64_t
;
69 typedef unsigned long gcc_uint64_t
;
76 /* Generate a unique temporary file name from TEMPLATE.
78 TEMPLATE has the form:
82 The last six characters of TEMPLATE must be "XXXXXX"; they are
83 replaced with a string that makes the filename unique.
85 Returns a file descriptor open on the file for reading and writing. */
90 static const char letters
[]
91 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
92 static gcc_uint64_t value
;
93 #ifdef HAVE_GETTIMEOFDAY
100 len
= strlen (template);
103 || strncmp (&template[len
- 6], "XXXXXX", 6))
108 XXXXXX
= &template[len
- 6];
110 #ifdef HAVE_GETTIMEOFDAY
111 /* Get some more or less random data. */
112 gettimeofday (&tv
, NULL
);
113 value
+= ((gcc_uint64_t
) tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
118 for (count
= 0; count
< TMP_MAX
; ++count
)
120 gcc_uint64_t v
= value
;
123 /* Fill in the random bits. */
124 XXXXXX
[0] = letters
[v
% 62];
126 XXXXXX
[1] = letters
[v
% 62];
128 XXXXXX
[2] = letters
[v
% 62];
130 XXXXXX
[3] = letters
[v
% 62];
132 XXXXXX
[4] = letters
[v
% 62];
134 XXXXXX
[5] = letters
[v
% 62];
136 fd
= open (template, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
138 /* The file does not exist. */
141 /* This is a random value. It is only necessary that the next
142 TMP_MAX values generated by adding 7777 to VALUE are different
143 with (module 2^32). */
147 /* We return the null string if we can't find a unique file name. */