1 /*
2 * Posix stdio -- fdopen
3 */
4 #include "iolib.h"
5 /*
6 * Open the named file with the given mode, using the given FILE
7 * Legal modes are given below, `additional characters may follow these sequences':
8 * r rb open to read
9 * w wb open to write, truncating
10 * a ab open to write positioned at eof, creating if non-existant
11 * r+ r+b rb+ open to read and write, creating if non-existant
12 * w+ w+b wb+ open to read and write, truncating
13 * a+ a+b ab+ open to read and write, positioned at eof, creating if non-existant.
14 */
fdopen(const int fd,const char * mode)15 FILE *fdopen(const int fd, const char *mode){
16 FILE *f;
17 qlock(&_stdiolk);
18 for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
19 if(f->state==CLOSED)
20 break;
21 if(f==&_IO_stream[FOPEN_MAX]) {
22 qunlock(&_stdiolk);
23 return NULL;
24 }
25 f->fd=fd;
26 if(mode[0]=='a')
27 seek(f->fd, 0L, 2);
28 if(f->fd==-1) return NULL;
29 f->flags=0;
30 f->state=OPEN;
31 f->buf=0;
32 f->rp=0;
33 f->wp=0;
34 f->lp=0;
35 qunlock(&_stdiolk);
36 return f;
37 }
38