mkstemp.c 659 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <string.h>
  2. #include <features.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. int mkstemp(template)
  7. char *template;
  8. {
  9. int i;
  10. int num __attribute__ ((unused)); /* UNINITIALIZED */
  11. int n2;
  12. int l = strlen(template);
  13. if (l < 6) {
  14. __set_errno(EINVAL);
  15. return -1;
  16. }
  17. for (i = l - 6; i < l; i++)
  18. if (template[i] != 'X') {
  19. __set_errno(EINVAL);
  20. return -1;
  21. }
  22. again:
  23. n2 = num;
  24. for (i = l - 1; i >= l - 6; i--) {
  25. template[i] = '0' + n2 % 10;
  26. n2 /= 10;
  27. }
  28. i = open(template, O_RDWR | O_EXCL | O_CREAT, 0666);
  29. if (i == -1) {
  30. if (errno == EEXIST) {
  31. num++;
  32. goto again;
  33. } else
  34. return -1;
  35. }
  36. return i;
  37. }