From 9b72a7e795d74e4dceec516d592609de12b69e85 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 18 May 2014 08:57:51 +0200 Subject: reduce code complexity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/util.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src/libmdsserver/util.c') diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 3b6cde1..41c6ff1 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -126,6 +126,7 @@ size_t send_message(int socket, const char* message, size_t length) size_t sent = 0; ssize_t just_sent; + errno = 0; while (length > 0) if ((just_sent = send(socket, message + sent, min(block_size, length), MSG_NOSIGNAL)) < 0) { @@ -222,8 +223,7 @@ char* full_read(int fd) if (state_buf_size == state_buf_ptr) { char* old_buf = state_buf; - state_buf = realloc(state_buf, (state_buf_size <<= 1) * sizeof(char)); - if (state_buf == NULL) + if (xrealloc(state_buf, state_buf_size <<= 1, char)) { free(old_buf); return NULL; @@ -245,3 +245,27 @@ char* full_read(int fd) return state_buf; } + +/** + * Check whether a string begins with a specific string, + * where neither of the strings are necessarily NUL-terminated + * + * @param haystack The string that should start with the other string + * @param needle The string the first string should start with + * @param haystack_n The length of `haystack` + * @param needle_n The length of `needle` + * @return Whether the `haystack` begins with `needle` + */ +int startswith_n(const char* haystack, const char* needle, size_t haystack_n, size_t needle_n) +{ + size_t i; + if (haystack_n < needle_n) + return 0; + + for (i = 0; i < needle_n; i++) + if (haystack[i] != needle[i]) + return 0; + + return 1; +} + -- cgit v1.2.3-70-g09d2