diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/ap/stdio/freopen.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/ap/stdio/freopen.c')
-rwxr-xr-x | sys/src/ape/lib/ap/stdio/freopen.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/stdio/freopen.c b/sys/src/ape/lib/ap/stdio/freopen.c new file mode 100755 index 000000000..ba586bbfc --- /dev/null +++ b/sys/src/ape/lib/ap/stdio/freopen.c @@ -0,0 +1,66 @@ +/* + * pANS stdio -- freopen + */ +#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 *freopen(const char *name, const char *mode, FILE *f){ + int m; + + if(f->state!=CLOSED){ + fclose(f); +/* premature; fall through and see what happens */ +/* f->state=OPEN; */ + } + + m = *mode++; + if(m == 0) + return NULL; + if(*mode == 'b') + mode++; + switch(m){ + default: + return NULL; + case 'r': + f->fd=open(name, (*mode == '+'? O_RDWR: O_RDONLY)); + break; + case 'w': + f->fd=creat(name, 0666); /* implicitly O_WRONLY */ + /* for O_RDWR, have to creat, close, open */ + if(*mode == '+' && f->fd >= 0) { + close(f->fd); + f->fd=open(name, O_RDWR); + } + break; + case 'a': + f->fd=open(name, (*mode == '+'? O_RDWR: O_WRONLY)); + if(f->fd<0) { + f->fd=creat(name, 0666); + /* for O_RDWR, have to creat, close, open */ + if(*mode == '+' && f->fd >= 0) { + close(f->fd); + f->fd=open(name, O_RDWR); + } + } + lseek(f->fd, 0L, 2); + break; + } + + if(f->fd==-1) + return NULL; + f->flags=(mode[0]=='a')? APPEND : 0; + f->state=OPEN; + f->buf=0; + f->rp=0; + f->wp=0; + f->lp=0; + return f; +} |