diff options
author | Cedric Bail <cedric.bail@free.fr> | 2013-03-31 00:09:37 +0900 |
---|---|---|
committer | Cedric Bail <cedric.bail@free.fr> | 2013-03-31 00:13:06 +0900 |
commit | 1c50ffe83f41a6859e7595fd60851cd2b9b8e39b (patch) | |
tree | ba79e63707e3a60e7eb208f1cec6d9a2a833f9bb /src/lib/evil | |
parent | fe409a5b425558b3e1edbeaf76b0e1e97ba31295 (diff) |
Evil: add mkdtemp
Diffstat (limited to 'src/lib/evil')
-rw-r--r-- | src/lib/evil/evil_stdlib.c | 110 | ||||
-rw-r--r-- | src/lib/evil/evil_stdlib.h | 7 |
2 files changed, 89 insertions, 28 deletions
diff --git a/src/lib/evil/evil_stdlib.c b/src/lib/evil/evil_stdlib.c index ffd9acde9a..b7c814ec44 100644 --- a/src/lib/evil/evil_stdlib.c +++ b/src/lib/evil/evil_stdlib.c | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <io.h> | 7 | #include <io.h> |
8 | #include <fcntl.h> | 8 | #include <fcntl.h> |
9 | #include <sys/stat.h> | 9 | #include <sys/stat.h> |
10 | #include <sys/types.h> | ||
10 | 11 | ||
11 | #include <errno.h> | 12 | #include <errno.h> |
12 | 13 | ||
@@ -269,51 +270,106 @@ unsetenv(const char *name) | |||
269 | * Files related functions | 270 | * Files related functions |
270 | * | 271 | * |
271 | */ | 272 | */ |
273 | static int | ||
274 | _mkstemp_init(char *__template, char **suffix, size_t *length, DWORD *val) | ||
275 | { | ||
276 | *length = strlen(__template); | ||
277 | if ((*length < 6) || | ||
278 | (strncmp (__template + *length - 6, "XXXXXX", 6))) | ||
279 | { | ||
280 | errno = EINVAL; | ||
281 | return 0; | ||
282 | } | ||
272 | 283 | ||
273 | int | 284 | *suffix = __template + *length - 6; |
274 | mkstemp(char *__template) | 285 | |
286 | *val = GetTickCount(); | ||
287 | *val += GetCurrentProcessId(); | ||
288 | |||
289 | return 1; | ||
290 | } | ||
291 | |||
292 | static int | ||
293 | _mkstemp(char *suffix, int val) | ||
275 | { | 294 | { |
276 | const char lookup[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | 295 | const char lookup[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
296 | DWORD v = val; | ||
297 | |||
298 | suffix[0] = lookup[v % 62]; | ||
299 | v /= 62; | ||
300 | suffix[1] = lookup[v % 62]; | ||
301 | v /= 62; | ||
302 | suffix[2] = lookup[v % 62]; | ||
303 | v /= 62; | ||
304 | suffix[3] = lookup[v % 62]; | ||
305 | v /= 62; | ||
306 | suffix[4] = lookup[v % 62]; | ||
307 | v /= 62; | ||
308 | suffix[5] = lookup[v % 62]; | ||
309 | v /= 62; | ||
310 | |||
311 | val += 7777; | ||
312 | |||
313 | return v; | ||
314 | } | ||
315 | |||
316 | EAPI char * | ||
317 | mkdtemp(char *__template) | ||
318 | { | ||
277 | char *suffix; | 319 | char *suffix; |
278 | DWORD val; | 320 | DWORD val; |
279 | size_t length; | 321 | size_t length; |
280 | int i; | 322 | int i; |
281 | 323 | ||
282 | if (!__template) | 324 | if (!__template) |
283 | return 0; | ||
284 | |||
285 | length = strlen(__template); | ||
286 | if ((length < 6) || | ||
287 | (strncmp (__template + length - 6, "XXXXXX", 6))) | ||
288 | { | 325 | { |
289 | errno = EINVAL; | 326 | errno = EINVAL; |
290 | return -1; | 327 | return NULL; |
291 | } | 328 | } |
292 | 329 | ||
293 | suffix = __template + length - 6; | 330 | if (!_mkstemp_init(__template, &suffix, &length, &val)) |
331 | return NULL; | ||
294 | 332 | ||
295 | val = GetTickCount(); | 333 | for (i = 0; i < 32768; i++) |
296 | val += GetCurrentProcessId(); | 334 | { |
335 | val = _mkstemp(suffix, val); | ||
336 | |||
337 | if (mkdir(__template)) | ||
338 | return __template; | ||
339 | |||
340 | if (errno == EFAULT || | ||
341 | errno == ENOSPC || | ||
342 | errno == ENOMEM || | ||
343 | errno == ENOENT || | ||
344 | errno == ENOTDIR || | ||
345 | errno == EPERM || | ||
346 | errno == EROFS) | ||
347 | return NULL; | ||
348 | } | ||
349 | |||
350 | errno = EEXIST; | ||
351 | return NULL; | ||
352 | } | ||
353 | |||
354 | int | ||
355 | mkstemp(char *__template) | ||
356 | { | ||
357 | char *suffix; | ||
358 | DWORD val; | ||
359 | size_t length; | ||
360 | int i; | ||
361 | |||
362 | if (!__template) | ||
363 | return 0; | ||
364 | |||
365 | if (!_mkstemp_init(__template, &suffix, &length, &val)) | ||
366 | return -1; | ||
297 | 367 | ||
298 | for (i = 0; i < 32768; i++) | 368 | for (i = 0; i < 32768; i++) |
299 | { | 369 | { |
300 | DWORD v; | ||
301 | int fd; | 370 | int fd; |
302 | 371 | ||
303 | v = val; | 372 | val = _mkstemp(suffix, val); |
304 | |||
305 | suffix[0] = lookup[v % 62]; | ||
306 | v /= 62; | ||
307 | suffix[1] = lookup[v % 62]; | ||
308 | v /= 62; | ||
309 | suffix[2] = lookup[v % 62]; | ||
310 | v /= 62; | ||
311 | suffix[3] = lookup[v % 62]; | ||
312 | v /= 62; | ||
313 | suffix[4] = lookup[v % 62]; | ||
314 | v /= 62; | ||
315 | suffix[5] = lookup[v % 62]; | ||
316 | v /= 62; | ||
317 | 373 | ||
318 | #ifndef __MINGW32CE__ | 374 | #ifndef __MINGW32CE__ |
319 | fd = _open(__template, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE); | 375 | fd = _open(__template, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE); |
@@ -337,8 +393,6 @@ mkstemp(char *__template) | |||
337 | #endif /* __MINGW32CE__ */ | 393 | #endif /* __MINGW32CE__ */ |
338 | if (fd >= 0) | 394 | if (fd >= 0) |
339 | return fd; | 395 | return fd; |
340 | |||
341 | val += 7777; | ||
342 | } | 396 | } |
343 | 397 | ||
344 | errno = EEXIST; | 398 | errno = EEXIST; |
diff --git a/src/lib/evil/evil_stdlib.h b/src/lib/evil/evil_stdlib.h index b102b66f8e..52318dc964 100644 --- a/src/lib/evil/evil_stdlib.h +++ b/src/lib/evil/evil_stdlib.h | |||
@@ -160,6 +160,13 @@ EAPI int unsetenv(const char *name); | |||
160 | EAPI int mkstemp(char *__template); | 160 | EAPI int mkstemp(char *__template); |
161 | 161 | ||
162 | /** | 162 | /** |
163 | * @brief create an unique temporary directory | ||
164 | * | ||
165 | * @since 1.8.0 | ||
166 | */ | ||
167 | EAPI char *mkdtemp(char *__template); | ||
168 | |||
169 | /** | ||
163 | * @brief Return an absolute or full path name for a specified relative path name. | 170 | * @brief Return an absolute or full path name for a specified relative path name. |
164 | * | 171 | * |
165 | * @param file_name The absolute path name. | 172 | * @param file_name The absolute path name. |