* mallocs.c: #include <config.h> if HAVE_CONFIG_H.
[bpt/guile.git] / libguile / mkstemp.c
CommitLineData
98cb9664 1/* Copyright (C) 1991, 1992, 1996, 1998, 2001 Free Software Foundation, Inc.
23cc5968
MV
2 This file is derived from mkstemps.c from the GNU Libiberty Library
3 which in turn is derived from the GNU C Library.
4
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.
9
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.
14
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,
57e2f421
MV
18 Boston, MA 02111-1307, USA.
19
20 As a special exception, the Free Software Foundation gives permission
21 for additional uses of the text contained in its release of GUILE.
22
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.
28
29 This exception does not however invalidate any other reasons why
30 the executable file might be covered by the GNU General Public License.
31
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.
39
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. */
23cc5968 43
98cb9664 44#include "libguile/scmconfig.h"
23cc5968
MV
45
46#ifdef HAVE_STDLIB_H
47#include <stdlib.h>
48#endif
49#ifdef HAVE_STRING_H
50#include <string.h>
51#endif
52#include <errno.h>
53#include <stdio.h>
54#include <fcntl.h>
55#ifdef HAVE_UNISTD_H
56#include <unistd.h>
57#endif
58#ifdef HAVE_SYS_TIME_H
59#include <sys/time.h>
60#endif
5507e726
MV
61#ifdef __MINGW32__
62#include <process.h>
63#endif
23cc5968
MV
64
65/* We need to provide a type for gcc_uint64_t. */
66#ifdef __GNUC__
67typedef unsigned long long gcc_uint64_t;
68#else
69typedef unsigned long gcc_uint64_t;
70#endif
71
72#ifndef TMP_MAX
73#define TMP_MAX 16384
74#endif
75
76/* Generate a unique temporary file name from TEMPLATE.
77
78 TEMPLATE has the form:
79
80 <path>/ccXXXXXX
81
82 The last six characters of TEMPLATE must be "XXXXXX"; they are
83 replaced with a string that makes the filename unique.
84
85 Returns a file descriptor open on the file for reading and writing. */
86int
87mkstemp (template)
88 char *template;
89{
90 static const char letters[]
91 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
92 static gcc_uint64_t value;
93#ifdef HAVE_GETTIMEOFDAY
94 struct timeval tv;
95#endif
96 char *XXXXXX;
97 size_t len;
98 int count;
99
100 len = strlen (template);
101
102 if ((int) len < 6
103 || strncmp (&template[len - 6], "XXXXXX", 6))
104 {
105 return -1;
106 }
107
108 XXXXXX = &template[len - 6];
109
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 ();
114#else
115 value += getpid ();
116#endif
117
118 for (count = 0; count < TMP_MAX; ++count)
119 {
120 gcc_uint64_t v = value;
121 int fd;
122
123 /* Fill in the random bits. */
124 XXXXXX[0] = letters[v % 62];
125 v /= 62;
126 XXXXXX[1] = letters[v % 62];
127 v /= 62;
128 XXXXXX[2] = letters[v % 62];
129 v /= 62;
130 XXXXXX[3] = letters[v % 62];
131 v /= 62;
132 XXXXXX[4] = letters[v % 62];
133 v /= 62;
134 XXXXXX[5] = letters[v % 62];
135
136 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
137 if (fd >= 0)
138 /* The file does not exist. */
139 return fd;
140
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). */
144 value += 7777;
145 }
146
147 /* We return the null string if we can't find a unique file name. */
148 template[0] = '\0';
149 return -1;
150}