summaryrefslogtreecommitdiff
path: root/sys/src/cmd/postscript/common/tempnam.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/postscript/common/tempnam.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/postscript/common/tempnam.c')
-rwxr-xr-xsys/src/cmd/postscript/common/tempnam.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/sys/src/cmd/postscript/common/tempnam.c b/sys/src/cmd/postscript/common/tempnam.c
new file mode 100755
index 000000000..00d819cab
--- /dev/null
+++ b/sys/src/cmd/postscript/common/tempnam.c
@@ -0,0 +1,36 @@
+#if defined(V9) || defined(BSD4_2) || defined(plan9)
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+char *
+tempnam(char *dir, char *pfx)
+{
+ int pid;
+ char *tnm;
+ struct stat stb;
+ static int seq = 0;
+
+ if (dir == NULL)
+ dir = ".";
+#ifdef plan9
+ /* our access emulation has a race when checking for write access */
+ if (access(dir, R_OK|X_OK) == -1)
+#else
+ if (access(dir, R_OK|W_OK|X_OK) == -1)
+#endif
+ return NULL;
+ pid = getpid();
+ tnm = malloc(strlen(dir) + 1 + strlen(pfx) + 2*20 + 1);
+ if (tnm == NULL)
+ return NULL;
+ do {
+ sprintf(tnm, "%s/%s.%d.%d", dir, pfx, pid, seq++);
+ errno = 0;
+ } while (stat(tnm, &stb) >= 0 && seq < 256);
+ return tnm;
+}
+#endif