xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 42266)
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
634816Sbostic  * provided that the above copyright notice and this paragraph are
734816Sbostic  * duplicated in all such forms and that any documentation,
834816Sbostic  * advertising materials, and other materials related to such
934816Sbostic  * distribution and use acknowledge that the software was developed
1034816Sbostic  * by the University of California, Berkeley.  The name of the
1134816Sbostic  * University may not be used to endorse or promote products derived
1234816Sbostic  * from this software without specific prior written permission.
1334816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621379Sdist  */
177899Ssam 
1826620Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*42266Sbostic static char sccsid[] = "@(#)getprotoent.c	5.6 (Berkeley) 05/21/90";
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 
307899Ssam static FILE *protof = NULL;
317899Ssam static char line[BUFSIZ+1];
327899Ssam static struct protoent proto;
337899Ssam static char *proto_aliases[MAXALIASES];
347899Ssam static char *any();
3528280Slepreau int _proto_stayopen;
367899Ssam 
377899Ssam setprotoent(f)
387899Ssam 	int f;
397899Ssam {
407899Ssam 	if (protof == NULL)
41*42266Sbostic 		protof = fopen(_PATH_PROTOCOLS, "r" );
427899Ssam 	else
437899Ssam 		rewind(protof);
4428280Slepreau 	_proto_stayopen |= f;
457899Ssam }
467899Ssam 
477899Ssam endprotoent()
487899Ssam {
4928280Slepreau 	if (protof) {
507899Ssam 		fclose(protof);
517899Ssam 		protof = NULL;
527899Ssam 	}
5328280Slepreau 	_proto_stayopen = 0;
547899Ssam }
557899Ssam 
567899Ssam struct protoent *
577899Ssam getprotoent()
587899Ssam {
597899Ssam 	char *p;
607899Ssam 	register char *cp, **q;
617899Ssam 
62*42266Sbostic 	if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
637899Ssam 		return (NULL);
647899Ssam again:
657899Ssam 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
667899Ssam 		return (NULL);
677899Ssam 	if (*p == '#')
687899Ssam 		goto again;
697899Ssam 	cp = any(p, "#\n");
707899Ssam 	if (cp == NULL)
717899Ssam 		goto again;
727899Ssam 	*cp = '\0';
737899Ssam 	proto.p_name = p;
747899Ssam 	cp = any(p, " \t");
757899Ssam 	if (cp == NULL)
767899Ssam 		goto again;
777899Ssam 	*cp++ = '\0';
787899Ssam 	while (*cp == ' ' || *cp == '\t')
797899Ssam 		cp++;
807899Ssam 	p = any(cp, " \t");
817899Ssam 	if (p != NULL)
827899Ssam 		*p++ = '\0';
837899Ssam 	proto.p_proto = atoi(cp);
849190Ssam 	q = proto.p_aliases = proto_aliases;
859190Ssam 	if (p != NULL) {
869190Ssam 		cp = p;
8710087Ssam 		while (cp && *cp) {
889190Ssam 			if (*cp == ' ' || *cp == '\t') {
899190Ssam 				cp++;
909190Ssam 				continue;
919190Ssam 			}
929190Ssam 			if (q < &proto_aliases[MAXALIASES - 1])
939190Ssam 				*q++ = cp;
949190Ssam 			cp = any(cp, " \t");
9510087Ssam 			if (cp != NULL)
969190Ssam 				*cp++ = '\0';
977899Ssam 		}
987899Ssam 	}
997899Ssam 	*q = NULL;
1007899Ssam 	return (&proto);
1017899Ssam }
1027899Ssam 
1037899Ssam static char *
1047899Ssam any(cp, match)
1057899Ssam 	register char *cp;
1067899Ssam 	char *match;
1077899Ssam {
1087899Ssam 	register char *mp, c;
1097899Ssam 
1107899Ssam 	while (c = *cp) {
1117899Ssam 		for (mp = match; *mp; mp++)
1127899Ssam 			if (*mp == c)
1137899Ssam 				return (cp);
1147899Ssam 		cp++;
1157899Ssam 	}
1167899Ssam 	return ((char *)0);
1177899Ssam }
118