From 2061b653da64bcb90c217144686f74ee734fb90c Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 15 Jul 2011 22:36:48 +0100 Subject: Move parts over to util.c --- getgbook.c | 33 +-------------------------------- util.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 util.c diff --git a/getgbook.c b/getgbook.c index c17abf6..a98ef79 100644 --- a/getgbook.c +++ b/getgbook.c @@ -5,38 +5,7 @@ #include #include #include -#include -#include -#include -#include - -void die(char *msg) { - fputs(msg, stderr); - exit(EXIT_FAILURE); -} - -int dial(char *host, char *port) { - static struct addrinfo hints; - int srv; - struct addrinfo *res, *r; - - 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"); - for(r = res; r; r = r->ai_next) { - if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) - continue; - if(connect(srv, r->ai_addr, r->ai_addrlen) == 0) - break; - close(srv); - } - freeaddrinfo(res); - if(!r) - die("error: cannot connect to host\n"); - return srv; -} +#include "util.c" char * get(FILE *srv, char *host, char *path) { size_t l, res; diff --git a/util.c b/util.c new file mode 100644 index 0000000..d9afa59 --- /dev/null +++ b/util.c @@ -0,0 +1,34 @@ +/* See LICENSE file for license details. */ +#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; + int srv; + struct addrinfo *res, *r; + + 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"); + for(r = res; r; r = r->ai_next) { + if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) + continue; + if(connect(srv, r->ai_addr, r->ai_addrlen) == 0) + break; + close(srv); + } + freeaddrinfo(res); + if(!r) + die("error: cannot connect to host\n"); + return srv; +} -- cgit v1.2.3