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