|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)getprotoent.c 5.7 (Berkeley) 6/1/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <stdio.h>
25: #include <sys/types.h>
26: #include <sys/socket.h>
27: #include <netdb.h>
28: #include <ctype.h>
29:
30: #define MAXALIASES 35
31:
32: static FILE *protof = NULL;
33: static char line[BUFSIZ+1];
34: static struct protoent proto;
35: static char *proto_aliases[MAXALIASES];
36: static char *any();
37: int _proto_stayopen;
38:
39: setprotoent(f)
40: int f;
41: {
42: if (protof == NULL)
43: protof = fopen(_PATH_PROTOCOLS, "r" );
44: else
45: rewind(protof);
46: _proto_stayopen |= f;
47: }
48:
49: endprotoent()
50: {
51: if (protof) {
52: fclose(protof);
53: protof = NULL;
54: }
55: _proto_stayopen = 0;
56: }
57:
58: struct protoent *
59: getprotoent()
60: {
61: char *p;
62: register char *cp, **q;
63:
64: if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
65: return (NULL);
66: again:
67: if ((p = fgets(line, BUFSIZ, protof)) == NULL)
68: return (NULL);
69: if (*p == '#')
70: goto again;
71: cp = any(p, "#\n");
72: if (cp == NULL)
73: goto again;
74: *cp = '\0';
75: proto.p_name = p;
76: cp = any(p, " \t");
77: if (cp == NULL)
78: goto again;
79: *cp++ = '\0';
80: while (*cp == ' ' || *cp == '\t')
81: cp++;
82: p = any(cp, " \t");
83: if (p != NULL)
84: *p++ = '\0';
85: proto.p_proto = atoi(cp);
86: q = proto.p_aliases = proto_aliases;
87: if (p != NULL) {
88: cp = p;
89: while (cp && *cp) {
90: if (*cp == ' ' || *cp == '\t') {
91: cp++;
92: continue;
93: }
94: if (q < &proto_aliases[MAXALIASES - 1])
95: *q++ = cp;
96: cp = any(cp, " \t");
97: if (cp != NULL)
98: *cp++ = '\0';
99: }
100: }
101: *q = NULL;
102: return (&proto);
103: }
104:
105: static char *
106: any(cp, match)
107: register char *cp;
108: char *match;
109: {
110: register char *mp, c;
111:
112: while (c = *cp) {
113: for (mp = match; *mp; mp++)
114: if (*mp == c)
115: return (cp);
116: cp++;
117: }
118: return ((char *)0);
119: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.