static int
dial(char *host, char *port) {
- static struct addrinfo hints;
- int srv;
+ struct addrinfo hints;
struct addrinfo *res, *r;
+ int fd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
if(getaddrinfo(host, port, &hints, &res) != 0)
eprint("error: cannot resolve hostname '%s':", host);
for(r = res; r; r = r->ai_next) {
- if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
+ if((fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
continue;
- if(connect(srv, r->ai_addr, r->ai_addrlen) == 0)
+ if(connect(fd, r->ai_addr, r->ai_addrlen) == 0)
break;
- close(srv);
+ close(fd);
}
freeaddrinfo(res);
if(!r)
eprint("error: cannot connect to host '%s'\n", host);
- return srv;
+ return fd;
}
-#define strlcpy _strlcpy
-static void
-strlcpy(char *to, const char *from, int l) {
- memccpy(to, from, '\0', l);
- to[l-1] = '\0';
-}
-
-static void
-eat(char **s, int (*p)(int), int r) {
- char *q;
-
- for(q = *s; *q && p(*q) == r; q++)
- ;
- *s = q;
+static char *
+eat(char *s, int (*p)(int), int r) {
+ while(*s != '\0' && p((unsigned char)*s) == r)
+ s++;
+ return s;
}
static char*
-tok(char **s) {
- char *p;
-
- eat(s, isspace, 1);
- p = *s;
- eat(s, isspace, 0);
- if(**s)
- *(*s)++ = '\0';
- return p;
+skip(char *s, char c) {
+ while(*s != c && *s != '\0')
+ s++;
+ if(*s != '\0')
+ *s++ = '\0';
+ return s;
}
-static char*
-ctok(char **s, int c) {
- char *p, *q;
+static void
+trim(char *s) {
+ char *e;
- q = *s;
- for(p = q; *p && *p != c; p++)
+ for (e = s + strlen(s); e > s && isspace((unsigned char)*(e - 1)); e--)
;
- if(*p) *p++ = '\0';
- *s = p;
- return q;
+ *e = '\0';
}