{
struct stdiofd *d = cookie;
int ev;
- size_t off;
ssize_t ret;
- off = 0;
- while(off < len) {
+ while(1) {
if(d->sock)
- ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
+ ret = send(d->fd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL);
else
- ret = write(d->fd, buf + off, len - off);
- if(ret < 0) {
- if(errno == EAGAIN) {
- ev = block(d->fd, EV_WRITE, d->timeout);
- if(ev < 0) {
- /* If we just go on, we should get the real error. */
- continue;
- } else if(ev == 0) {
- errno = ETIMEDOUT;
- return(off);
- } else {
- continue;
- }
- } else {
- return(off);
+ ret = write(d->fd, buf, len);
+ if((ret < 0) && (errno == EAGAIN)) {
+ ev = block(d->fd, EV_WRITE, d->timeout);
+ if(ev < 0) {
+ /* If we just go on, we should get the real error. */
+ continue;
+ } else if(ev == 0) {
+ errno = ETIMEDOUT;
+ return(-1);
}
} else {
- off += ret;
+ return(ret);
}
}
- return(off);
}
static int mtclose(void *cookie)
static ssize_t pipewrite(void *pdata, const void *buf, size_t len)
{
struct pipe *p = pdata;
- size_t off, part;
+ ssize_t ret;
if(p->closed & 1) {
errno = EPIPE;
return(-1);
}
- off = 0;
- while(off < len) {
- while(p->data.d >= p->bufmax) {
- if(p->w) {
- errno = EBUSY;
- return(-1);
- }
- if(p->closed & 1) {
- if(off == 0) {
- errno = EPIPE;
- return(-1);
- }
- return(off);
- }
- p->w = current;
- yield();
- p->w = NULL;
+ while(p->data.d >= p->bufmax) {
+ if(p->w) {
+ errno = EBUSY;
+ return(-1);
+ }
+ p->w = current;
+ yield();
+ p->w = NULL;
+ if(p->closed & 1) {
+ errno = EPIPE;
+ return(-1);
}
- part = min(len - off, p->bufmax - p->data.d);
- sizebuf(p->data, p->data.d + part);
- memcpy(p->data.b + p->data.d, buf + off, part);
- off += part;
- p->data.d += part;
- if(p->r)
- resume(p->r, 0);
}
- return(off);
+ ret = min(len, p->bufmax - p->data.d);
+ sizebuf(p->data, p->data.d + ret);
+ memcpy(p->data.b + p->data.d, buf, ret);
+ p->data.d += ret;
+ if(p->r)
+ resume(p->r, 0);
+ return(ret);
}
static int pipewclose(void *pdata)
static ssize_t wrapwrite(void *pdata, const char *buf, size_t len)
{
struct stdif *nf = pdata;
+ size_t off;
+ ssize_t ret;
- return(nf->write(nf->pdata, buf, len));
+ /*
+ * XXX? In seeming violation of its own manual, glibc requires the
+ * cookie-write function to complete writing the entire buffer,
+ * rather than working like write(2).
+ */
+ off = 0;
+ while(off < len) {
+ ret = nf->write(nf->pdata, buf + off, len - off);
+ if(ret < 0)
+ return(off);
+ off += ret;
+ }
+ return(off);
}
static int wrapseek(void *pdata, off_t *pos, int whence)