From 3aabbb4ab12be066b20c5fe8e5fc25c1ff084564 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Mon, 17 Jun 2002 21:15:35 +0000 Subject: Make things more re-entrany, kill some cruft. -Erik --- libc/inet/getproto.c | 170 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 99 insertions(+), 71 deletions(-) (limited to 'libc/inet/getproto.c') diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c index e5c6d9068..0f145ead4 100644 --- a/libc/inet/getproto.c +++ b/libc/inet/getproto.c @@ -52,6 +52,7 @@ */ #define __FORCE_GLIBC +#define _GNU_SOURCE #include #include #include @@ -60,6 +61,18 @@ #include #include +#ifdef __UCLIBC_HAS_THREADS__ +#include +static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + + + #define MAXALIASES 35 static FILE *protof = NULL; @@ -70,97 +83,112 @@ static int proto_stayopen; void setprotoent(int f) { - if (protof == NULL) - protof = fopen(_PATH_PROTOCOLS, "r" ); - else - rewind(protof); - proto_stayopen |= f; + LOCK; + if (protof == NULL) + protof = fopen(_PATH_PROTOCOLS, "r" ); + else + rewind(protof); + proto_stayopen |= f; + UNLOCK; } void endprotoent(void) { - if (protof) { - fclose(protof); - protof = NULL; - } - proto_stayopen = 0; + LOCK; + if (protof) { + fclose(protof); + protof = NULL; + } + proto_stayopen = 0; + UNLOCK; } struct protoent * getprotoent(void) { - char *p; - register char *cp, **q; + char *p; + register char *cp, **q; - if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) - return (NULL); + LOCK; + if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) { + UNLOCK; + return (NULL); + } again: - if ((p = fgets(line, BUFSIZ, protof)) == NULL) - return (NULL); - if (*p == '#') - goto again; - cp = strpbrk(p, "#\n"); - if (cp == NULL) - goto again; - *cp = '\0'; - proto.p_name = p; - cp = strpbrk(p, " \t"); - if (cp == NULL) - goto again; - *cp++ = '\0'; - while (*cp == ' ' || *cp == '\t') + if ((p = fgets(line, BUFSIZ, protof)) == NULL) { + UNLOCK; + return (NULL); + } + + if (*p == '#') + goto again; + cp = strpbrk(p, "#\n"); + if (cp == NULL) + goto again; + *cp = '\0'; + proto.p_name = p; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + proto.p_proto = atoi(cp); + q = proto.p_aliases = proto_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { cp++; - p = strpbrk(cp, " \t"); - if (p != NULL) - *p++ = '\0'; - proto.p_proto = atoi(cp); - q = proto.p_aliases = proto_aliases; - if (p != NULL) { - cp = p; - while (cp && *cp) { - if (*cp == ' ' || *cp == '\t') { - cp++; - continue; - } - if (q < &proto_aliases[MAXALIASES - 1]) - *q++ = cp; - cp = strpbrk(cp, " \t"); - if (cp != NULL) - *cp++ = '\0'; - } + continue; + } + if (q < &proto_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; } - *q = NULL; - return (&proto); + } + *q = NULL; + UNLOCK; + return (&proto); } struct protoent * getprotobyname(const char *name) { - register struct protoent *p; - register char **cp; - - setprotoent(proto_stayopen); - while ((p = getprotoent()) != NULL) { - if (strcmp(p->p_name, name) == 0) - break; - for (cp = p->p_aliases; *cp != 0; cp++) - if (strcmp(*cp, name) == 0) - goto found; - } + register struct protoent *p; + register char **cp; + + LOCK; + setprotoent(proto_stayopen); + while ((p = getprotoent()) != NULL) { + if (strcmp(p->p_name, name) == 0) + break; + for (cp = p->p_aliases; *cp != 0; cp++) + if (strcmp(*cp, name) == 0) + goto found; + } found: - if (!proto_stayopen) - endprotoent(); - return (p); + if (!proto_stayopen) + endprotoent(); + UNLOCK; + return (p); } struct protoent * getprotobynumber(int proto) { - register struct protoent *p; - - setprotoent(proto_stayopen); - while ((p = getprotoent()) != NULL) - if (p->p_proto == proto) - break; - if (!proto_stayopen) - endprotoent(); - return (p); + register struct protoent *p; + + LOCK; + setprotoent(proto_stayopen); + while ((p = getprotoent()) != NULL) + if (p->p_proto == proto) + break; + if (!proto_stayopen) + endprotoent(); + UNLOCK; + return (p); } -- cgit v1.2.3