* Bring back rbuild build to be used until bug 6372 is fixed.
[reactos.git] / tools / winebuild / mkstemps.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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #ifdef HAVE_PROCESS_H
35 #include <process.h>
36 #endif
37
38 /* We need to provide a type for gcc_uint64_t. */
39 #ifdef __GNUC__
40 __extension__ typedef unsigned long long gcc_uint64_t;
41 #else
42 typedef unsigned long gcc_uint64_t;
43 #endif
44
45 #ifndef TMP_MAX
46 #define TMP_MAX 16384
47 #endif
48
49 /*
50
51 @deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
52
53 Generate a unique temporary file name from @var{template}.
54 @var{template} has the form:
55
56 @example
57 @var{path}/ccXXXXXX@var{suffix}
58 @end example
59
60 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
61 length). The last six characters of @var{template} before @var{suffix}
62 must be @samp{XXXXXX}; they are replaced with a string that makes the
63 filename unique. Returns a file descriptor open on the file for
64 reading and writing.
65
66 @end deftypefn
67
68 */
69
70 int
71 mkstemps (
72 char *template,
73 int suffix_len)
74 {
75 static const char letters[]
76 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
77 static gcc_uint64_t value;
78 #ifdef HAVE_GETTIMEOFDAY
79 struct timeval tv;
80 #endif
81 char *XXXXXX;
82 size_t len;
83 int count;
84
85 len = strlen (template);
86
87 if ((int) len < 6 + suffix_len
88 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
89 {
90 return -1;
91 }
92
93 XXXXXX = &template[len - 6 - suffix_len];
94
95 #ifdef HAVE_GETTIMEOFDAY
96 /* Get some more or less random data. */
97 gettimeofday (&tv, NULL);
98 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
99 #else
100 value += getpid ();
101 #endif
102
103 for (count = 0; count < TMP_MAX; ++count)
104 {
105 gcc_uint64_t v = value;
106 int fd;
107
108 /* Fill in the random bits. */
109 XXXXXX[0] = letters[v % 62];
110 v /= 62;
111 XXXXXX[1] = letters[v % 62];
112 v /= 62;
113 XXXXXX[2] = letters[v % 62];
114 v /= 62;
115 XXXXXX[3] = letters[v % 62];
116 v /= 62;
117 XXXXXX[4] = letters[v % 62];
118 v /= 62;
119 XXXXXX[5] = letters[v % 62];
120
121 #ifdef VMS
122 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600, "fop=tmd");
123 #else
124 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
125 #endif
126 if (fd >= 0)
127 /* The file does not exist. */
128 return fd;
129
130 /* This is a random value. It is only necessary that the next
131 TMP_MAX values generated by adding 7777 to VALUE are different
132 with (module 2^32). */
133 value += 7777;
134 }
135
136 /* We return the null string if we can't find a unique file name. */
137 template[0] = '\0';
138 return -1;
139 }