Changes in uspace/lib/posix/stdlib.c [11544f4:102a729] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdlib.c
r11544f4 r102a729 40 40 41 41 #include "errno.h" 42 #include "fcntl.h"43 42 #include "limits.h" 44 #include "string.h"45 #include "sys/stat.h"46 #include "unistd.h"47 43 48 44 #include "libc/sort.h" … … 389 385 390 386 /** 391 * Creates and opens an unique temporary file from template. 392 * 393 * @param tmpl Template. Last six characters must be XXXXXX. 394 * @return The opened file descriptor or -1 on error. 395 */ 396 int posix_mkstemp(char *tmpl) 397 { 398 int fd = -1; 399 400 char *tptr = tmpl + posix_strlen(tmpl) - 6; 401 402 while (fd < 0) { 403 if (*posix_mktemp(tmpl) == '\0') { 404 /* Errno set by mktemp(). */ 405 return -1; 406 } 407 408 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 409 410 if (fd == -1) { 411 /* Restore template to it's original state. */ 412 snprintf(tptr, 7, "XXXXXX"); 413 } 414 } 415 416 return fd; 417 } 418 419 /** 420 * Creates an unique temporary file name from template. 421 * 422 * @param tmpl Template. Last six characters must be XXXXXX. 423 * @return The value of tmpl. The template is modified in place. 424 * If no temporary file name can be created, template is 425 * reduced to an empty string. 387 * 388 * @param tmpl 389 * @return 426 390 */ 427 391 char *posix_mktemp(char *tmpl) 428 392 { 429 int tmpl_len = posix_strlen(tmpl); 430 if (tmpl_len < 6) { 431 errno = EINVAL; 432 *tmpl = '\0'; 433 return tmpl; 434 } 435 436 char *tptr = tmpl + tmpl_len - 6; 437 if (posix_strcmp(tptr, "XXXXXX") != 0) { 438 errno = EINVAL; 439 *tmpl = '\0'; 440 return tmpl; 441 } 442 443 static int seq = 0; 444 445 for (; seq < 1000000; ++seq) { 446 snprintf(tptr, 7, "%06d", seq); 447 448 int orig_errno = errno; 449 errno = 0; 450 /* Check if the file exists. */ 451 if (posix_access(tmpl, F_OK) == -1) { 452 if (errno == ENOENT) { 453 errno = orig_errno; 454 break; 455 } else { 456 /* errno set by access() */ 457 *tmpl = '\0'; 458 return tmpl; 459 } 460 } 461 } 462 463 if (seq == 10000000) { 464 errno = EEXIST; 465 *tmpl = '\0'; 466 return tmpl; 467 } 468 469 return tmpl; 393 // TODO: low priority, just a compile-time dependency of binutils 394 not_implemented(); 470 395 } 471 396
Note:
See TracChangeset
for help on using the changeset viewer.