make getservby*_r return error code rather than -1 (and using errno)

untested but should be correct..
This commit is contained in:
Rich Felker 2012-07-22 18:49:49 -04:00
parent 68c5822367
commit 75fff40b9b
2 changed files with 35 additions and 19 deletions

View File

@ -12,25 +12,33 @@ int getservbyname_r(const char *name, const char *prots,
struct addrinfo *ai, hint = { .ai_family = AF_INET }; struct addrinfo *ai, hint = { .ai_family = AF_INET };
int i; int i;
if (!prots) return -( if (!prots) {
getservbyname_r(name, "tcp", se, buf, buflen, res) int r = getservbyname_r(name, "tcp", se, buf, buflen, res);
&& getservbyname_r(name, "udp", se, buf, buflen, res) ); if (r) r = getservbyname_r(name, "udp", se, buf, buflen, res);
return r;
}
/* Align buffer */ /* Align buffer */
i = (uintptr_t)buf & sizeof(char *)-1; i = (uintptr_t)buf & sizeof(char *)-1;
if (!i) i = sizeof(char *); if (!i) i = sizeof(char *);
if (buflen < 3*sizeof(char *)-i) { if (buflen < 3*sizeof(char *)-i)
errno = ERANGE; return ERANGE;
return -1;
}
buf += sizeof(char *)-i; buf += sizeof(char *)-i;
buflen -= sizeof(char *)-i; buflen -= sizeof(char *)-i;
if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP; if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP;
else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP; else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP;
else return -1; else return EINVAL;
if (getaddrinfo(0, name, &hint, &ai) < 0) return -1; switch (getaddrinfo(0, name, &hint, &ai)) {
case EAI_MEMORY:
case EAI_SYSTEM:
return ENOMEM;
default:
return ENOENT;
case 0:
break;
}
se->s_name = (char *)name; se->s_name = (char *)name;
se->s_aliases = (void *)buf; se->s_aliases = (void *)buf;

View File

@ -15,21 +15,21 @@ int getservbyport_r(int port, const char *prots,
.sin_port = port, .sin_port = port,
}; };
if (!prots) return -( if (!prots) {
getservbyport_r(port, "tcp", se, buf, buflen, res) int r = getservbyport_r(port, "tcp", se, buf, buflen, res);
&& getservbyport_r(port, "udp", se, buf, buflen, res) ); if (r) r = getservbyport_r(port, "udp", se, buf, buflen, res);
return r;
}
/* Align buffer */ /* Align buffer */
i = (uintptr_t)buf & sizeof(char *)-1; i = (uintptr_t)buf & sizeof(char *)-1;
if (!i) i = sizeof(char *); if (!i) i = sizeof(char *);
if (buflen < 3*sizeof(char *)-i) { if (buflen < 3*sizeof(char *)-i)
errno = ERANGE; return ERANGE;
return -1;
}
buf += sizeof(char *)-i; buf += sizeof(char *)-i;
buflen -= sizeof(char *)-i; buflen -= sizeof(char *)-i;
if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return -1; if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return EINVAL;
se->s_port = port; se->s_port = port;
se->s_proto = (char *)prots; se->s_proto = (char *)prots;
@ -39,8 +39,16 @@ int getservbyport_r(int port, const char *prots,
se->s_aliases[1] = 0; se->s_aliases[1] = 0;
se->s_aliases[0] = se->s_name = buf; se->s_aliases[0] = se->s_name = buf;
if (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen, switch (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen,
strcmp(prots, "udp") ? 0 : NI_DGRAM) < 0) return -1; strcmp(prots, "udp") ? 0 : NI_DGRAM)) {
case EAI_MEMORY:
case EAI_SYSTEM:
return ENOMEM;
default:
return ENOENT;
case 0:
break;
}
*res = se; *res = se;
return 0; return 0;