diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -1,6 +1,11 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdarg.h> #include <stdlib.h> #include <string.h> #include <ctype.h> +#include <sys/socket.h> +#include <errno.h> #include "memory.h" #include "util.h" @@ -22,3 +27,41 @@ void str_toupper(char *str){ str++; } } + + +// Returns -1 on error, 0 on success +int sendall(int sock,const char *buf,ssize_t len){ + if(len==-1){ + len=strlen(buf); + } + + ssize_t sent=0; + while(sent<len){ + ssize_t ret=send(sock,buf+sent,len-sent,0); + if(ret<0){ + if(errno==EINTR){ + continue; + } else { + return -1; + } + } + sent+=ret; + } + + return 0; +} + +__attribute__((format (printf, 2, 3))) +int sendallf(int sock,const char *format,...){ + va_list ap; + va_start(ap,format); + char *buf; + int len=vasprintf(&buf,format,ap); + va_end(ap); + if(len<0){ + return -1; + } + int ret=sendall(sock,buf,len); + free(buf); + return ret; +} |