#include "c.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Go to the source code of this file.
|
static int | GETTEMP (char *path, int *doopen, int domkdir) |
|
char * | mkdtemp (char *path) |
|
◆ _DIAGASSERT
#define _DIAGASSERT |
( |
|
x | ) |
do {} while (0) |
◆ GETTEMP()
static int GETTEMP |
( |
char * |
path, |
|
|
int * |
doopen, |
|
|
int |
domkdir |
|
) |
| |
|
static |
Definition at line 94 of file mkdtemp.c.
95{
97 *trv;
99 u_int pid;
100
101
102
103
104
105
106 static char xtra[2] = "aa";
107 int xcnt = 0;
108
110
111
112 pid = getpid();
113
114
115 for (trv = path; *trv; ++trv)
116 if (*trv == 'X')
117 xcnt++;
118 else
119 xcnt = 0;
120
121
122 if (xcnt > 0)
123 {
124 *--trv = xtra[0];
125 xcnt--;
126 }
127 if (xcnt > 5)
128 {
129 *--trv = xtra[1];
130 xcnt--;
131 }
132
133
134 for (; xcnt > 0; xcnt--)
135 {
136 *--trv = (pid % 10) + '0';
137 pid /= 10;
138 }
139
140
141 if (xtra[0] != 'z')
142 xtra[0]++;
143 else
144 {
145 xtra[0] = 'a';
146 if (xtra[1] != 'z')
147 xtra[1]++;
148 else
149 xtra[1] = 'a';
150 }
151
152
153
154
155
156 for (
start = trv + 1;; --trv)
157 {
158 if (trv <= path)
159 break;
160 if (*trv == '/')
161 {
163
164 *trv = '\0';
165 e =
stat(path, &sbuf);
166 *trv = '/';
168 return doopen == NULL && !domkdir;
170 {
171 errno = ENOTDIR;
172 return doopen == NULL && !domkdir;
173 }
174 break;
175 }
176 }
177
178 for (;;)
179 {
180 if (doopen)
181 {
182 if ((*doopen =
183 open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
184 return 1;
185 if (errno != EEXIST)
186 return 0;
187 }
188 else if (domkdir)
189 {
190 if (
mkdir(path, 0700) >= 0)
191 return 1;
192 if (errno != EEXIST)
193 return 0;
194 }
195 else if (
lstat(path, &sbuf))
196 return errno == ENOENT ? 1 : 0;
197
198
200 {
201 if (!*trv)
202 return 0;
203 if (*trv == 'z')
204 *trv++ = 'a';
205 else
206 {
207 if (isdigit((unsigned char) *trv))
208 *trv = 'a';
209 else
210 ++*trv;
211 break;
212 }
213 }
214 }
215
216}
References _DIAGASSERT, lstat, mkdir, S_ISDIR, stat::st_mode, start, and stat.
Referenced by mkdtemp().
◆ mkdtemp()
char * mkdtemp |
( |
char * |
path | ) |
|