xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 61150)
121379Sdist /*
2*61150Sbostic  * Copyright (c) 1983, 1993
3*61150Sbostic  *	The Regents of the University of California.  All rights reserved.
433679Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
621379Sdist  */
77899Ssam 
826620Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61150Sbostic static char sccsid[] = "@(#)getprotoent.c	8.1 (Berkeley) 06/04/93";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121379Sdist 
1212691Ssam #include <sys/types.h>
137899Ssam #include <sys/socket.h>
147899Ssam #include <netdb.h>
1546604Sbostic #include <stdio.h>
1646604Sbostic #include <stdlib.h>
1746604Sbostic #include <string.h>
187899Ssam 
197899Ssam #define	MAXALIASES	35
207899Ssam 
217899Ssam static FILE *protof = NULL;
227899Ssam static char line[BUFSIZ+1];
237899Ssam static struct protoent proto;
247899Ssam static char *proto_aliases[MAXALIASES];
2528280Slepreau int _proto_stayopen;
267899Ssam 
2746604Sbostic void
setprotoent(f)287899Ssam setprotoent(f)
297899Ssam 	int f;
307899Ssam {
317899Ssam 	if (protof == NULL)
3242266Sbostic 		protof = fopen(_PATH_PROTOCOLS, "r" );
337899Ssam 	else
347899Ssam 		rewind(protof);
3528280Slepreau 	_proto_stayopen |= f;
367899Ssam }
377899Ssam 
3846604Sbostic void
endprotoent()397899Ssam endprotoent()
407899Ssam {
4128280Slepreau 	if (protof) {
427899Ssam 		fclose(protof);
437899Ssam 		protof = NULL;
447899Ssam 	}
4528280Slepreau 	_proto_stayopen = 0;
467899Ssam }
477899Ssam 
487899Ssam struct protoent *
getprotoent()497899Ssam getprotoent()
507899Ssam {
517899Ssam 	char *p;
527899Ssam 	register char *cp, **q;
537899Ssam 
5442266Sbostic 	if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
557899Ssam 		return (NULL);
567899Ssam again:
577899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
587899Ssam 		return (NULL);
597899Ssam 	if (*p == '#')
607899Ssam 		goto again;
6146604Sbostic 	cp = strpbrk(p, "#\n");
627899Ssam 	if (cp == NULL)
637899Ssam 		goto again;
647899Ssam 	*cp = '\0';
657899Ssam 	proto.p_name = p;
6646604Sbostic 	cp = strpbrk(p, " \t");
677899Ssam 	if (cp == NULL)
687899Ssam 		goto again;
697899Ssam 	*cp++ = '\0';
707899Ssam 	while (*cp == ' ' || *cp == '\t')
717899Ssam 		cp++;
7246604Sbostic 	p = strpbrk(cp, " \t");
737899Ssam 	if (p != NULL)
747899Ssam 		*p++ = '\0';
757899Ssam 	proto.p_proto = atoi(cp);
769190Ssam 	q = proto.p_aliases = proto_aliases;
779190Ssam 	if (p != NULL) {
789190Ssam 		cp = p;
7910087Ssam 		while (cp && *cp) {
809190Ssam 			if (*cp == ' ' || *cp == '\t') {
819190Ssam 				cp++;
829190Ssam 				continue;
839190Ssam 			}
849190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
859190Ssam 				*q++ = cp;
8646604Sbostic 			cp = strpbrk(cp, " \t");
8710087Ssam 			if (cp != NULL)
889190Ssam 				*cp++ = '\0';
897899Ssam 		}
907899Ssam 	}
917899Ssam 	*q = NULL;
927899Ssam 	return (&proto);
937899Ssam }
94