summaryrefslogtreecommitdiffstats
path: root/src/cmd/postscript/common/tempnam.c
diff options
context:
space:
mode:
authorwkj <devnull@localhost>2004-05-16 07:54:22 +0000
committerwkj <devnull@localhost>2004-05-16 07:54:22 +0000
commitb855148c9b6d28fedfd083d037bcf246f1913d92 (patch)
tree0075eb6ea37427fa48b78cb937fabc04175cead1 /src/cmd/postscript/common/tempnam.c
parent61f5c35c9465f0702739b41249a664d409f0482c (diff)
downloadplan9port-b855148c9b6d28fedfd083d037bcf246f1913d92.tar.gz
plan9port-b855148c9b6d28fedfd083d037bcf246f1913d92.zip
Checkpoint.
Diffstat (limited to 'src/cmd/postscript/common/tempnam.c')
-rw-r--r--src/cmd/postscript/common/tempnam.c83
1 files changed, 59 insertions, 24 deletions
diff --git a/src/cmd/postscript/common/tempnam.c b/src/cmd/postscript/common/tempnam.c
index 529025ed..a759876e 100644
--- a/src/cmd/postscript/common/tempnam.c
+++ b/src/cmd/postscript/common/tempnam.c
@@ -1,27 +1,62 @@
#include <stdio.h>
-#include <errno.h>
-
-#if defined(V9) || defined(BSD4_2) || defined(plan9)
-char *tempnam(char *dir, char *pfx) {
- int pid;
- unsigned int len;
- char *tnm, *malloc();
- static int seq = 0;
-
- pid = getpid();
- len = strlen(dir) + strlen(pfx) + 10;
- if ((tnm = malloc(len)) != NULL) {
- sprintf(tnm, "%s", dir);
- if (access(tnm, 7) == -1)
- return(NULL);
- do {
- sprintf(tnm, "%s/%s%d%d", dir, pfx, pid, seq++);
- errno = 0;
- if (access(tnm, 7) == -1)
- if (errno == ENOENT)
- return(tnm);
- } while (1);
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define nil ((void*)0)
+
+char*
+mkfname(char *tmpdir, char *prefix)
+{
+ int n;
+ char *p, *fname;
+
+ if((p = getenv("TMPDIR")) != nil)
+ goto Mktemp;
+ if((p = tmpdir) != nil)
+ goto Mktemp;
+ p = "/tmp";
+
+ Mktemp:
+ n = strlen(p)+1+strlen(prefix)+1+8+1;
+ if((fname = malloc(n)) == nil)
+ return nil;
+ memset(fname, 0, n);
+ strcat(fname, p);
+ if((n = strlen(p)) > 0 && p[n-1] != '/')
+ strcat(fname, "/");
+ strcat(fname, prefix);
+ strcat(fname, ".XXXXXXXX");
+
+ return fname;
+}
+
+extern int mkstemp();
+
+char*
+safe_tempnam(char *tmpdir, char *prefix)
+{
+ int fd;
+ char *fname;
+
+ if((fname = mkfname(tmpdir, prefix)) == nil)
+ return nil;
+
+ if((fd = mkstemp(fname)) < 0){ /* XXX: leak fd, fname */
+ free(fname);
+ return nil;
}
- return(tnm);
+ return fname;
+}
+
+int
+safe_tmpnam(char *fname)
+{
+ char *p;
+
+ if((p = mkfname(nil, "tmpfile")) == nil)
+ return -1;
+ strcpy(fname, p);
+ free(p);
+ return mkstemp(fname);
}
-#endif