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. */
13 static int _gettemp(char *, int *, int, size_t, int);
14 static uint32_t arc4random ();
16 static const char padchar[] =
17 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
23 return _gettemp(path, &fd, 0, 0, O_BINARY) ? fd : -1;
29 return _gettemp(path, NULL, 1, 0, 0) ? path : NULL;
33 mkstemps(char *path, int len)
36 return _gettemp(path, &fd, 0, len, O_BINARY) ? fd : -1;
40 mkostemp(char *path, int flags)
43 return _gettemp(path, &fd, 0, 0, flags & ~O_ACCMODE) ? fd : -1;
47 mkostemps(char *path, int len, int flags)
50 return _gettemp(path, &fd, 0, len, flags & ~O_ACCMODE) ? fd : -1;
56 return _gettemp(path, NULL, 0, 0, 0) ? path : (char *) NULL;
60 _gettemp(char *path, int *doopen, int domkdir, size_t suffixlen, int flags)
62 char *start, *trv, *suffp;
65 if (doopen && domkdir)
71 trv = strchr (path, '\0');
72 if ((size_t) (trv - path) < suffixlen)
80 /* Fill space with random characters */
81 while (trv >= path && *trv == 'X')
83 uint32_t rand = arc4random () % (sizeof (padchar) - 1);
84 *trv-- = padchar[rand];
94 * check the target directory.
97 if (doopen != NULL || domkdir)
99 for (; trv > path; trv--)
104 int rval = stat64 (path, &sbuf);
108 if (!S_ISDIR (sbuf.st_mode))
122 if ((*doopen = open (path, O_CREAT | O_EXCL | O_RDWR | flags,
123 S_IRUSR | S_IWUSR)) >= 0)
130 if (mkdir (path, 0700) == 0)
135 else if (lstat64 (path, &sbuf))
136 return errno == ENOENT;
138 /* If we have a collision, cycle through the space of filenames */
141 if (*trv == '\0' || trv == suffp)
143 pad = strchr (padchar, *trv);
144 if (pad == NULL || *++pad == '\0')
162 char buf[sizeof (int) / 8];
164 int fd = open ("/dev/urandom", O_RDONLY);
171 * Copyright (c) 1987, 1993
172 * The Regents of the University of California. All rights reserved.
174 * Redistribution and use in source and binary forms, with or without
175 * modification, are permitted provided that the following conditions
177 * 1. Redistributions of source code must retain the above copyright
178 * notice, this list of conditions and the following disclaimer.
179 * 2. Redistributions in binary form must reproduce the above copyright
180 * notice, this list of conditions and the following disclaimer in the
181 * documentation and/or other materials provided with the distribution.
182 * 4. Neither the name of the University nor the names of its contributors
183 * may be used to endorse or promote products derived from this software
184 * without specific prior written permission.
186 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
187 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
190 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
192 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
193 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
194 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
195 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF