lib: Transfer the responsibility of fopencookie bugs to the generic implementation.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 3 Mar 2014 00:32:15 +0000 (01:32 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 3 Mar 2014 00:32:15 +0000 (01:32 +0100)
lib/mtio.c
lib/utils.c

index c084473..b2f55f8 100644 (file)
@@ -67,35 +67,26 @@ static ssize_t mtwrite(void *cookie, const void *buf, size_t len)
 {
     struct stdiofd *d = cookie;
     int ev;
 {
     struct stdiofd *d = cookie;
     int ev;
-    size_t off;
     ssize_t ret;
     
     ssize_t ret;
     
-    off = 0;
-    while(off < len) {
+    while(1) {
        if(d->sock)
        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
        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 {
            }
        } else {
-           off += ret;
+           return(ret);
        }
     }
        }
     }
-    return(off);
 }
 
 static int mtclose(void *cookie)
 }
 
 static int mtclose(void *cookie)
@@ -188,39 +179,32 @@ static int piperclose(void *pdata)
 static ssize_t pipewrite(void *pdata, const void *buf, size_t len)
 {
     struct pipe *p = pdata;
 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);
     }
     
     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 int pipewclose(void *pdata)
index e2c85a9..ad28086 100644 (file)
@@ -495,8 +495,22 @@ static ssize_t wrapread(void *pdata, char *buf, size_t len)
 static ssize_t wrapwrite(void *pdata, const char *buf, size_t len)
 {
     struct stdif *nf = 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)
 }
 
 static int wrapseek(void *pdata, off_t *pos, int whence)