Code cleanup

This commit is contained in:
Pierre Pronchery 2014-04-25 19:13:10 +08:00
parent f7c9a0df76
commit 93e8e13de2

View File

@ -19,10 +19,11 @@
static struct addrinfo * _init_address(char const * name, int domain, int flags) static struct addrinfo * _init_address(char const * name, int domain, int flags)
{ {
struct addrinfo * ai = NULL; struct addrinfo * ai = NULL;
char * hostname;
char * servname;
char sep = ':'; char sep = ':';
char * p; char * p;
char * q; char * q;
char * r;
long l; long l;
struct addrinfo hints; struct addrinfo hints;
int res = 0; int res = 0;
@ -32,27 +33,27 @@ static struct addrinfo * _init_address(char const * name, int domain, int flags)
flags); flags);
#endif #endif
/* guess the port separator */ /* guess the port separator */
if((q = strchr(name, ':')) != NULL && strchr(++q, ':') != NULL) if((p = strchr(name, ':')) != NULL && strchr(++p, ':') != NULL)
sep = '.'; sep = '.';
/* obtain the name */ /* obtain the name */
if(strlen(name) == 0) if(strlen(name) == 0)
p = NULL; hostname = NULL;
else if((p = strdup(name)) == NULL) else if((hostname = strdup(name)) == NULL)
{ {
error_set_code(1, "%s", strerror(errno)); error_set_code(1, "%s", strerror(errno));
return NULL; return NULL;
} }
/* obtain the port number */ /* obtain the port number */
if(p == NULL || (q = strrchr(p, sep)) == NULL) if(hostname == NULL || (servname = strrchr(hostname, sep)) == NULL)
{ {
l = 0; l = 0;
q = NULL; servname = NULL;
} }
else else
{ {
*(q++) = '\0'; *(servname++) = '\0';
l = strtol(q, &r, 10); l = strtol(servname, &q, 10);
if(q[0] == '\0' || *r != '\0') if(servname[0] == '\0' || *q != '\0')
l = -error_set_code(1, "%s", strerror(EINVAL)); l = -error_set_code(1, "%s", strerror(EINVAL));
} }
/* FIXME perform this asynchronously */ /* FIXME perform this asynchronously */
@ -61,8 +62,8 @@ static struct addrinfo * _init_address(char const * name, int domain, int flags)
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = flags; hints.ai_flags = flags;
if(l >= 0) if(l >= 0)
res = getaddrinfo(p, q, &hints, &ai); res = getaddrinfo(hostname, servname, &hints, &ai);
free(p); free(hostname);
/* check for errors */ /* check for errors */
if(res != 0) if(res != 0)
{ {