mktemp.c 533 B

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