1 #if defined(V9) || defined(BSD4_2) || defined(plan9)
2 #define _BSD_EXTENSION
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 char *
tempnam(char * dir,char * pfx)13 tempnam(char *dir, char *pfx)
14 {
15 int pid;
16 char *tnm;
17 struct stat stb;
18 static int seq = 0;
19
20 if (dir == NULL)
21 dir = ".";
22 #ifdef plan9
23 /* our access emulation has a race when checking for write access */
24 if (access(dir, R_OK|X_OK) == -1)
25 #else
26 if (access(dir, R_OK|W_OK|X_OK) == -1)
27 #endif
28 return NULL;
29 pid = getpid();
30 tnm = malloc(strlen(dir) + 1 + strlen(pfx) + 2*20 + 1);
31 if (tnm == NULL)
32 return NULL;
33 do {
34 sprintf(tnm, "%s/%s.%d.%d", dir, pfx, pid, seq++);
35 errno = 0;
36 } while (stat(tnm, &stb) >= 0 && seq < 256);
37 return tnm;
38 }
39 #endif
40