mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-28 13:30:29 +00:00
Improve getservbyname and getservbyport (#207)
- support aliases in /etc/services - use case insensitive comparisons - add tests
This commit is contained in:
parent
58fb2fb3d3
commit
c0bec24fa2
7 changed files with 202 additions and 58 deletions
|
@ -30,30 +30,25 @@
|
|||
|
||||
struct servent *getservbyport(int port, const char *proto) {
|
||||
static struct servent *ptr1, se1;
|
||||
char name[DNS_NAME_MAX];
|
||||
static char s_name[DNS_NAME_MAX + 1];
|
||||
char *localproto = proto;
|
||||
|
||||
if (!ptr1) {
|
||||
se1.s_name = NULL;
|
||||
se1.s_aliases = (char **)malloc(sizeof(char *) * 1);
|
||||
if (!se1.s_aliases) return NULL;
|
||||
se1.s_aliases[0] = NULL;
|
||||
|
||||
se1.s_name = s_name;
|
||||
if (!(se1.s_aliases = calloc(1, sizeof(char *)))) return NULL;
|
||||
se1.s_port = 0;
|
||||
se1.s_proto = NULL;
|
||||
ptr1 = &se1;
|
||||
}
|
||||
|
||||
if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) {
|
||||
if (LookupServicesByPort(port, &localproto, ptr1->s_name, DNS_NAME_MAX,
|
||||
NULL) == -1) {
|
||||
// localproto got alloc'd during the lookup?
|
||||
if (!proto && localproto != proto) free(localproto);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ptr1->s_port = port;
|
||||
if (ptr1->s_name) free(ptr1->s_name);
|
||||
ptr1->s_name = strdup(name);
|
||||
|
||||
if (ptr1->s_proto) free(ptr1->s_proto);
|
||||
ptr1->s_proto = strdup(localproto);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue