mktemp.c 395 B

12345678910111213141516171819202122232425262728293031
  1. #include <u.h>
  2. #include <libc.h>
  3. char*
  4. mktemp(char *as)
  5. {
  6. char *s;
  7. unsigned pid;
  8. int i;
  9. char err[ERRMAX];
  10. pid = getpid();
  11. s = as;
  12. while(*s++)
  13. ;
  14. s--;
  15. while(*--s == 'X') {
  16. *s = pid % 10 + '0';
  17. pid = pid/10;
  18. }
  19. s++;
  20. i = 'a';
  21. while(access(as, 0) != -1) {
  22. if (i == 'z')
  23. return "/";
  24. *s = i++;
  25. }
  26. err[0] = '\0';
  27. errstr(err, sizeof err); /* clear the error */
  28. return as;
  29. }