mkstemp.c 564 B

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