mktemp.c 618 B

123456789101112131415161718192021222324252627282930
  1. /* $Id$ */
  2. /* mktemp - make a name for a temporary file; only here for backwards compat */
  3. /* no _-protected system-calls? */
  4. unsigned int getpid(void);
  5. int access(char *, int);
  6. char *mktemp(char *template)
  7. {
  8. register int pid, k;
  9. register char *p;
  10. pid = getpid(); /* get process id as semi-unique number */
  11. p = template;
  12. while (*p) p++; /* find end of string */
  13. /* Replace XXXXXX at end of template with pid. */
  14. while (*--p == 'X') {
  15. *p = '0' + (pid % 10);
  16. pid /= 10;
  17. }
  18. p++;
  19. for (k = 'a'; k <= 'z'; k++) {
  20. *p = k;
  21. if (access(template, 0) < 0) {
  22. return template;
  23. }
  24. }
  25. return("/");
  26. }