From 42489d4b990a8793c837811c34a869ec16d055ba Mon Sep 17 00:00:00 2001 From: millert <> Date: Mon, 25 Oct 2004 03:09:01 +0000 Subject: Change return value of reentrant getproto* and getserv* to match the IBM/Digital API. --- src/lib/libc/net/getproto.c | 16 +++++++++------- src/lib/libc/net/getprotoent.3 | 28 +++++++++++++++++++++------- src/lib/libc/net/getprotoent.c | 20 +++++++++++--------- src/lib/libc/net/getprotoname.c | 18 ++++++++++-------- src/lib/libc/net/getservbyname.c | 20 +++++++++++--------- src/lib/libc/net/getservbyport.c | 18 ++++++++++-------- src/lib/libc/net/getservent.3 | 30 ++++++++++++++++++++++-------- src/lib/libc/net/getservent.c | 20 +++++++++++--------- 8 files changed, 105 insertions(+), 65 deletions(-) diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c index 3f05a93cce..16d840394c 100644 --- a/src/lib/libc/net/getproto.c +++ b/src/lib/libc/net/getproto.c @@ -28,26 +28,26 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getproto.c,v 1.5 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getproto.c,v 1.6 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include #include -struct protoent * +int getprotobynumber_r(int num, struct protoent *pe, struct protoent_data *pd) { - struct protoent *p; + int error; setprotoent_r(pd->stayopen, pd); - while ((p = getprotoent_r(pe, pd))) - if (p->p_proto == num) + while ((error = getprotoent_r(pe, pd)) == 0) + if (pe->p_proto == num) break; if (!pd->stayopen && pd->fp != NULL) { (void)fclose(pd->fp); pd->fp = NULL; } - return (p); + return (error); } struct protoent * @@ -56,5 +56,7 @@ getprotobynumber(int num) extern struct protoent_data _protoent_data; static struct protoent proto; - return getprotobynumber_r(num, &proto, &_protoent_data); + if (getprotobynumber_r(num, &proto, &_protoent_data) != 0) + return (NULL); + return (&proto); } diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 index 2497f9f7ca..5ebd03cc45 100644 --- a/src/lib/libc/net/getprotoent.3 +++ b/src/lib/libc/net/getprotoent.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: getprotoent.3,v 1.11 2004/10/17 20:24:23 millert Exp $ +.\" $OpenBSD: getprotoent.3,v 1.12 2004/10/25 03:09:01 millert Exp $ .\" .\" Copyright (c) 1983, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -46,15 +46,15 @@ .Fd #include .Ft struct protoent * .Fn getprotoent "void" -.Ft struct protoent * +.Ft int .Fn getprotoent_r "struct protoent *protoent" "struct protoent_data *protoent_data" .Ft struct protoent * .Fn getprotobyname "const char *name" -.Ft struct protoent * +.Ft int .Fn getprotobyname_r "const char *name" "struct protoent *protoent" "struct protoent_data *protoent_data" .Ft struct protoent * .Fn getprotobynumber "int proto" -.Ft struct protoent * +.Ft int .Fn getprotobynumber_r "int proto" "struct protoent *protoent" "struct protoent_data *protoent_data" .Ft void .Fn setprotoent "int stayopen" @@ -144,9 +144,23 @@ also take a pointer to a .Fa protoent structure which is used to store the results of the database lookup. .Sh RETURN VALUES -Null pointer (0) returned on -.Dv EOF -or error. +The +.Fn getprotoent , +.Fn getprotobyport , +and +.Fn getprotobyname +functions return a pointer to a +.Fa servent +structure on success or a null pointer if end-of-file +is reached or an error occurs. +.Pp +The +.Fn getprotoent_r , +.Fn getprotobyport_r , +and +.Fn getprotobyname_r +functions return 0 on success or \-1 if end-of-file +is reached or an error occurs. .Sh FILES .Bl -tag -width /etc/protocols -compact .It Pa /etc/protocols diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c index f3327db9bd..7e93d233ef 100644 --- a/src/lib/libc/net/getprotoent.c +++ b/src/lib/libc/net/getprotoent.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.6 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.7 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -65,7 +65,7 @@ endprotoent_r(struct protoent_data *pd) pd->stayopen = 0; } -struct protoent * +int getprotoent_r(struct protoent *pe, struct protoent_data *pd) { char *p, *cp, **q, *endp; @@ -74,10 +74,10 @@ getprotoent_r(struct protoent *pe, struct protoent_data *pd) int serrno; if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "r" )) == NULL) - return (NULL); + return (-1); again: if ((p = fgetln(pd->fp, &len)) == NULL) - return (NULL); + return (-1); if (len == 0 || *p == '#' || *p == '\n') goto again; if (p[len-1] == '\n') @@ -86,7 +86,7 @@ again: len = cp - p; cp = realloc(pd->line, len + 1); if (cp == NULL) - return (NULL); + return (-1); pd->line = pe->p_name = memcpy(cp, p, len); cp[len] = '\0'; cp = strpbrk(cp, " \t"); @@ -109,7 +109,7 @@ again: serrno = errno; endprotoent_r(pd); errno = serrno; - return (NULL); + return (-1); } } q = pe->p_aliases = pd->aliases; @@ -127,7 +127,7 @@ again: serrno = errno; endprotoent_r(pd); errno = serrno; - return (NULL); + return (-1); } pd->maxaliases *= 2; q = (char **)p + (q - pe->p_aliases); @@ -140,7 +140,7 @@ again: } } *q = NULL; - return (pe); + return (0); } struct protoent_data _protoent_data; /* shared with getproto{,name}.c */ @@ -162,5 +162,7 @@ getprotoent(void) { static struct protoent proto; - return getprotoent_r(&proto, &_protoent_data); + if (getprotoent_r(&proto, &_protoent_data) != 0) + return (NULL); + return (&proto); } diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c index 429304e7a1..f257bf4a71 100644 --- a/src/lib/libc/net/getprotoname.c +++ b/src/lib/libc/net/getprotoname.c @@ -28,25 +28,25 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getprotoname.c,v 1.5 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getprotoname.c,v 1.6 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include #include #include -struct protoent * +int getprotobyname_r(const char *name, struct protoent *pe, struct protoent_data *pd) { - struct protoent *p; char **cp; + int error; setprotoent_r(pd->stayopen, pd); - while ((p = getprotoent_r(pe, pd))) { - if (strcmp(p->p_name, name) == 0) + while ((error = getprotoent_r(pe, pd)) == 0) { + if (strcmp(pe->p_name, name) == 0) break; - for (cp = p->p_aliases; *cp != 0; cp++) + for (cp = pe->p_aliases; *cp != 0; cp++) if (strcmp(*cp, name) == 0) goto found; } @@ -55,7 +55,7 @@ found: fclose(pd->fp); pd->fp = NULL; } - return (p); + return (error); } struct protoent * @@ -64,5 +64,7 @@ getprotobyname(const char *name) extern struct protoent_data _protoent_data; static struct protoent proto; - return getprotobyname_r(name, &proto, &_protoent_data); + if (getprotobyname_r(name, &proto, &_protoent_data) != 0) + return (NULL); + return (&proto); } diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c index 597c42497f..062318630f 100644 --- a/src/lib/libc/net/getservbyname.c +++ b/src/lib/libc/net/getservbyname.c @@ -28,37 +28,37 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.8 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.9 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include #include #include -struct servent * +int getservbyname_r(const char *name, const char *proto, struct servent *se, struct servent_data *sd) { - struct servent *p; char **cp; + int error; setservent_r(sd->stayopen, sd); - while ((p = getservent_r(se, sd))) { - if (strcmp(name, p->s_name) == 0) + while ((error = getservent_r(se, sd)) == 0) { + if (strcmp(name, se->s_name) == 0) goto gotname; - for (cp = p->s_aliases; *cp; cp++) + for (cp = se->s_aliases; *cp; cp++) if (strcmp(name, *cp) == 0) goto gotname; continue; gotname: - if (proto == 0 || strcmp(p->s_proto, proto) == 0) + if (proto == 0 || strcmp(se->s_proto, proto) == 0) break; } if (!sd->stayopen && sd->fp != NULL) { fclose(sd->fp); sd->fp = NULL; } - return (p); + return (error); } struct servent * @@ -67,5 +67,7 @@ getservbyname(const char *name, const char *proto) extern struct servent_data _servent_data; static struct servent serv; - return (getservbyname_r(name, proto, &serv, &_servent_data)); + if (getservbyname_r(name, proto, &serv, &_servent_data) != 0) + return (NULL); + return (&serv); } diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c index e8b8efc504..5e210b2e8b 100644 --- a/src/lib/libc/net/getservbyport.c +++ b/src/lib/libc/net/getservbyport.c @@ -28,31 +28,31 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getservbyport.c,v 1.5 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getservbyport.c,v 1.6 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include #include #include -struct servent * +int getservbyport_r(int port, const char *proto, struct servent *se, struct servent_data *sd) { - struct servent *p; + int error; setservent_r(sd->stayopen, sd); - while ((p = getservent_r(se, sd))) { - if (p->s_port != port) + while ((error = getservent_r(se, sd)) == 0) { + if (se->s_port != port) continue; - if (proto == 0 || strcmp(p->s_proto, proto) == 0) + if (proto == 0 || strcmp(se->s_proto, proto) == 0) break; } if (!sd->stayopen && sd->fp != NULL) { fclose(sd->fp); sd->fp = NULL; } - return (p); + return (error); } struct servent * @@ -61,5 +61,7 @@ getservbyport(int port, const char *proto) extern struct servent_data _servent_data; static struct servent serv; - return (getservbyport_r(port, proto, &serv, &_servent_data)); + if (getservbyport_r(port, proto, &serv, &_servent_data) != 0) + return (NULL); + return (&serv); } diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 index f5294b8f70..0e93e195b6 100644 --- a/src/lib/libc/net/getservent.3 +++ b/src/lib/libc/net/getservent.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: getservent.3,v 1.14 2004/10/17 20:24:23 millert Exp $ +.\" $OpenBSD: getservent.3,v 1.15 2004/10/25 03:09:01 millert Exp $ .\" .\" Copyright (c) 1983, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -46,15 +46,15 @@ .Fd #include .Ft struct servent * .Fn getservent "void" -.Ft struct servent * +.Ft int .Fn getservent_r "struct servent *servent" "struct servent_data *servent_data" .Ft struct servent * .Fn getservbyname "const char *name" "const char *proto" -.Ft struct servent * +.Ft int .Fn getservbyname_r "const char *name" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" .Ft struct servent * .Fn getservbyport "int port" "const char *proto" -.Ft struct servent * +.Ft int .Fn getservbyport_r "int port" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" .Ft void .Fn setservent "int stayopen" @@ -153,10 +153,24 @@ structure which is used to store the results of the database lookup. .Bl -tag -width /etc/services -compact .It Pa /etc/services .El -.Sh DIAGNOSTICS -Null pointer (0) returned on -.Dv EOF -or error. +.Sh RETURN VALUES +The +.Fn getservent , +.Fn getservbyport , +and +.Fn getservbyname +functions return a pointer to a +.Fa servent +structure on success or a null pointer if end-of-file +is reached or an error occurs. +.Pp +The +.Fn getservent_r , +.Fn getservbyport_r , +and +.Fn getservbyname_r +functions return 0 on success or \-1 if end-of-file +is reached or an error occurs. .Sh SEE ALSO .Xr getprotoent 3 , .Xr services 5 diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c index 9cf53774d6..3eaf2a4adc 100644 --- a/src/lib/libc/net/getservent.c +++ b/src/lib/libc/net/getservent.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getservent.c,v 1.8 2004/10/17 20:24:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: getservent.c,v 1.9 2004/10/25 03:09:01 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -65,7 +65,7 @@ endservent_r(struct servent_data *sd) sd->stayopen = 0; } -struct servent * +int getservent_r(struct servent *se, struct servent_data *sd) { char *p, *cp, **q, *endp; @@ -74,10 +74,10 @@ getservent_r(struct servent *se, struct servent_data *sd) int serrno; if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL) - return (NULL); + return (-1); again: if ((p = fgetln(sd->fp, &len)) == NULL) - return (NULL); + return (-1); if (len == 0 || *p == '#' || *p == '\n') goto again; if (p[len-1] == '\n') @@ -86,7 +86,7 @@ again: len = cp - p; cp = realloc(sd->line, len + 1); if (cp == NULL) - return (NULL); + return (-1); sd->line = se->s_name = memcpy(cp, p, len); cp[len] = '\0'; p = strpbrk(cp, " \t"); @@ -111,7 +111,7 @@ again: serrno = errno; endservent_r(sd); errno = serrno; - return (NULL); + return (-1); } } q = se->s_aliases = sd->aliases; @@ -130,7 +130,7 @@ again: serrno = errno; endservent_r(sd); errno = serrno; - return (NULL); + return (-1); } sd->maxaliases *= 2; q = (char **)p + (q - se->s_aliases); @@ -142,7 +142,7 @@ again: *cp++ = '\0'; } *q = NULL; - return (se); + return (0); } struct servent_data _servent_data; /* shared with getservby{name,port}.c */ @@ -164,5 +164,7 @@ getservent(void) { static struct servent serv; - return getservent_r(&serv, &_servent_data); + if (getservent_r(&serv, &_servent_data) != 0) + return (NULL); + return (&serv); } -- cgit v1.2.3-55-g6feb