1 /* Copyright (C) 1991, 1992, 1996, 1998, 2004 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., 51 Franklin Street - Fifth Floor,
17    Boston, MA 02110-1301, USA.  */
18 /*
19  * This file was copied on 2013-08-03 from:
20  * https://raw.github.com/mirrors/gcc/master/libiberty/mkstemps.c
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "compat.h"
28 
29 #include <sys/types.h>
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #include <errno.h>
37 #include <stdio.h>
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 
46 /* We need to provide a type for gcc_uint64_t.  */
47 #ifdef __GNUC__
48 __extension__ typedef unsigned long long gcc_uint64_t;
49 #else
50 typedef unsigned long gcc_uint64_t;
51 #endif
52 
53 #ifndef TMP_MAX
54 #define TMP_MAX 16384
55 #endif
56 
57 #ifndef O_BINARY
58 # define O_BINARY 0
59 #endif
60 
61 /*
62 
63 @deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
64 
65 Generate a unique temporary file name from @var{pattern}.
66 @var{pattern} has the form:
67 
68 @example
69    @var{path}/ccXXXXXX@var{suffix}
70 @end example
71 
72 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
73 length).  The last six characters of @var{pattern} before @var{suffix}
74 must be @samp{XXXXXX}; they are replaced with a string that makes the
75 filename unique.  Returns a file descriptor open on the file for
76 reading and writing.
77 
78 @end deftypefn
79 
80 */
81 
82 int
compat_mkstemps(char * pattern,int suffix_len)83 compat_mkstemps (char *pattern, int suffix_len)
84 {
85   static const char letters[]
86     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
87   static gcc_uint64_t value;
88 #ifdef HAVE_GETTIMEOFDAY
89   struct timeval tv;
90 #endif
91   char *XXXXXX;
92   size_t len;
93   int count;
94 
95   len = strlen (pattern);
96 
97   if ((int) len < 6 + suffix_len
98       || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
99     {
100       return -1;
101     }
102 
103   XXXXXX = &pattern[len - 6 - suffix_len];
104 
105 #ifdef HAVE_GETTIMEOFDAY
106   /* Get some more or less random data.  */
107   gettimeofday (&tv, NULL);
108   value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
109 #else
110   value += getpid ();
111 #endif
112 
113   for (count = 0; count < TMP_MAX; ++count)
114     {
115       gcc_uint64_t v = value;
116       int fd;
117 
118       /* Fill in the random bits.  */
119       XXXXXX[0] = letters[v % 62];
120       v /= 62;
121       XXXXXX[1] = letters[v % 62];
122       v /= 62;
123       XXXXXX[2] = letters[v % 62];
124       v /= 62;
125       XXXXXX[3] = letters[v % 62];
126       v /= 62;
127       XXXXXX[4] = letters[v % 62];
128       v /= 62;
129       XXXXXX[5] = letters[v % 62];
130 
131       fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
132       if (fd >= 0)
133 	/* The file does not exist.  */
134 	return fd;
135       if (errno != EEXIST
136 #ifdef EISDIR
137 	  && errno != EISDIR
138 #endif
139 	 )
140 	/* Fatal error (EPERM, ENOSPC etc).  Doesn't make sense to loop.  */
141 	break;
142 
143       /* This is a random value.  It is only necessary that the next
144 	 TMP_MAX values generated by adding 7777 to VALUE are different
145 	 with (module 2^32).  */
146       value += 7777;
147     }
148 
149   /* We return the null string if we can't find a unique file name.  */
150   pattern[0] = '\0';
151   return -1;
152 }
153