use a common definition of NULL as 0L for C and C++
[musl] / src / network / getservbyname_r.c
index 5c02515..811c174 100644 (file)
@@ -12,21 +12,33 @@ int getservbyname_r(const char *name, const char *prots,
        struct addrinfo *ai, hint = { .ai_family = AF_INET };
        int i;
 
+       if (!prots) {
+               int r = getservbyname_r(name, "tcp", se, buf, buflen, res);
+               if (r) r = getservbyname_r(name, "udp", se, buf, buflen, res);
+               return r;
+       }
+
        /* Align buffer */
        i = (uintptr_t)buf & sizeof(char *)-1;
        if (!i) i = sizeof(char *);
-       if (buflen < 3*sizeof(char *)-i) {
-               errno = ERANGE;
-               return -1;
-       }
+       if (buflen < 3*sizeof(char *)-i)
+               return ERANGE;
        buf += sizeof(char *)-i;
        buflen -= sizeof(char *)-i;
 
        if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP;
        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_aliases = (void *)buf;