summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/plan9/tmpfile.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/ape/lib/ap/plan9/tmpfile.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/ap/plan9/tmpfile.c')
-rwxr-xr-xsys/src/ape/lib/ap/plan9/tmpfile.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/plan9/tmpfile.c b/sys/src/ape/lib/ap/plan9/tmpfile.c
new file mode 100755
index 000000000..c3ab5fd88
--- /dev/null
+++ b/sys/src/ape/lib/ap/plan9/tmpfile.c
@@ -0,0 +1,44 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include "sys9.h"
+#undef OPEN
+#include "../stdio/iolib.h"
+#include "lib.h"
+#include "dir.h"
+
+FILE *
+tmpfile(void){
+ FILE *f;
+ static char name[]="/tmp/tf0000000000000";
+ char *p;
+ int n;
+ for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
+ if(f->state==CLOSED)
+ break;
+ if(f==&_IO_stream[FOPEN_MAX])
+ return NULL;
+ while(access(name, 0) >= 0){
+ p = name+7;
+ while(*p == '9')
+ *p++ = '0';
+ if(*p == '\0')
+ return NULL;
+ ++*p;
+ }
+ n = _CREATE(name, 64|2, 0777); /* remove-on-close */
+ if(n==-1){
+ _syserrno();
+ return NULL;
+ }
+ _fdinfo[n].flags = FD_ISOPEN;
+ _fdinfo[n].oflags = 2;
+ f->fd=n;
+ f->flags=0;
+ f->state=OPEN;
+ f->buf=0;
+ f->rp=0;
+ f->wp=0;
+ f->lp=0;
+ return f;
+}