From 394f88f26dd4a79e33d2dd8397f05be1b240ffd4 Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 28 May 2006 15:15:04 +0000 Subject: [PATCH] Added `sread' and `swrite': `read' and `write' like functions that assure that all data is written or fail completely.. --- src/common.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/common.c b/src/common.c index 78be3afc..138b7678 100644 --- a/src/common.c +++ b/src/common.c @@ -125,6 +125,61 @@ void sfree (void **ptr) } #endif +ssize_t sread (int fd, void *buf, size_t count) +{ + char *ptr; + size_t nleft; + ssize_t status; + + ptr = (char *) buf; + nleft = count; + + while (nleft > 0) + { + status = read (fd, (void *) ptr, nleft); + + if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR))) + continue; + + if (status < 0) + return (status); + + assert (nleft >= status); + + nleft = nleft - status; + ptr = ptr + status; + } + + return (0); +} + + +ssize_t swrite (int fd, const void *buf, size_t count) +{ + const char *ptr; + size_t nleft; + ssize_t status; + + ptr = (const char *) buf; + nleft = buflen; + + while (nleft > 0) + { + status = write (fd, (const void *) ptr, nleft); + + if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR))) + continue; + + if (status < 0) + return (status); + + nleft = nleft - status; + ptr = ptr + status; + } + + return (0); +} + int strsplit (char *string, char **fields, size_t size) { size_t i; diff --git a/src/common.h b/src/common.h index 4dc34b07..f6a8e19a 100644 --- a/src/common.h +++ b/src/common.h @@ -37,6 +37,44 @@ void sstrncpy(char *d, const char *s, int len); char *sstrdup(const char *s); void *smalloc(size_t size); +/* + * NAME + * sread + * + * DESCRIPTION + * Reads exactly `n' bytes or failes. Syntax and other behavior is analogous + * to `read(2)'. + * + * PARAMETERS + * `fd' File descriptor to write to. + * `buf' Buffer that is to be written. + * `count' Numver of bytes in the buffer. + * + * RETURN VALUE + * Zero upon success or non-zero if an error occured. `errno' is set in this + * case. + */ +ssize_t sread (int fd, void *buf, size_t count); + +/* + * NAME + * swrite + * + * DESCRIPTION + * Writes exactly `n' bytes or failes. Syntax and other behavior is analogous + * to `write(2)'. + * + * PARAMETERS + * `fd' File descriptor to write to. + * `buf' Buffer that is to be written. + * `count' Numver of bytes in the buffer. + * + * RETURN VALUE + * Zero upon success or non-zero if an error occured. `errno' is set in this + * case. + */ +ssize_t swrite (int fd, const void *buf, size_t count); + /* * NAME * strsplit -- 2.30.2