tempnam.c 554 B

123456789101112131415161718192021222324252627
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #if defined(V9) || defined(BSD4_2) || defined(plan9)
  4. char *tempnam(char *dir, char *pfx) {
  5. int pid;
  6. unsigned int len;
  7. char *tnm, *malloc();
  8. static int seq = 0;
  9. pid = getpid();
  10. len = strlen(dir) + strlen(pfx) + 10;
  11. if ((tnm = malloc(len)) != NULL) {
  12. sprintf(tnm, "%s", dir);
  13. if (access(tnm, 7) == -1)
  14. return(NULL);
  15. do {
  16. sprintf(tnm, "%s/%s%d%d", dir, pfx, pid, seq++);
  17. errno = 0;
  18. if (access(tnm, 7) == -1)
  19. if (errno == ENOENT)
  20. return(tnm);
  21. } while (1);
  22. }
  23. return(tnm);
  24. }
  25. #endif