]> mj.ucw.cz Git - libucw.git/commitdiff
Got tired by repeating the same `gather pieces from incomplete reads' subroutine.
authorMartin Mares <mj@ucw.cz>
Sat, 10 Apr 2004 20:35:24 +0000 (20:35 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 10 Apr 2004 20:35:24 +0000 (20:35 +0000)
lib/Makefile
lib/carefulio.c [new file with mode: 0644]
lib/lib.h

index 8524a45c447e57accecd876e77f4ec21992fe24a..50abafc6a9847bcacb7b3f1352e9ded8f9c7381b 100644 (file)
@@ -10,7 +10,7 @@ LIBSH_MODS=alloc alloc_str ctmatch db fastbuf fb-file fb-mem lists \
        wordsplit str_ctype str_upper str_lower bucket conf object sorter \
        finger proctitle ipaccess profile bitsig randomkey \
        hashfunc base64 base224 fb-temp fb-mmap fb-printf urlkey \
-       partmap fb-limfd fb-buffer mainloop exitstatus runcmd
+       partmap fb-limfd fb-buffer mainloop exitstatus runcmd carefulio
 
 ifdef CONFIG_OWN_REGEX
 include lib/regex/Makefile
diff --git a/lib/carefulio.c b/lib/carefulio.c
new file mode 100644 (file)
index 0000000..7bfeee9
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *     Sherlock Library -- Careful Read/Write
+ *
+ *     (c) 2004 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "lib/lib.h"
+
+#include <unistd.h>
+
+/*
+ *  Reads and writes on sockets and pipes can return partial results,
+ *  so we implement an iterated read/write call.
+ */
+
+int
+careful_read(int fd, void *buf, int len)
+{
+  byte *pos = buf;
+  while (len)
+    {
+      int l = read(fd, pos, len);
+      if (l < 0)
+       return -1;
+      if (!l)
+       return 0;
+      pos += l;
+      len -= l;
+    }
+  return 1;
+}
+
+int
+careful_write(int fd, void *buf, int len)
+{
+  byte *pos = buf;
+  while (len)
+    {
+      int l = write(fd, pos, len);
+      if (l <= 0)
+       return -1;
+      if (!l)
+       return 0;
+      pos += l;
+      len -= l;
+    }
+  return 1;
+}
index 58692942789ce3b007a056dfcba8486cd338e911..c06d8f5feabc127caf3268da1e13eb337959e8aa 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -192,4 +192,9 @@ int run_command_v(byte *cmd, va_list args);
 void NONRET exec_command_v(byte *cmd, va_list args);
 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
 
+/* carefulio.c */
+
+int careful_read(int fd, void *buf, int len);
+int careful_write(int fd, void *buf, int len);
+
 #endif