diff options
author | ume <ume@FreeBSD.org> | 2010-04-04 07:31:10 +0000 |
---|---|---|
committer | ume <ume@FreeBSD.org> | 2010-04-04 07:31:10 +0000 |
commit | 6e468628cf2ea2fc37557d96be5e76436f206aed (patch) | |
tree | e479eb7d0c2e0130f47e9ea6218f5e096a5f9b2d /lib/libc/net/getservent.c | |
parent | ad7b5cc594ec25c321fa7e493162a1e3e4985d7b (diff) | |
download | FreeBSD-src-6e468628cf2ea2fc37557d96be5e76436f206aed.zip FreeBSD-src-6e468628cf2ea2fc37557d96be5e76436f206aed.tar.gz |
Reduce duplicate code.
MFC after: 2 weeks
Diffstat (limited to 'lib/libc/net/getservent.c')
-rw-r--r-- | lib/libc/net/getservent.c | 92 |
1 files changed, 33 insertions, 59 deletions
diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c index bdbe755..3dcad3f 100644 --- a/lib/libc/net/getservent.c +++ b/lib/libc/net/getservent.c @@ -207,6 +207,32 @@ servent_unpack(char *p, struct servent *serv, char **aliases, return 0; } +static int +parse_result(struct servent *serv, char *buffer, size_t bufsize, + char *resultbuf, size_t resultbuflen, int *errnop) +{ + char **aliases; + int aliases_size; + + if (bufsize <= resultbuflen + _ALIGNBYTES + sizeof(char *)) { + *errnop = ERANGE; + return (NS_RETURN); + } + aliases = (char **)_ALIGN(&buffer[resultbuflen + 1]); + aliases_size = (buffer + bufsize - (char *)aliases) / sizeof(char *); + if (aliases_size < 1) { + *errnop = ERANGE; + return (NS_RETURN); + } + + memcpy(buffer, resultbuf, resultbuflen); + buffer[resultbuflen] = '\0'; + + if (servent_unpack(buffer, serv, aliases, aliases_size, errnop) != 0) + return ((*errnop == 0) ? NS_NOTFOUND : NS_RETURN); + return (NS_SUCCESS); +} + /* files backend implementation */ static void files_endstate(void *p) @@ -258,8 +284,6 @@ files_servent(void *retval, void *mdata, va_list ap) size_t bufsize; int *errnop; - char **aliases; - int aliases_size; size_t linesize; char *line; char **cp; @@ -347,35 +371,12 @@ files_servent(void *retval, void *mdata, va_list ap) continue; } - if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - aliases = (char **)_ALIGN(&buffer[linesize + 1]); - aliases_size = (buffer + bufsize - - (char *)aliases) / sizeof(char *); - if (aliases_size < 1) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - - memcpy(buffer, line, linesize); - buffer[linesize] = '\0'; - - rv = servent_unpack(buffer, serv, aliases, aliases_size, + rv = parse_result(serv, buffer, bufsize, line, linesize, errnop); - if (rv !=0 ) { - if (*errnop == 0) { - rv = NS_NOTFOUND; - continue; - } - else { - rv = NS_RETURN; - break; - } - } + if (rv == NS_NOTFOUND) + continue; + if (rv == NS_RETURN) + break; rv = NS_NOTFOUND; switch (serv_mdata->how) { @@ -483,9 +484,6 @@ nis_servent(void *retval, void *mdata, va_list ap) size_t bufsize; int *errnop; - char **aliases; - int aliases_size; - name = NULL; proto = NULL; how = (enum nss_lookup_type)mdata; @@ -591,32 +589,8 @@ nis_servent(void *retval, void *mdata, va_list ap) break; }; - if (bufsize <= resultbuflen + _ALIGNBYTES + sizeof(char *)) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - - aliases = (char **)_ALIGN(&buffer[resultbuflen + 1]); - aliases_size = - (buffer + bufsize - (char *)aliases) / sizeof(char *); - if (aliases_size < 1) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - - memcpy(buffer, resultbuf, resultbuflen); - buffer[resultbuflen] = '\0'; - - if (servent_unpack(buffer, serv, aliases, aliases_size, - errnop) != 0) { - if (*errnop == 0) - rv = NS_NOTFOUND; - else - rv = NS_RETURN; - } else - rv = NS_SUCCESS; + rv = parse_result(serv, buffer, bufsize, resultbuf, + resultbuflen, errnop); free(resultbuf); } while (!(rv & NS_TERMINATE) && how == nss_lt_all); |