summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/stdio/fdopen.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/stdio/fdopen.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/ap/stdio/fdopen.c')
-rwxr-xr-xsys/src/ape/lib/ap/stdio/fdopen.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/stdio/fdopen.c b/sys/src/ape/lib/ap/stdio/fdopen.c
new file mode 100755
index 000000000..0c585b965
--- /dev/null
+++ b/sys/src/ape/lib/ap/stdio/fdopen.c
@@ -0,0 +1,33 @@
+/*
+ * Posix stdio -- fdopen
+ */
+#include "iolib.h"
+/*
+ * Open the named file with the given mode, using the given FILE
+ * Legal modes are given below, `additional characters may follow these sequences':
+ * r rb open to read
+ * w wb open to write, truncating
+ * a ab open to write positioned at eof, creating if non-existant
+ * r+ r+b rb+ open to read and write, creating if non-existant
+ * w+ w+b wb+ open to read and write, truncating
+ * a+ a+b ab+ open to read and write, positioned at eof, creating if non-existant.
+ */
+FILE *fdopen(const int fd, const char *mode){
+ FILE *f;
+ for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
+ if(f->state==CLOSED)
+ break;
+ if(f==&_IO_stream[FOPEN_MAX])
+ return NULL;
+ f->fd=fd;
+ if(mode[0]=='a')
+ lseek(f->fd, 0L, 2);
+ if(f->fd==-1) return NULL;
+ f->flags=0;
+ f->state=OPEN;
+ f->buf=0;
+ f->rp=0;
+ f->wp=0;
+ f->lp=0;
+ return f;
+}