]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/lib/libc/stdio/mktemp.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / lib / libc / stdio / mktemp.c
1 /*
2  * Copyright (c) 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)mktemp.c    8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <unistd.h>
50 #include "un-namespace.h"
51
52 char *_mktemp(char *);
53
54 static int _gettemp(char *, int *, int, int);
55
56 static const unsigned char padchar[] =
57 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58
59 int
60 mkstemps(path, slen)
61         char *path;
62         int slen;
63 {
64         int fd;
65
66         return (_gettemp(path, &fd, 0, slen) ? fd : -1);
67 }
68
69 int
70 mkstemp(path)
71         char *path;
72 {
73         int fd;
74
75         return (_gettemp(path, &fd, 0, 0) ? fd : -1);
76 }
77
78 char *
79 mkdtemp(path)
80         char *path;
81 {
82         return (_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
83 }
84
85 char *
86 _mktemp(path)
87         char *path;
88 {
89         return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
90 }
91
92 __warn_references(mktemp,
93     "warning: mktemp() possibly used unsafely; consider using mkstemp()");
94
95 char *
96 mktemp(path)
97         char *path;
98 {
99         return (_mktemp(path));
100 }
101
102 static int
103 _gettemp(path, doopen, domkdir, slen)
104         char *path;
105         int *doopen;
106         int domkdir;
107         int slen;
108 {
109         char *start, *trv, *suffp, *carryp;
110         char *pad;
111         struct stat sbuf;
112         int rval;
113         uint32_t rand;
114         char carrybuf[MAXPATHLEN];
115
116         if ((doopen != NULL && domkdir) || slen < 0) {
117                 errno = EINVAL;
118                 return (0);
119         }
120
121         for (trv = path; *trv != '\0'; ++trv)
122                 ;
123         trv -= slen;
124         suffp = trv;
125         --trv;
126         if (trv < path || NULL != strchr(suffp, '/')) {
127                 errno = EINVAL;
128                 return (0);
129         }
130
131         /* Fill space with random characters */
132         while (trv >= path && *trv == 'X') {
133                 rand = arc4random() % (sizeof(padchar) - 1);
134                 *trv-- = padchar[rand];
135         }
136         start = trv + 1;
137
138         /* save first combination of random characters */
139         memcpy(carrybuf, start, suffp - start);
140
141         /*
142          * check the target directory.
143          */
144         if (doopen != NULL || domkdir) {
145                 for (; trv > path; --trv) {
146                         if (*trv == '/') {
147                                 *trv = '\0';
148                                 rval = stat(path, &sbuf);
149                                 *trv = '/';
150                                 if (rval != 0)
151                                         return (0);
152                                 if (!S_ISDIR(sbuf.st_mode)) {
153                                         errno = ENOTDIR;
154                                         return (0);
155                                 }
156                                 break;
157                         }
158                 }
159         }
160
161         for (;;) {
162                 if (doopen) {
163                         if ((*doopen =
164                             _open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
165                                 return (1);
166                         if (errno != EEXIST)
167                                 return (0);
168                 } else if (domkdir) {
169                         if (mkdir(path, 0700) == 0)
170                                 return (1);
171                         if (errno != EEXIST)
172                                 return (0);
173                 } else if (lstat(path, &sbuf))
174                         return (errno == ENOENT);
175
176                 /* If we have a collision, cycle through the space of filenames */
177                 for (trv = start, carryp = carrybuf;;) {
178                         /* have we tried all possible permutations? */
179                         if (trv == suffp)
180                                 return (0); /* yes - exit with EEXIST */
181                         pad = strchr(padchar, *trv);
182                         if (pad == NULL) {
183                                 /* this should never happen */
184                                 errno = EIO;
185                                 return (0);
186                         }
187                         /* increment character */
188                         *trv = (*++pad == '\0') ? padchar[0] : *pad;
189                         /* carry to next position? */
190                         if (*trv == *carryp) {
191                                 /* increment position and loop */
192                                 ++trv;
193                                 ++carryp;
194                         } else {
195                                 /* try with new name */
196                                 break;
197                         }
198                 }
199         }
200         /*NOTREACHED*/
201 }