diff options
author | Nick White <hg@njw.me.uk> | 2011-07-21 22:52:14 +0100 |
---|---|---|
committer | Nick White <hg@njw.me.uk> | 2011-07-21 22:52:14 +0100 |
commit | 2792ba2ddfbf7ece7b960faf1ec28c022e9c566e (patch) | |
tree | 2cfc1f83c7930dd747bdd36e9fe190de8f80f54a /util.c | |
parent | 2c87bf5e7c229e7c7c85bb6bc0cae03e989ad388 (diff) |
Separate util.c, use page codes, remove die()
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -1,14 +1,12 @@ /* See COPYING file for copyright, license and warranty details. */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <unistd.h> #include <netdb.h> #include <netinet/in.h> #include <sys/socket.h> -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 \ |