mktemp.c 593 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <string.h>
  2. #include <features.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. char *mktemp(template)
  7. char *template;
  8. {
  9. int i;
  10. int num __attribute__ ((unused)); /* UNINITIALIZED */
  11. int n2;
  12. int l = strlen(template);
  13. struct stat stbuf;
  14. if (l < 6) {
  15. errno = EINVAL;
  16. return 0;
  17. }
  18. for (i = l - 6; i < l; i++)
  19. if (template[i] != 'X') {
  20. errno = EINVAL;
  21. return 0;
  22. }
  23. again:
  24. n2 = num;
  25. for (i = l - 1; i >= l - 6; i--) {
  26. template[i] = '0' + n2 % 10;
  27. n2 /= 10;
  28. }
  29. if (stat(template, &stbuf) == 0) {
  30. num++;
  31. goto again;
  32. }
  33. return template;
  34. }