From 2792ba2ddfbf7ece7b960faf1ec28c022e9c566e Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 21 Jul 2011 22:52:14 +0100 Subject: Separate util.c, use page codes, remove die() --- util.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index a34dad3..ab6fdf8 100644 --- a/util.c +++ b/util.c @@ -1,14 +1,12 @@ /* See COPYING file for copyright, license and warranty details. */ +#include +#include +#include #include #include #include #include -void die(char *msg) { - fputs(msg, stderr); - exit(EXIT_FAILURE); -} - /* plundered from suckless' sic */ static int dial(char *host, char *port) { static struct addrinfo hints; @@ -18,8 +16,10 @@ static int dial(char *host, char *port) { memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if(getaddrinfo(host, port, &hints, &res) != 0) - die("error: cannot resolve hostname\n"); + if(getaddrinfo(host, port, &hints, &res) != 0) { + fprintf(stderr, "error: cannot resolve hostname %s\n", host); + return -1; + } for(r = res; r; r = r->ai_next) { if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) continue; @@ -28,8 +28,10 @@ static int dial(char *host, char *port) { close(srv); } freeaddrinfo(res); - if(!r) - die("error: cannot connect to host\n"); + if(!r) { + fprintf(stderr, "error: cannot connect to host %s\n", host); + return -1; + } return srv; } @@ -39,7 +41,7 @@ int get(char *host, char *path, char **buf) { char h[1024] = "\0"; FILE *srv; - fd = dial(host, "80"); + if((fd = dial(host, "80")) == -1) return 0; srv = fdopen(fd, "r+"); fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION \ -- cgit v1.2.3