1
0

mkostemps.c 647 B

123456789101112131415161718192021222324252627282930
  1. #define _BSD_SOURCE
  2. #include <string.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include "libc.h"
  7. char *__randname(char *);
  8. int __mkostemps(char *template, int len, int flags)
  9. {
  10. size_t l = strlen(template);
  11. if (l<6 || len>l-6 || memcmp(template+l-len-6, "XXXXXX", 6)) {
  12. errno = EINVAL;
  13. return -1;
  14. }
  15. int fd, retries = 100;
  16. do {
  17. __randname(template+l-len-6);
  18. if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)
  19. return fd;
  20. } while (--retries && errno == EEXIST);
  21. memcpy(template+l-len-6, "XXXXXX", 6);
  22. return -1;
  23. }
  24. weak_alias(__mkostemps, mkostemps);
  25. weak_alias(__mkostemps, mkostemps64);