From c0a2cdadcbdb1d5331efb149b9ef473e2bbdcbec Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 10 Apr 2004 20:35:24 +0000 Subject: [PATCH] Got tired by repeating the same `gather pieces from incomplete reads' subroutine. --- lib/Makefile | 2 +- lib/carefulio.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/lib.h | 5 +++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/carefulio.c diff --git a/lib/Makefile b/lib/Makefile index 8524a45c..50abafc6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 index 00000000..7bfeee9b --- /dev/null +++ b/lib/carefulio.c @@ -0,0 +1,51 @@ +/* + * Sherlock Library -- Careful Read/Write + * + * (c) 2004 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" + +#include + +/* + * 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; +} diff --git a/lib/lib.h b/lib/lib.h index 58692942..c06d8f5f 100644 --- 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 -- 2.39.2