xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 34816)
121379Sdist /*
221379Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
533679Sbostic  * Redistribution and use in source and binary forms are permitted
6*34816Sbostic  * provided that the above copyright notice and this paragraph are
7*34816Sbostic  * duplicated in all such forms and that any documentation,
8*34816Sbostic  * advertising materials, and other materials related to such
9*34816Sbostic  * distribution and use acknowledge that the software was developed
10*34816Sbostic  * by the University of California, Berkeley.  The name of the
11*34816Sbostic  * University may not be used to endorse or promote products derived
12*34816Sbostic  * from this software without specific prior written permission.
13*34816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621379Sdist  */
177899Ssam 
1826620Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34816Sbostic static char sccsid[] = "@(#)getprotoent.c	5.5 (Berkeley) 06/27/88";
2033679Sbostic #endif /* LIBC_SCCS and not lint */
2121379Sdist 
227899Ssam #include <stdio.h>
2312691Ssam #include <sys/types.h>
247899Ssam #include <sys/socket.h>
257899Ssam #include <netdb.h>
267899Ssam #include <ctype.h>
277899Ssam 
287899Ssam #define	MAXALIASES	35
297899Ssam 
309808Ssam static char PROTODB[] = "/etc/protocols";
317899Ssam static FILE *protof = NULL;
327899Ssam static char line[BUFSIZ+1];
337899Ssam static struct protoent proto;
347899Ssam static char *proto_aliases[MAXALIASES];
357899Ssam static char *any();
3628280Slepreau int _proto_stayopen;
377899Ssam 
387899Ssam setprotoent(f)
397899Ssam 	int f;
407899Ssam {
417899Ssam 	if (protof == NULL)
427899Ssam 		protof = fopen(PROTODB, "r" );
437899Ssam 	else
447899Ssam 		rewind(protof);
4528280Slepreau 	_proto_stayopen |= f;
467899Ssam }
477899Ssam 
487899Ssam endprotoent()
497899Ssam {
5028280Slepreau 	if (protof) {
517899Ssam 		fclose(protof);
527899Ssam 		protof = NULL;
537899Ssam 	}
5428280Slepreau 	_proto_stayopen = 0;
557899Ssam }
567899Ssam 
577899Ssam struct protoent *
587899Ssam getprotoent()
597899Ssam {
607899Ssam 	char *p;
617899Ssam 	register char *cp, **q;
627899Ssam 
637899Ssam 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
647899Ssam 		return (NULL);
657899Ssam again:
667899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
677899Ssam 		return (NULL);
687899Ssam 	if (*p == '#')
697899Ssam 		goto again;
707899Ssam 	cp = any(p, "#\n");
717899Ssam 	if (cp == NULL)
727899Ssam 		goto again;
737899Ssam 	*cp = '\0';
747899Ssam 	proto.p_name = p;
757899Ssam 	cp = any(p, " \t");
767899Ssam 	if (cp == NULL)
777899Ssam 		goto again;
787899Ssam 	*cp++ = '\0';
797899Ssam 	while (*cp == ' ' || *cp == '\t')
807899Ssam 		cp++;
817899Ssam 	p = any(cp, " \t");
827899Ssam 	if (p != NULL)
837899Ssam 		*p++ = '\0';
847899Ssam 	proto.p_proto = atoi(cp);
859190Ssam 	q = proto.p_aliases = proto_aliases;
869190Ssam 	if (p != NULL) {
879190Ssam 		cp = p;
8810087Ssam 		while (cp && *cp) {
899190Ssam 			if (*cp == ' ' || *cp == '\t') {
909190Ssam 				cp++;
919190Ssam 				continue;
929190Ssam 			}
939190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
949190Ssam 				*q++ = cp;
959190Ssam 			cp = any(cp, " \t");
9610087Ssam 			if (cp != NULL)
979190Ssam 				*cp++ = '\0';
987899Ssam 		}
997899Ssam 	}
1007899Ssam 	*q = NULL;
1017899Ssam 	return (&proto);
1027899Ssam }
1037899Ssam 
1047899Ssam static char *
1057899Ssam any(cp, match)
1067899Ssam 	register char *cp;
1077899Ssam 	char *match;
1087899Ssam {
1097899Ssam 	register char *mp, c;
1107899Ssam 
1117899Ssam 	while (c = *cp) {
1127899Ssam 		for (mp = match; *mp; mp++)
1137899Ssam 			if (*mp == c)
1147899Ssam 				return (cp);
1157899Ssam 		cp++;
1167899Ssam 	}
1177899Ssam 	return ((char *)0);
1187899Ssam }
119