summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-25 20:51:20 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-25 20:54:16 +0100
commit70431dcb67e33bd466d03fb41e5a90ba301127a4 (patch)
treed3d2d5373c5da4f27f431d3504b55c623e9b95e0
parentef28fcb0f59d11bdf9f574a999045bc24ca40e18 (diff)
Make sendall* utils
-rw-r--r--main.c41
-rw-r--r--util.c43
-rw-r--r--util.h4
3 files changed, 49 insertions, 39 deletions
diff --git a/main.c b/main.c
index 0747724..60b9584 100644
--- a/main.c
+++ b/main.c
@@ -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){
diff --git a/util.c b/util.c
index e8c92cc..9d9c5d3 100644
--- a/util.c
+++ b/util.c
@@ -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;
+}
diff --git a/util.h b/util.h
index 1c99bea..5831b95 100644
--- a/util.h
+++ b/util.h
@@ -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)));