]> gcc.gnu.org Git - gcc.git/blob - libiberty/mkstemp.c
choose-temp.c: Don't include gansidecl.h.
[gcc.git] / libiberty / mkstemp.c
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 #ifndef IN_GCC
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"
40 #else
41 #include "system.h"
42 #endif
43
44 /* We need to provide a type for gcc_uint64_t. */
45 #ifdef __GNUC__
46 typedef unsigned long long gcc_uint64_t;
47 #else
48 typedef unsigned long gcc_uint64_t;
49 #endif
50
51 #ifndef TMP_MAX
52 #define TMP_MAX 16384
53 #endif
54
55 /* Generate a unique temporary file name from TEMPLATE.
56
57 TEMPLATE has the form:
58
59 <path>/ccXXXXXX<suffix>
60
61 SUFFIX_LEN tells us how long <suffix> is (it can be zero length).
62
63 The last six characters of TEMPLATE before <suffix> must be "XXXXXX";
64 they are replaced with a string that makes the filename unique.
65
66 Returns a file descriptor open on the file for reading and writing. */
67 int
68 mkstemps (template, suffix_len)
69 char *template;
70 int suffix_len;
71 {
72 static const char letters[]
73 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
74 static gcc_uint64_t value;
75 #ifdef HAVE_GETTIMEOFDAY
76 struct timeval tv;
77 #endif
78 char *XXXXXX;
79 size_t len;
80 int count;
81
82 len = strlen (template);
83
84 if ((int) len < 6 + suffix_len
85 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
86 {
87 return -1;
88 }
89
90 XXXXXX = &template[len - 6 - suffix_len];
91
92 #ifdef HAVE_GETTIMEOFDAY
93 /* Get some more or less random data. */
94 gettimeofday (&tv, NULL);
95 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
96 #else
97 value += getpid ();
98 #endif
99
100 for (count = 0; count < TMP_MAX; ++count)
101 {
102 gcc_uint64_t v = value;
103 int fd;
104
105 /* Fill in the random bits. */
106 XXXXXX[0] = letters[v % 62];
107 v /= 62;
108 XXXXXX[1] = letters[v % 62];
109 v /= 62;
110 XXXXXX[2] = letters[v % 62];
111 v /= 62;
112 XXXXXX[3] = letters[v % 62];
113 v /= 62;
114 XXXXXX[4] = letters[v % 62];
115 v /= 62;
116 XXXXXX[5] = letters[v % 62];
117
118 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
119 if (fd >= 0)
120 /* The file does not exist. */
121 return fd;
122
123 /* This is a random value. It is only necessary that the next
124 TMP_MAX values generated by adding 7777 to VALUE are different
125 with (module 2^32). */
126 value += 7777;
127 }
128
129 /* We return the null string if we can't find a unique file name. */
130 template[0] = '\0';
131 return -1;
132 }
This page took 0.04212 seconds and 5 git commands to generate.