xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 12691)
1*12691Ssam /*	getprotoent.c	4.6	83/05/23	*/
27899Ssam 
37899Ssam #include <stdio.h>
4*12691Ssam #include <sys/types.h>
57899Ssam #include <sys/socket.h>
67899Ssam #include <netdb.h>
77899Ssam #include <ctype.h>
87899Ssam 
97899Ssam #define	MAXALIASES	35
107899Ssam 
119808Ssam static char PROTODB[] = "/etc/protocols";
127899Ssam static FILE *protof = NULL;
137899Ssam static char line[BUFSIZ+1];
147899Ssam static struct protoent proto;
157899Ssam static char *proto_aliases[MAXALIASES];
167899Ssam static int stayopen = 0;
177899Ssam static char *any();
187899Ssam 
197899Ssam setprotoent(f)
207899Ssam 	int f;
217899Ssam {
227899Ssam 	if (protof == NULL)
237899Ssam 		protof = fopen(PROTODB, "r" );
247899Ssam 	else
257899Ssam 		rewind(protof);
267899Ssam 	stayopen |= f;
277899Ssam }
287899Ssam 
297899Ssam endprotoent()
307899Ssam {
317899Ssam 	if (protof && !stayopen) {
327899Ssam 		fclose(protof);
337899Ssam 		protof = NULL;
347899Ssam 	}
357899Ssam }
367899Ssam 
377899Ssam struct protoent *
387899Ssam getprotoent()
397899Ssam {
407899Ssam 	char *p;
417899Ssam 	register char *cp, **q;
427899Ssam 
437899Ssam 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
447899Ssam 		return (NULL);
457899Ssam again:
467899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
477899Ssam 		return (NULL);
487899Ssam 	if (*p == '#')
497899Ssam 		goto again;
507899Ssam 	cp = any(p, "#\n");
517899Ssam 	if (cp == NULL)
527899Ssam 		goto again;
537899Ssam 	*cp = '\0';
547899Ssam 	proto.p_name = p;
557899Ssam 	cp = any(p, " \t");
567899Ssam 	if (cp == NULL)
577899Ssam 		goto again;
587899Ssam 	*cp++ = '\0';
597899Ssam 	while (*cp == ' ' || *cp == '\t')
607899Ssam 		cp++;
617899Ssam 	p = any(cp, " \t");
627899Ssam 	if (p != NULL)
637899Ssam 		*p++ = '\0';
647899Ssam 	proto.p_proto = atoi(cp);
659190Ssam 	q = proto.p_aliases = proto_aliases;
669190Ssam 	if (p != NULL) {
679190Ssam 		cp = p;
6810087Ssam 		while (cp && *cp) {
699190Ssam 			if (*cp == ' ' || *cp == '\t') {
709190Ssam 				cp++;
719190Ssam 				continue;
729190Ssam 			}
739190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
749190Ssam 				*q++ = cp;
759190Ssam 			cp = any(cp, " \t");
7610087Ssam 			if (cp != NULL)
779190Ssam 				*cp++ = '\0';
787899Ssam 		}
797899Ssam 	}
807899Ssam 	*q = NULL;
817899Ssam 	return (&proto);
827899Ssam }
837899Ssam 
847899Ssam static char *
857899Ssam any(cp, match)
867899Ssam 	register char *cp;
877899Ssam 	char *match;
887899Ssam {
897899Ssam 	register char *mp, c;
907899Ssam 
917899Ssam 	while (c = *cp) {
927899Ssam 		for (mp = match; *mp; mp++)
937899Ssam 			if (*mp == c)
947899Ssam 				return (cp);
957899Ssam 		cp++;
967899Ssam 	}
977899Ssam 	return ((char *)0);
987899Ssam }
99