blob: 69dc4dc6f90619b0220e62c3b4fc15fb054d3b63 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/* THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL OPENBSD SOURCE */
2/* Changes: Removed mktemp */
3
4/*
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char rcsid[] = "$OpenBSD: mktemp.c,v 1.13 1998/06/30 23:03:13 deraadt Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44#include <errno.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <ctype.h>
48#include <unistd.h>
49
Damien Miller3d112ef1999-10-28 13:20:30 +100050#include "helper.h"
51
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052static int _gettemp __P((char *, int *, int, int));
53
54int
55mkstemps(path, slen)
56 char *path;
57 int slen;
58{
59 int fd;
60
61 return (_gettemp(path, &fd, 0, slen) ? fd : -1);
62}
63
64int
65mkstemp(path)
66 char *path;
67{
68 int fd;
69
70 return (_gettemp(path, &fd, 0, 0) ? fd : -1);
71}
72
73char *
74mkdtemp(path)
75 char *path;
76{
77 return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
78}
79
80static int
81_gettemp(path, doopen, domkdir, slen)
82 char *path;
83 register int *doopen;
84 int domkdir;
85 int slen;
86{
87 register char *start, *trv, *suffp;
88 struct stat sbuf;
89 int pid, rval;
90
91 if (doopen && domkdir) {
92 errno = EINVAL;
93 return(0);
94 }
95
96 for (trv = path; *trv; ++trv)
97 ;
98 trv -= slen;
99 suffp = trv;
100 --trv;
101 if (trv < path) {
102 errno = EINVAL;
103 return (0);
104 }
105 pid = getpid();
106 while (*trv == 'X' && pid != 0) {
107 *trv-- = (pid % 10) + '0';
108 pid /= 10;
109 }
110 while (*trv == 'X') {
111 char c;
112
113 pid = (arc4random() & 0xffff) % (26+26);
114 if (pid < 26)
115 c = pid + 'A';
116 else
117 c = (pid - 26) + 'a';
118 *trv-- = c;
119 }
120 start = trv + 1;
121
122 /*
123 * check the target directory; if you have six X's and it
124 * doesn't exist this runs for a *very* long time.
125 */
126 if (doopen || domkdir) {
127 for (;; --trv) {
128 if (trv <= path)
129 break;
130 if (*trv == '/') {
131 *trv = '\0';
132 rval = stat(path, &sbuf);
133 *trv = '/';
134 if (rval != 0)
135 return(0);
136 if (!S_ISDIR(sbuf.st_mode)) {
137 errno = ENOTDIR;
138 return(0);
139 }
140 break;
141 }
142 }
143 }
144
145 for (;;) {
146 if (doopen) {
147 if ((*doopen =
148 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
149 return(1);
150 if (errno != EEXIST)
151 return(0);
152 } else if (domkdir) {
153 if (mkdir(path, 0700) == 0)
154 return(1);
155 if (errno != EEXIST)
156 return(0);
157 } else if (lstat(path, &sbuf))
158 return(errno == ENOENT ? 1 : 0);
159
160 /* tricky little algorithm for backward compatibility */
161 for (trv = start;;) {
162 if (!*trv)
163 return (0);
164 if (*trv == 'Z') {
165 if (trv == suffp)
166 return (0);
167 *trv++ = 'a';
168 } else {
169 if (isdigit(*trv))
170 *trv = 'a';
171 else if (*trv == 'z') /* inc from z to A */
172 *trv = 'A';
173 else {
174 if (trv == suffp)
175 return (0);
176 ++*trv;
177 }
178 break;
179 }
180 }
181 }
182 /*NOTREACHED*/
183}