]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/utils.c
Add complete_read(), an EINTR-proof and partial-read-proof read,
[ppp.git] / pppd / utils.c
index 8c7045298b7bf0546022fc25a8a705549df12659..fb2c6aa20b05af1cfb9be9c27b3d8f3520c87a55 100644 (file)
@@ -33,7 +33,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: utils.c,v 1.20 2002/12/04 23:03:33 paulus Exp $"
+#define RCSID  "$Id: utils.c,v 1.21 2003/03/30 08:26:56 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -832,6 +832,32 @@ dump_packet(const char *tag, unsigned char *p, int len)
     dbglog("%s %P", tag, p, len);
 }
 
+/*
+ * complete_read - read a full `count' bytes from fd,
+ * unless end-of-file or an error other than EINTR is encountered.
+ */
+ssize_t
+complete_read(int fd, void *buf, size_t count)
+{
+       size_t done;
+       ssize_t nb;
+       char *ptr = buf;
+
+       for (done = 0; done < count; ) {
+               nb = read(fd, ptr, count - done);
+               if (nb < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -1;
+               }
+               if (nb == 0)
+                       break;
+               done += nb;
+               ptr += nb;
+       }
+       return done;
+}
+
 /* Procedures for locking the serial device using a lock file. */
 #ifndef LOCK_DIR
 #ifdef __linux__