X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fmtio.c;h=957cbc21b90cc1714689629e7a0f0c081005ba19;hb=2931529e223c46eb32293bd5b104a84ca4df5ac6;hp=b2f55f8ef6e2c9ded84dbee955617f450f0adb06;hpb=c3424008c9b9b1ee9ffff7ca4e92812b9021ad93;p=ashd.git diff --git a/lib/mtio.c b/lib/mtio.c index b2f55f8..957cbc2 100644 --- a/lib/mtio.c +++ b/lib/mtio.c @@ -31,12 +31,7 @@ #include #include #include - -struct stdiofd { - int fd; - int sock; - int timeout; -}; +#include static ssize_t mtread(void *cookie, void *buf, size_t len) { @@ -98,7 +93,7 @@ static int mtclose(void *cookie) return(0); } -FILE *mtstdopen(int fd, int issock, int timeout, char *mode) +FILE *mtstdopen(int fd, int issock, int timeout, char *mode, struct stdiofd **infop) { struct stdiofd *d; FILE *ret; @@ -117,11 +112,41 @@ FILE *mtstdopen(int fd, int issock, int timeout, char *mode) d->fd = fd; d->sock = issock; d->timeout = timeout; - ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose); - if(!ret) + if(!(ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose))) { free(d); - else - fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + return(NULL); + } + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + if(infop) + *infop = d; + return(ret); +} + +struct bufio *mtbioopen(int fd, int issock, int timeout, char *mode, struct stdiofd **infop) +{ + static struct bufioops ops = { + .read = mtread, .write = mtwrite, .close = mtclose, + }; + struct stdiofd *d; + struct bufio *ret; + + if(!strcmp(mode, "r")) { + } else if(!strcmp(mode, "w")) { + } else if(!strcmp(mode, "r+")) { + } else { + return(NULL); + } + omalloc(d); + d->fd = fd; + d->sock = issock; + d->timeout = timeout; + if(!(ret = bioopen(d, &ops))) { + free(d); + return(NULL); + } + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + if(infop) + *infop = d; return(ret); }