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