OSDN Git Service

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