mkdtemp.c 582 B

1234567891011121314151617181920212223242526
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8. #include <sys/stat.h>
  9. #include "libc.h"
  10. char *__mktemp(char *);
  11. char *mkdtemp(char *template)
  12. {
  13. int retries = 100, t0 = *template;
  14. while (retries--) {
  15. if (!*__mktemp(template)) return 0;
  16. if (!mkdir(template, 0700)) return template;
  17. if (errno != EEXIST) return 0;
  18. /* this is safe because mktemp verified
  19. * that we have a valid template string */
  20. template[0] = t0;
  21. strcpy(template+strlen(template)-6, "XXXXXX");
  22. }
  23. return 0;
  24. }