xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 42626)
121379Sdist /*
221379Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
5*42626Sbostic  * %sccs.include.redist.c%
621379Sdist  */
77899Ssam 
826620Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42626Sbostic static char sccsid[] = "@(#)getprotoent.c	5.7 (Berkeley) 06/01/90";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121379Sdist 
127899Ssam #include <stdio.h>
1312691Ssam #include <sys/types.h>
147899Ssam #include <sys/socket.h>
157899Ssam #include <netdb.h>
167899Ssam #include <ctype.h>
177899Ssam 
187899Ssam #define	MAXALIASES	35
197899Ssam 
207899Ssam static FILE *protof = NULL;
217899Ssam static char line[BUFSIZ+1];
227899Ssam static struct protoent proto;
237899Ssam static char *proto_aliases[MAXALIASES];
247899Ssam static char *any();
2528280Slepreau int _proto_stayopen;
267899Ssam 
277899Ssam setprotoent(f)
287899Ssam 	int f;
297899Ssam {
307899Ssam 	if (protof == NULL)
3142266Sbostic 		protof = fopen(_PATH_PROTOCOLS, "r" );
327899Ssam 	else
337899Ssam 		rewind(protof);
3428280Slepreau 	_proto_stayopen |= f;
357899Ssam }
367899Ssam 
377899Ssam endprotoent()
387899Ssam {
3928280Slepreau 	if (protof) {
407899Ssam 		fclose(protof);
417899Ssam 		protof = NULL;
427899Ssam 	}
4328280Slepreau 	_proto_stayopen = 0;
447899Ssam }
457899Ssam 
467899Ssam struct protoent *
477899Ssam getprotoent()
487899Ssam {
497899Ssam 	char *p;
507899Ssam 	register char *cp, **q;
517899Ssam 
5242266Sbostic 	if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
537899Ssam 		return (NULL);
547899Ssam again:
557899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
567899Ssam 		return (NULL);
577899Ssam 	if (*p == '#')
587899Ssam 		goto again;
597899Ssam 	cp = any(p, "#\n");
607899Ssam 	if (cp == NULL)
617899Ssam 		goto again;
627899Ssam 	*cp = '\0';
637899Ssam 	proto.p_name = p;
647899Ssam 	cp = any(p, " \t");
657899Ssam 	if (cp == NULL)
667899Ssam 		goto again;
677899Ssam 	*cp++ = '\0';
687899Ssam 	while (*cp == ' ' || *cp == '\t')
697899Ssam 		cp++;
707899Ssam 	p = any(cp, " \t");
717899Ssam 	if (p != NULL)
727899Ssam 		*p++ = '\0';
737899Ssam 	proto.p_proto = atoi(cp);
749190Ssam 	q = proto.p_aliases = proto_aliases;
759190Ssam 	if (p != NULL) {
769190Ssam 		cp = p;
7710087Ssam 		while (cp && *cp) {
789190Ssam 			if (*cp == ' ' || *cp == '\t') {
799190Ssam 				cp++;
809190Ssam 				continue;
819190Ssam 			}
829190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
839190Ssam 				*q++ = cp;
849190Ssam 			cp = any(cp, " \t");
8510087Ssam 			if (cp != NULL)
869190Ssam 				*cp++ = '\0';
877899Ssam 		}
887899Ssam 	}
897899Ssam 	*q = NULL;
907899Ssam 	return (&proto);
917899Ssam }
927899Ssam 
937899Ssam static char *
947899Ssam any(cp, match)
957899Ssam 	register char *cp;
967899Ssam 	char *match;
977899Ssam {
987899Ssam 	register char *mp, c;
997899Ssam 
1007899Ssam 	while (c = *cp) {
1017899Ssam 		for (mp = match; *mp; mp++)
1027899Ssam 			if (*mp == c)
1037899Ssam 				return (cp);
1047899Ssam 		cp++;
1057899Ssam 	}
1067899Ssam 	return ((char *)0);
1077899Ssam }
108