xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 21379)
1*21379Sdist /*
2*21379Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21379Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21379Sdist  * specifies the terms and conditions for redistribution.
5*21379Sdist  */
67899Ssam 
7*21379Sdist #ifndef lint
8*21379Sdist static char sccsid[] = "@(#)getprotoent.c	5.1 (Berkeley) 05/30/85";
9*21379Sdist #endif not lint
10*21379Sdist 
117899Ssam #include <stdio.h>
1212691Ssam #include <sys/types.h>
137899Ssam #include <sys/socket.h>
147899Ssam #include <netdb.h>
157899Ssam #include <ctype.h>
167899Ssam 
177899Ssam #define	MAXALIASES	35
187899Ssam 
199808Ssam static char PROTODB[] = "/etc/protocols";
207899Ssam static FILE *protof = NULL;
217899Ssam static char line[BUFSIZ+1];
227899Ssam static struct protoent proto;
237899Ssam static char *proto_aliases[MAXALIASES];
247899Ssam static int stayopen = 0;
257899Ssam static char *any();
267899Ssam 
277899Ssam setprotoent(f)
287899Ssam 	int f;
297899Ssam {
307899Ssam 	if (protof == NULL)
317899Ssam 		protof = fopen(PROTODB, "r" );
327899Ssam 	else
337899Ssam 		rewind(protof);
347899Ssam 	stayopen |= f;
357899Ssam }
367899Ssam 
377899Ssam endprotoent()
387899Ssam {
397899Ssam 	if (protof && !stayopen) {
407899Ssam 		fclose(protof);
417899Ssam 		protof = NULL;
427899Ssam 	}
437899Ssam }
447899Ssam 
457899Ssam struct protoent *
467899Ssam getprotoent()
477899Ssam {
487899Ssam 	char *p;
497899Ssam 	register char *cp, **q;
507899Ssam 
517899Ssam 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
527899Ssam 		return (NULL);
537899Ssam again:
547899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
557899Ssam 		return (NULL);
567899Ssam 	if (*p == '#')
577899Ssam 		goto again;
587899Ssam 	cp = any(p, "#\n");
597899Ssam 	if (cp == NULL)
607899Ssam 		goto again;
617899Ssam 	*cp = '\0';
627899Ssam 	proto.p_name = p;
637899Ssam 	cp = any(p, " \t");
647899Ssam 	if (cp == NULL)
657899Ssam 		goto again;
667899Ssam 	*cp++ = '\0';
677899Ssam 	while (*cp == ' ' || *cp == '\t')
687899Ssam 		cp++;
697899Ssam 	p = any(cp, " \t");
707899Ssam 	if (p != NULL)
717899Ssam 		*p++ = '\0';
727899Ssam 	proto.p_proto = atoi(cp);
739190Ssam 	q = proto.p_aliases = proto_aliases;
749190Ssam 	if (p != NULL) {
759190Ssam 		cp = p;
7610087Ssam 		while (cp && *cp) {
779190Ssam 			if (*cp == ' ' || *cp == '\t') {
789190Ssam 				cp++;
799190Ssam 				continue;
809190Ssam 			}
819190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
829190Ssam 				*q++ = cp;
839190Ssam 			cp = any(cp, " \t");
8410087Ssam 			if (cp != NULL)
859190Ssam 				*cp++ = '\0';
867899Ssam 		}
877899Ssam 	}
887899Ssam 	*q = NULL;
897899Ssam 	return (&proto);
907899Ssam }
917899Ssam 
927899Ssam static char *
937899Ssam any(cp, match)
947899Ssam 	register char *cp;
957899Ssam 	char *match;
967899Ssam {
977899Ssam 	register char *mp, c;
987899Ssam 
997899Ssam 	while (c = *cp) {
1007899Ssam 		for (mp = match; *mp; mp++)
1017899Ssam 			if (*mp == c)
1027899Ssam 				return (cp);
1037899Ssam 		cp++;
1047899Ssam 	}
1057899Ssam 	return ((char *)0);
1067899Ssam }
107