X-Git-Url: https://git.armaanb.net/?p=sic.git;a=blobdiff_plain;f=util.c;h=8cea8836cf6dc14dbadb5394201b45cda9dc0d34;hp=26953d0a879157c8b9232bf04977296660b413e6;hb=HEAD;hpb=6af1b812ae862a64e0c92060b993b591a72d741e diff --git a/util.c b/util.c index 26953d0..8cea883 100644 --- a/util.c +++ b/util.c @@ -18,9 +18,9 @@ eprint(const char *fmt, ...) { 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; @@ -28,28 +28,21 @@ dial(char *host, char *port) { 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; -} - -#define strlcpy _strlcpy -static void -strlcpy(char *to, const char *from, int l) { - memccpy(to, from, '\0', l); - to[l-1] = '\0'; + return fd; } static char * eat(char *s, int (*p)(int), int r) { - while(s != '\0' && p(*s) == r) + while(*s != '\0' && p((unsigned char)*s) == r) s++; return s; } @@ -67,8 +60,7 @@ static void trim(char *s) { char *e; - e = s + strlen(s) - 1; - while(isspace(*e) && e > s) - e--; - *(e + 1) = '\0'; + for (e = s + strlen(s); e > s && isspace((unsigned char)*(e - 1)); e--) + ; + *e = '\0'; }