xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 9190)
1*9190Ssam /*	getprotoent.c	4.3	82/11/14	*/
27899Ssam 
37899Ssam #include <stdio.h>
47899Ssam #include <sys/socket.h>
57899Ssam #include <netdb.h>
67899Ssam #include <ctype.h>
77899Ssam 
87899Ssam #define	MAXALIASES	35
97899Ssam 
107899Ssam static char *PROTODB = "/usr/lib/protocols";
117899Ssam static FILE *protof = NULL;
127899Ssam static char line[BUFSIZ+1];
137899Ssam static struct protoent proto;
147899Ssam static char *proto_aliases[MAXALIASES];
157899Ssam static int stayopen = 0;
167899Ssam static char *any();
177899Ssam 
187899Ssam setprotoent(f)
197899Ssam 	int f;
207899Ssam {
217899Ssam 	if (protof == NULL)
227899Ssam 		protof = fopen(PROTODB, "r" );
237899Ssam 	else
247899Ssam 		rewind(protof);
257899Ssam 	stayopen |= f;
267899Ssam }
277899Ssam 
287899Ssam endprotoent()
297899Ssam {
307899Ssam 	if (protof && !stayopen) {
317899Ssam 		fclose(protof);
327899Ssam 		protof = NULL;
337899Ssam 	}
347899Ssam }
357899Ssam 
367899Ssam struct protoent *
377899Ssam getprotoent()
387899Ssam {
397899Ssam 	char *p;
407899Ssam 	register char *cp, **q;
417899Ssam 
427899Ssam 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
437899Ssam 		return (NULL);
447899Ssam again:
457899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
467899Ssam 		return (NULL);
477899Ssam 	if (*p == '#')
487899Ssam 		goto again;
497899Ssam 	cp = any(p, "#\n");
507899Ssam 	if (cp == NULL)
517899Ssam 		goto again;
527899Ssam 	*cp = '\0';
537899Ssam 	proto.p_name = p;
547899Ssam 	cp = any(p, " \t");
557899Ssam 	if (cp == NULL)
567899Ssam 		goto again;
577899Ssam 	*cp++ = '\0';
587899Ssam 	while (*cp == ' ' || *cp == '\t')
597899Ssam 		cp++;
607899Ssam 	p = any(cp, " \t");
617899Ssam 	if (p != NULL)
627899Ssam 		*p++ = '\0';
637899Ssam 	proto.p_proto = atoi(cp);
64*9190Ssam 	q = proto.p_aliases = proto_aliases;
65*9190Ssam 	if (p != NULL) {
66*9190Ssam 		cp = p;
67*9190Ssam 		while (*cp) {
68*9190Ssam 			if (*cp == ' ' || *cp == '\t') {
69*9190Ssam 				cp++;
70*9190Ssam 				continue;
71*9190Ssam 			}
72*9190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
73*9190Ssam 				*q++ = cp;
74*9190Ssam 			cp = any(cp, " \t");
75*9190Ssam 			if (*cp != NULL)
76*9190Ssam 				*cp++ = '\0';
777899Ssam 		}
787899Ssam 	}
797899Ssam 	*q = NULL;
807899Ssam 	return (&proto);
817899Ssam }
827899Ssam 
837899Ssam static char *
847899Ssam any(cp, match)
857899Ssam 	register char *cp;
867899Ssam 	char *match;
877899Ssam {
887899Ssam 	register char *mp, c;
897899Ssam 
907899Ssam 	while (c = *cp) {
917899Ssam 		for (mp = match; *mp; mp++)
927899Ssam 			if (*mp == c)
937899Ssam 				return (cp);
947899Ssam 		cp++;
957899Ssam 	}
967899Ssam 	return ((char *)0);
977899Ssam }
98