121383Sdist /*
2*61150Sbostic * Copyright (c) 1983, 1993
3*61150Sbostic * The Regents of the University of California. All rights reserved.
433679Sbostic *
542626Sbostic * %sccs.include.redist.c%
621383Sdist */
77901Ssam
826624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61150Sbostic static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 06/04/93";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121383Sdist
129808Ssam #include <sys/types.h>
137901Ssam #include <sys/socket.h>
147901Ssam #include <netdb.h>
1546604Sbostic #include <stdio.h>
1646604Sbostic #include <string.h>
1746604Sbostic #include <stdlib.h>
187901Ssam
197901Ssam #define MAXALIASES 35
207901Ssam
217901Ssam static FILE *servf = NULL;
227901Ssam static char line[BUFSIZ+1];
237901Ssam static struct servent serv;
247901Ssam static char *serv_aliases[MAXALIASES];
2528280Slepreau int _serv_stayopen;
267901Ssam
2746604Sbostic void
setservent(f)287901Ssam setservent(f)
297901Ssam int f;
307901Ssam {
317901Ssam if (servf == NULL)
3242266Sbostic servf = fopen(_PATH_SERVICES, "r" );
337901Ssam else
347901Ssam rewind(servf);
3528280Slepreau _serv_stayopen |= f;
367901Ssam }
377901Ssam
3846604Sbostic void
endservent()397901Ssam endservent()
407901Ssam {
4128280Slepreau if (servf) {
427901Ssam fclose(servf);
437901Ssam servf = NULL;
447901Ssam }
4528280Slepreau _serv_stayopen = 0;
467901Ssam }
477901Ssam
487901Ssam struct servent *
getservent()497901Ssam getservent()
507901Ssam {
517901Ssam char *p;
527901Ssam register char *cp, **q;
537901Ssam
5442266Sbostic if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
557901Ssam return (NULL);
567901Ssam again:
577901Ssam if ((p = fgets(line, BUFSIZ, servf)) == NULL)
587901Ssam return (NULL);
597901Ssam if (*p == '#')
607901Ssam goto again;
6146604Sbostic cp = strpbrk(p, "#\n");
627901Ssam if (cp == NULL)
637901Ssam goto again;
647901Ssam *cp = '\0';
657901Ssam serv.s_name = p;
6646604Sbostic p = strpbrk(p, " \t");
677901Ssam if (p == NULL)
687901Ssam goto again;
697901Ssam *p++ = '\0';
707901Ssam while (*p == ' ' || *p == '\t')
717901Ssam p++;
7246604Sbostic cp = strpbrk(p, ",/");
737901Ssam if (cp == NULL)
747901Ssam goto again;
757901Ssam *cp++ = '\0';
769808Ssam serv.s_port = htons((u_short)atoi(p));
777901Ssam serv.s_proto = cp;
789190Ssam q = serv.s_aliases = serv_aliases;
7946604Sbostic cp = strpbrk(cp, " \t");
8010087Ssam if (cp != NULL)
817901Ssam *cp++ = '\0';
8210087Ssam while (cp && *cp) {
8310087Ssam if (*cp == ' ' || *cp == '\t') {
8410087Ssam cp++;
8510087Ssam continue;
867901Ssam }
8710087Ssam if (q < &serv_aliases[MAXALIASES - 1])
8810087Ssam *q++ = cp;
8946604Sbostic cp = strpbrk(cp, " \t");
9010087Ssam if (cp != NULL)
9110087Ssam *cp++ = '\0';
927901Ssam }
937901Ssam *q = NULL;
947901Ssam return (&serv);
957901Ssam }
96