|
|
1.1 root 1: /* getservent.c 4.5 83/01/02 */
2:
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <sys/socket.h>
6: #include <netdb.h>
7: #include <ctype.h>
8:
9: #define MAXALIASES 35
10:
11: static char SERVDB[] = "/etc/services";
12: static FILE *servf = NULL;
13: static char line[BUFSIZ+1];
14: static struct servent serv;
15: static char *serv_aliases[MAXALIASES];
16: static int stayopen = 0;
17: static char *any();
18:
19: setservent(f)
20: int f;
21: {
22: if (servf == NULL)
23: servf = fopen(SERVDB, "r" );
24: else
25: rewind(servf);
26: stayopen |= f;
27: }
28:
29: endservent()
30: {
31: if (servf && !stayopen) {
32: fclose(servf);
33: servf = NULL;
34: }
35: }
36:
37: struct servent *
38: getservent()
39: {
40: char *p;
41: register char *cp, **q;
42:
43: if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
44: return (NULL);
45: again:
46: if ((p = fgets(line, BUFSIZ, servf)) == NULL)
47: return (NULL);
48: if (*p == '#')
49: goto again;
50: cp = any(p, "#\n");
51: if (cp == NULL)
52: goto again;
53: *cp = '\0';
54: serv.s_name = p;
55: p = any(p, " \t");
56: if (p == NULL)
57: goto again;
58: *p++ = '\0';
59: while (*p == ' ' || *p == '\t')
60: p++;
61: cp = any(p, ",/");
62: if (cp == NULL)
63: goto again;
64: *cp++ = '\0';
65: serv.s_port = htons((u_short)atoi(p));
66: serv.s_proto = cp;
67: q = serv.s_aliases = serv_aliases;
68: cp = any(cp, " \t");
69: if (cp != NULL)
70: *cp++ = '\0';
71: while (cp && *cp) {
72: if (*cp == ' ' || *cp == '\t') {
73: cp++;
74: continue;
75: }
76: if (q < &serv_aliases[MAXALIASES - 1])
77: *q++ = cp;
78: cp = any(cp, " \t");
79: if (cp != NULL)
80: *cp++ = '\0';
81: }
82: *q = NULL;
83: return (&serv);
84: }
85:
86: static char *
87: any(cp, match)
88: register char *cp;
89: char *match;
90: {
91: register char *mp, c;
92:
93: while (c = *cp) {
94: for (mp = match; *mp; mp++)
95: if (*mp == c)
96: return (cp);
97: cp++;
98: }
99: return ((char *)0);
100: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.