Finita introduzione ai socket, con wrapper per read e write aggiunti.
[gapil.git] / sources / SockRead.c
diff --git a/sources/SockRead.c b/sources/SockRead.c
new file mode 100644 (file)
index 0000000..749d767
--- /dev/null
@@ -0,0 +1,24 @@
+#include <unistd.h>
+
+ssize_t SockRead(int fd, void *buf, size_t count) 
+{
+    size_t nleft;
+    ssize_t nread;
+    nleft = count;
+    while (nleft > 0) {             /* repeat until no left */
+       if ( (nread = read(fd, buf, nleft)) < 0) {
+           if (errno == EINTR) {   /* if interrupted by system call */
+               continue;           /* repeat the loop */
+           } else {
+               return(nread);      /* otherwise exit */
+           }
+       } else if (nread == 0) {    /* EOF */
+           break;                  /* break loop here */ 
+       }
+       nleft -= nread;             /* set left to read */
+       buf +=nread;                /* set pointer */
+    }
+    return (count - nleft);
+}
+