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