xref: /csrg-svn/lib/libc/net/getprotoent.c (revision 8324)
1 /*	getprotoent.c	4.2	82/10/05	*/
2 
3 #include <stdio.h>
4 #include <sys/socket.h>
5 #include <netdb.h>
6 #include <ctype.h>
7 
8 #define	MAXALIASES	35
9 
10 static char *PROTODB = "/usr/lib/protocols";
11 static FILE *protof = NULL;
12 static char line[BUFSIZ+1];
13 static struct protoent proto;
14 static char *proto_aliases[MAXALIASES];
15 static int stayopen = 0;
16 static char *any();
17 
18 setprotoent(f)
19 	int f;
20 {
21 	if (protof == NULL)
22 		protof = fopen(PROTODB, "r" );
23 	else
24 		rewind(protof);
25 	stayopen |= f;
26 }
27 
28 endprotoent()
29 {
30 	if (protof && !stayopen) {
31 		fclose(protof);
32 		protof = NULL;
33 	}
34 }
35 
36 struct protoent *
37 getprotoent()
38 {
39 	char *p;
40 	register char *cp, **q;
41 
42 	if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL)
43 		return (NULL);
44 again:
45 	if ((p = fgets(line, BUFSIZ, protof)) == NULL)
46 		return (NULL);
47 	if (*p == '#')
48 		goto again;
49 	cp = any(p, "#\n");
50 	if (cp == NULL)
51 		goto again;
52 	*cp = '\0';
53 	proto.p_name = p;
54 	cp = any(p, " \t");
55 	if (cp == NULL)
56 		goto again;
57 	*cp++ = '\0';
58 	while (*cp == ' ' || *cp == '\t')
59 		cp++;
60 	p = any(cp, " \t");
61 	if (p != NULL)
62 		*p++ = '\0';
63 	proto.p_proto = atoi(cp);
64 	proto.p_aliases = proto_aliases;
65 	cp = p;
66 	q = proto_aliases;
67 	while (*cp) {
68 		if (*cp == ' ' || *cp == '\t') {
69 			cp++;
70 			continue;
71 		}
72 		if (q < &proto_aliases[MAXALIASES - 1])
73 			*q++ = cp;
74 		cp = any(cp, " \t");
75 		if (*cp != NULL)
76 			*cp++ = '\0';
77 	}
78 	*q = NULL;
79 	return (&proto);
80 }
81 
82 static char *
83 any(cp, match)
84 	register char *cp;
85 	char *match;
86 {
87 	register char *mp, c;
88 
89 	while (c = *cp) {
90 		for (mp = match; *mp; mp++)
91 			if (*mp == c)
92 				return (cp);
93 		cp++;
94 	}
95 	return ((char *)0);
96 }
97