New file, slightly modified from libiberties
[bpt/guile.git] / libguile / mkstemp.c
CommitLineData
23cc5968
MV
1/* Copyright (C) 1991, 1992, 1996, 1998 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.
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,
18 Boston, MA 02111-1307, USA. */
19
20#include "scmconfig.h"
21
22#ifdef HAVE_STDLIB_H
23#include <stdlib.h>
24#endif
25#ifdef HAVE_STRING_H
26#include <string.h>
27#endif
28#include <errno.h>
29#include <stdio.h>
30#include <fcntl.h>
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34#ifdef HAVE_SYS_TIME_H
35#include <sys/time.h>
36#endif
37
38/* We need to provide a type for gcc_uint64_t. */
39#ifdef __GNUC__
40typedef unsigned long long gcc_uint64_t;
41#else
42typedef unsigned long gcc_uint64_t;
43#endif
44
45#ifndef TMP_MAX
46#define TMP_MAX 16384
47#endif
48
49/* Generate a unique temporary file name from TEMPLATE.
50
51 TEMPLATE has the form:
52
53 <path>/ccXXXXXX
54
55 The last six characters of TEMPLATE must be "XXXXXX"; they are
56 replaced with a string that makes the filename unique.
57
58 Returns a file descriptor open on the file for reading and writing. */
59int
60mkstemp (template)
61 char *template;
62{
63 static const char letters[]
64 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
65 static gcc_uint64_t value;
66#ifdef HAVE_GETTIMEOFDAY
67 struct timeval tv;
68#endif
69 char *XXXXXX;
70 size_t len;
71 int count;
72
73 len = strlen (template);
74
75 if ((int) len < 6
76 || strncmp (&template[len - 6], "XXXXXX", 6))
77 {
78 return -1;
79 }
80
81 XXXXXX = &template[len - 6];
82
83#ifdef HAVE_GETTIMEOFDAY
84 /* Get some more or less random data. */
85 gettimeofday (&tv, NULL);
86 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
87#else
88 value += getpid ();
89#endif
90
91 for (count = 0; count < TMP_MAX; ++count)
92 {
93 gcc_uint64_t v = value;
94 int fd;
95
96 /* Fill in the random bits. */
97 XXXXXX[0] = letters[v % 62];
98 v /= 62;
99 XXXXXX[1] = letters[v % 62];
100 v /= 62;
101 XXXXXX[2] = letters[v % 62];
102 v /= 62;
103 XXXXXX[3] = letters[v % 62];
104 v /= 62;
105 XXXXXX[4] = letters[v % 62];
106 v /= 62;
107 XXXXXX[5] = letters[v % 62];
108
109 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
110 if (fd >= 0)
111 /* The file does not exist. */
112 return fd;
113
114 /* This is a random value. It is only necessary that the next
115 TMP_MAX values generated by adding 7777 to VALUE are different
116 with (module 2^32). */
117 value += 7777;
118 }
119
120 /* We return the null string if we can't find a unique file name. */
121 template[0] = '\0';
122 return -1;
123}