diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-12-25 20:51:20 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-12-25 20:54:16 +0100 |
commit | 70431dcb67e33bd466d03fb41e5a90ba301127a4 (patch) | |
tree | d3d2d5373c5da4f27f431d3504b55c623e9b95e0 | |
parent | ef28fcb0f59d11bdf9f574a999045bc24ca40e18 (diff) |
Make sendall* utils
-rw-r--r-- | main.c | 41 | ||||
-rw-r--r-- | util.c | 43 | ||||
-rw-r--r-- | util.h | 4 |
3 files changed, 49 insertions, 39 deletions
@@ -1,16 +1,16 @@ -#define _GNU_SOURCE #include <stdio.h> #include <stdbool.h> -#include <stdarg.h> #include <string.h> #include <signal.h> #include <sys/socket.h> +#include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <errno.h> #include "http.h" #include "memory.h" +#include "util.h" #define PORT 8080 @@ -37,43 +37,6 @@ static void signal_handler(int sig){ } -// Returns -1 on error, 0 on success -static 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))) -static 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; -} - static void connection_handler(int sock){ Headers *headers=http_get_headers(sock); if(headers==NULL){ @@ -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; +} @@ -5,3 +5,7 @@ char* copy_buf(char *buf,int len); char* copy_str(char *str); void str_toupper(char *str); + +// Returns -1 on error, 0 on success +int sendall(int sock,const char *buf,ssize_t len); +int sendallf(int sock,const char *format,...) __attribute__((format (printf, 2, 3))); |