xref: /haiku/src/system/libroot/posix/stdlib/mktemp.c (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
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 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <unistd.h>
43 #include <stdint.h>
44 
45 
46 static int _gettemp(char *, int *, int, int);
47 
48 static const unsigned char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
49 
50 
51 #if 0
52 int
53 mkstemps(char *path, int slen)
54 {
55 	int fd;
56 	return _gettemp(path, &fd, 0, slen) ? fd : -1;
57 }
58 #endif
59 
60 
61 int
62 mkstemp(char *path)
63 {
64 	int fd;
65 	if (_gettemp(path, &fd, 0, 0))
66 		return fd;
67 
68 	return -1;
69 }
70 
71 
72 char *
73 mkdtemp(path)
74 	char *path;
75 {
76 	return (_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
77 }
78 
79 
80 char *
81 mktemp(char *path)
82 {
83 	if (_gettemp(path, (int *)NULL, 0, 0))
84 		return path;
85 
86 	return NULL;
87 }
88 
89 
90 static int
91 _gettemp(char *path, int *doopen, int domkdir, int slen)
92 {
93 	char *start, *trv, *suffp;
94 	char *pad;
95 	struct stat sbuf;
96 	int rval;
97 
98 	if (doopen != NULL && domkdir) {
99 		errno = EINVAL;
100 		return 0;
101 	}
102 
103 	for (trv = path; *trv != '\0'; ++trv)
104 		;
105 
106 	trv -= slen;
107 	suffp = trv;
108 	--trv;
109 	if (trv < path) {
110 		errno = EINVAL;
111 		return 0;
112 	}
113 
114 	/* Fill space with random characters */
115 	while (trv >= path && *trv == 'X') {
116 		uint32_t value = rand() % (sizeof(padchar) - 1);
117 		*trv-- = padchar[value];
118 	}
119 	start = trv + 1;
120 
121 	/*
122 	 * check the target directory.
123 	 */
124 	if (doopen != NULL || domkdir) {
125 		for (; trv > path; --trv) {
126 			if (*trv == '/') {
127 				*trv = '\0';
128 				rval = stat(path, &sbuf);
129 				*trv = '/';
130 				if (rval != 0)
131 					return 0;
132 				if (!S_ISDIR(sbuf.st_mode)) {
133 					errno = ENOTDIR;
134 					return 0;
135 				}
136 				break;
137 			}
138 		}
139 	}
140 
141 	for (;;) {
142 		if (doopen) {
143 			if ((*doopen = open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
144 				return 1;
145 			if (errno != EEXIST)
146 				return 0;
147 		} else if (domkdir) {
148 			if (mkdir(path, 0700) == 0)
149 				return 1;
150 			if (errno != EEXIST)
151 				return 0;
152 		} else if (lstat(path, &sbuf))
153 			return errno == ENOENT;
154 
155 		/* If we have a collision, cycle through the space of filenames */
156 		for (trv = start;;) {
157 			if (*trv == '\0' || trv == suffp)
158 				return 0;
159 			pad = strchr(padchar, *trv);
160 			if (pad == NULL || *++pad == '\0')
161 				*trv++ = padchar[0];
162 			else {
163 				*trv++ = *pad;
164 				break;
165 			}
166 		}
167 	}
168 
169 	/* not reached */
170 }
171