Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / mktemp.cc
blob710e432b408659f122f82e5e045768104265d173
1 /* mktemp.cc: mktemp functions
3 This file is adapted for Cygwin from FreeBSD and newlib.
5 See the copyright at the bottom of this file. */
7 #include "winsup.h"
8 #include "cygerrno.h"
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
13 static int _gettemp(char *, int *, int, size_t, int);
15 static const char padchar[] =
16 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
18 extern "C" int
19 mkstemp(char *path)
21 int fd;
22 return _gettemp(path, &fd, 0, 0, O_BINARY) ? fd : -1;
25 extern "C" char *
26 mkdtemp(char *path)
28 return _gettemp(path, NULL, 1, 0, 0) ? path : NULL;
31 extern "C" int
32 mkstemps(char *path, int len)
34 int fd;
35 return _gettemp(path, &fd, 0, len, O_BINARY) ? fd : -1;
38 extern "C" int
39 mkostemp(char *path, int flags)
41 int fd;
42 return _gettemp(path, &fd, 0, 0, flags & ~O_ACCMODE) ? fd : -1;
45 extern "C" int
46 mkostemps(char *path, int len, int flags)
48 int fd;
49 return _gettemp(path, &fd, 0, len, flags & ~O_ACCMODE) ? fd : -1;
52 extern "C" char *
53 mktemp(char *path)
55 return _gettemp(path, NULL, 0, 0, 0) ? path : (char *) NULL;
58 static int
59 _gettemp(char *path, int *doopen, int domkdir, size_t suffixlen, int flags)
61 char *start, *trv, *suffp;
62 char *pad;
64 if (doopen && domkdir)
66 set_errno (EINVAL);
67 return 0;
70 trv = strchr (path, '\0');
71 if ((size_t) (trv - path) < suffixlen)
73 set_errno (EINVAL);
74 return 0;
76 trv -= suffixlen;
77 suffp = trv--;
79 /* Fill space with random characters */
80 while (trv >= path && *trv == 'X')
82 uint32_t rand = arc4random () % (sizeof (padchar) - 1);
83 *trv-- = padchar[rand];
85 if (suffp - trv < 6)
87 set_errno (EINVAL);
88 return 0;
90 start = trv + 1;
93 * check the target directory.
95 struct stat sbuf;
96 if (doopen != NULL || domkdir)
98 for (; trv > path; trv--)
100 if (*trv == '/')
102 *trv = '\0';
103 int rval = stat (path, &sbuf);
104 *trv = '/';
105 if (rval != 0)
106 return 0;
107 if (!S_ISDIR (sbuf.st_mode))
109 set_errno (ENOTDIR);
110 return 0;
112 break;
117 for (;;)
119 if (doopen)
121 if ((*doopen = open (path, O_CREAT | O_EXCL | O_RDWR | flags,
122 S_IRUSR | S_IWUSR)) >= 0)
123 return 1;
124 if (errno != EEXIST)
125 return 0;
127 else if (domkdir)
129 if (mkdir (path, 0700) == 0)
130 return 1;
131 if (errno != EEXIST)
132 return 0;
134 else if (lstat (path, &sbuf))
135 return errno == ENOENT;
137 /* If we have a collision, cycle through the space of filenames */
138 for (trv = start;;)
140 if (*trv == '\0' || trv == suffp)
141 return 0;
142 pad = strchr (padchar, *trv);
143 if (pad == NULL || *++pad == '\0')
144 *trv++ = padchar[0];
145 else
147 *trv++ = *pad;
148 break;
152 /*NOTREACHED*/
156 * Copyright (c) 1987, 1993
157 * The Regents of the University of California. All rights reserved.
159 * Redistribution and use in source and binary forms, with or without
160 * modification, are permitted provided that the following conditions
161 * are met:
162 * 1. Redistributions of source code must retain the above copyright
163 * notice, this list of conditions and the following disclaimer.
164 * 2. Redistributions in binary form must reproduce the above copyright
165 * notice, this list of conditions and the following disclaimer in the
166 * documentation and/or other materials provided with the distribution.
167 * 4. Neither the name of the University nor the names of its contributors
168 * may be used to endorse or promote products derived from this software
169 * without specific prior written permission.
171 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
172 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
175 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
176 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
177 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
178 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
179 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
180 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
181 * SUCH DAMAGE.