ReactOS  0.4.12-dev-918-g6c6e7b8
mkstemps.c
Go to the documentation of this file.
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 /* if O_BINARY is not defined, the system is probably not expecting any such flag */
50 #ifndef O_BINARY
51 #define O_BINARY 0
52 #endif
53 
54 /*
55 
56 @deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
57 
58 Generate a unique temporary file name from @var{template}.
59 @var{template} has the form:
60 
61 @example
62  @var{path}/ccXXXXXX@var{suffix}
63 @end example
64 
65 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
66 length). The last six characters of @var{template} before @var{suffix}
67 must be @samp{XXXXXX}; they are replaced with a string that makes the
68 filename unique. Returns a file descriptor open on the file for
69 reading and writing.
70 
71 @end deftypefn
72 
73 */
74 
75 int
77  char *template,
78  int suffix_len)
79 {
80  static const char letters[]
81  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
82  static gcc_uint64_t value;
83 #ifdef HAVE_GETTIMEOFDAY
84  struct timeval tv;
85 #endif
86  char *XXXXXX;
87  size_t len;
88  int count;
89 
90  len = strlen (template);
91 
92  if ((int) len < 6 + suffix_len
93  || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
94  {
95  printf("wrong parameter\n");
96  return -1;
97  }
98 
99  XXXXXX = &template[len - 6 - suffix_len];
100 
101 #ifdef HAVE_GETTIMEOFDAY
102  /* Get some more or less random data. */
103  gettimeofday (&tv, NULL);
104  value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
105 #else
106  value += getpid ();
107 #endif
108 
109  for (count = 0; count < TMP_MAX; ++count)
110  {
111  gcc_uint64_t v = value;
112  int fd;
113 
114  /* Fill in the random bits. */
115  XXXXXX[0] = letters[v % 62];
116  v /= 62;
117  XXXXXX[1] = letters[v % 62];
118  v /= 62;
119  XXXXXX[2] = letters[v % 62];
120  v /= 62;
121  XXXXXX[3] = letters[v % 62];
122  v /= 62;
123  XXXXXX[4] = letters[v % 62];
124  v /= 62;
125  XXXXXX[5] = letters[v % 62];
126 
127 #ifdef VMS
128  fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600, "fop=tmd");
129 #else
130  fd = open (template, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
131 #endif
132  if (fd >= 0)
133  /* The file does not exist. */
134  return fd;
135 
136  /* This is a random value. It is only necessary that the next
137  TMP_MAX values generated by adding 7777 to VALUE are different
138  with (module 2^32). */
139  value += 7777;
140  }
141 
142  /* We return the null string if we can't find a unique file name. */
143  template[0] = '\0';
144  return -1;
145 }
#define open
Definition: acwin.h:71
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define TMP_MAX
Definition: mkstemps.c:46
GLuint GLuint GLsizei count
Definition: gl.h:1545
unsigned long tv_sec
Definition: linux.h:1738
static int fd
Definition: io.c:51
int mkstemps(char *template, int suffix_len)
Definition: mkstemps.c:76
unsigned long gcc_uint64_t
Definition: mkstemps.c:42
#define gettimeofday(tv, tz)
Definition: adns_win32.h:159
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
#define O_EXCL
Definition: fcntl.h:40
#define O_BINARY
Definition: mkstemps.c:51
_CRTIMP int __cdecl getpid(void)
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
GLenum GLsizei len
Definition: glext.h:6722
GLsizei const GLfloat * value
Definition: glext.h:6069
const GLdouble * v
Definition: gl.h:2040
#define O_RDWR
Definition: fcntl.h:36
#define O_CREAT
Definition: acwin.h:84
#define printf
Definition: config.h:203