2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/param.h>
48 #include "un-namespace.h"
50 char *_mktemp(char *);
52 static int _gettemp(int, char *, int *, int, int, int);
54 static const unsigned char padchar[] =
55 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58 mkostempsat(int dfd, char *path, int slen, int oflags)
62 return (_gettemp(dfd, path, &fd, 0, slen, oflags) ? fd : -1);
66 mkostemps(char *path, int slen, int oflags)
70 return (_gettemp(AT_FDCWD, path, &fd, 0, slen, oflags) ? fd : -1);
74 mkstemps(char *path, int slen)
78 return (_gettemp(AT_FDCWD, path, &fd, 0, slen, 0) ? fd : -1);
82 mkostemp(char *path, int oflags)
86 return (_gettemp(AT_FDCWD, path, &fd, 0, 0, oflags) ? fd : -1);
94 return (_gettemp(AT_FDCWD, path, &fd, 0, 0, 0) ? fd : -1);
100 return (_gettemp(AT_FDCWD, path, (int *)NULL, 1, 0, 0) ? path : (char *)NULL);
106 return (_gettemp(AT_FDCWD, path, (int *)NULL, 0, 0, 0) ? path : (char *)NULL);
109 __warn_references(mktemp,
110 "warning: mktemp() possibly used unsafely; consider using mkstemp()");
115 return (_mktemp(path));
119 _gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags)
121 char *start, *trv, *suffp, *carryp;
126 char carrybuf[MAXPATHLEN];
128 if ((doopen != NULL && domkdir) || slen < 0 ||
129 (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC |
135 for (trv = path; *trv != '\0'; ++trv)
137 if (trv - path >= MAXPATHLEN) {
138 errno = ENAMETOOLONG;
144 if (trv < path || NULL != strchr(suffp, '/')) {
149 /* Fill space with random characters */
150 while (trv >= path && *trv == 'X') {
151 rand = arc4random_uniform(sizeof(padchar) - 1);
152 *trv-- = padchar[rand];
156 /* save first combination of random characters */
157 memcpy(carrybuf, start, suffp - start);
160 * check the target directory.
162 if (doopen != NULL || domkdir) {
163 for (; trv > path; --trv) {
166 rval = fstatat(dfd, path, &sbuf, 0);
170 if (!S_ISDIR(sbuf.st_mode)) {
179 oflags |= O_CREAT | O_EXCL | O_RDWR;
182 *doopen = _openat(dfd, path, oflags, 0600);
187 } else if (domkdir) {
188 if (mkdir(path, 0700) == 0)
192 } else if (lstat(path, &sbuf))
193 return (errno == ENOENT);
195 /* If we have a collision, cycle through the space of filenames */
196 for (trv = start, carryp = carrybuf;;) {
197 /* have we tried all possible permutations? */
199 return (0); /* yes - exit with EEXIST */
200 pad = strchr(padchar, *trv);
202 /* this should never happen */
206 /* increment character */
207 *trv = (*++pad == '\0') ? padchar[0] : *pad;
208 /* carry to next position? */
209 if (*trv == *carryp) {
210 /* increment position and loop */
214 /* try with new name */