2000-10-31 Philip Blundell <philb@gnu.org>
[binutils.git] / libiberty / mkstemps.c
blob4b89162751d6da29470a18d720518d74ecb12ef4
1 /* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
2 This file is derived from mkstemp.c from the GNU C Library.
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.
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.
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. */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include <sys/types.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #include <errno.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39 #include "ansidecl.h"
41 /* We need to provide a type for gcc_uint64_t. */
42 #ifdef __GNUC__
43 __extension__ typedef unsigned long long gcc_uint64_t;
44 #else
45 typedef unsigned long gcc_uint64_t;
46 #endif
48 #ifndef TMP_MAX
49 #define TMP_MAX 16384
50 #endif
52 /* Generate a unique temporary file name from TEMPLATE.
54 TEMPLATE has the form:
56 <path>/ccXXXXXX<suffix>
58 SUFFIX_LEN tells us how long <suffix> is (it can be zero length).
60 The last six characters of TEMPLATE before <suffix> must be "XXXXXX";
61 they are replaced with a string that makes the filename unique.
63 Returns a file descriptor open on the file for reading and writing. */
64 int
65 mkstemps (template, suffix_len)
66 char *template;
67 int suffix_len;
69 static const char letters[]
70 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
71 static gcc_uint64_t value;
72 #ifdef HAVE_GETTIMEOFDAY
73 struct timeval tv;
74 #endif
75 char *XXXXXX;
76 size_t len;
77 int count;
79 len = strlen (template);
81 if ((int) len < 6 + suffix_len
82 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
84 return -1;
87 XXXXXX = &template[len - 6 - suffix_len];
89 #ifdef HAVE_GETTIMEOFDAY
90 /* Get some more or less random data. */
91 gettimeofday (&tv, NULL);
92 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
93 #else
94 value += getpid ();
95 #endif
97 for (count = 0; count < TMP_MAX; ++count)
99 gcc_uint64_t v = value;
100 int fd;
102 /* Fill in the random bits. */
103 XXXXXX[0] = letters[v % 62];
104 v /= 62;
105 XXXXXX[1] = letters[v % 62];
106 v /= 62;
107 XXXXXX[2] = letters[v % 62];
108 v /= 62;
109 XXXXXX[3] = letters[v % 62];
110 v /= 62;
111 XXXXXX[4] = letters[v % 62];
112 v /= 62;
113 XXXXXX[5] = letters[v % 62];
115 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
116 if (fd >= 0)
117 /* The file does not exist. */
118 return fd;
120 /* This is a random value. It is only necessary that the next
121 TMP_MAX values generated by adding 7777 to VALUE are different
122 with (module 2^32). */
123 value += 7777;
126 /* We return the null string if we can't find a unique file name. */
127 template[0] = '\0';
128 return -1;