xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 26620)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)getprotoent.c	5.2 (Berkeley) 03/09/86";
9 #endif LIBC_SCCS and not lint
10 
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netdb.h>
15 #include <ctype.h>
16 
17 #define	MAXALIASES	35
18 
19 static char PROTODB[] = "/etc/protocols";
20 static FILE *protof = NULL;
21 static char line[BUFSIZ+1];
22 static struct protoent proto;
23 static char *proto_aliases[MAXALIASES];
24 static int stayopen = 0;
25 static char *any();
26 
27 setprotoent(f)
28 	int f;
29 {
30 	if (protof == NULL)
31 		protof = fopen(PROTODB, "r" );
32 	else
33 		rewind(protof);
34 	stayopen |= f;
35 }
36 
37 endprotoent()
38 {
39 	if (protof && !stayopen) {
40 		fclose(protof);
41 		protof = NULL;
42 	}
43 }
44 
45 struct protoent *
46 getprotoent()
47 {
48 	char *p;
49 	register char *cp, **q;
50 
51 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
52 		return (NULL);
53 again:
54 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
55 		return (NULL);
56 	if (*p == '#')
57 		goto again;
58 	cp = any(p, "#\n");
59 	if (cp == NULL)
60 		goto again;
61 	*cp = '\0';
62 	proto.p_name = p;
63 	cp = any(p, " \t");
64 	if (cp == NULL)
65 		goto again;
66 	*cp++ = '\0';
67 	while (*cp == ' ' || *cp == '\t')
68 		cp++;
69 	p = any(cp, " \t");
70 	if (p != NULL)
71 		*p++ = '\0';
72 	proto.p_proto = atoi(cp);
73 	q = proto.p_aliases = proto_aliases;
74 	if (p != NULL) {
75 		cp = p;
76 		while (cp && *cp) {
77 			if (*cp == ' ' || *cp == '\t') {
78 				cp++;
79 				continue;
80 			}
81 			if (q < &proto_aliases[MAXALIASES - 1])
82 				*q++ = cp;
83 			cp = any(cp, " \t");
84 			if (cp != NULL)
85 				*cp++ = '\0';
86 		}
87 	}
88 	*q = NULL;
89 	return (&proto);
90 }
91 
92 static char *
93 any(cp, match)
94 	register char *cp;
95 	char *match;
96 {
97 	register char *mp, c;
98 
99 	while (c = *cp) {
100 		for (mp = match; *mp; mp++)
101 			if (*mp == c)
102 				return (cp);
103 		cp++;
104 	}
105 	return ((char *)0);
106 }
107