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[] = "@(#)getservent.c 5.3.1 (2.11BSD GTE) 6/27/94";
9: #endif LIBC_SCCS and not lint
10:
11: #include <stdio.h>
12: #include <sys/types.h>
13: #include <sys/socket.h>
14: #include <netinet/in.h>
15: #include <netdb.h>
16: #include <ctype.h>
17:
18: #define MAXALIASES 16
19:
20: static char SERVDB[] = "/etc/services";
21: static FILE *servf = NULL;
22: static char line[160+1];
23: static struct servent serv;
24: static char *serv_aliases[MAXALIASES];
25: static char *any();
26: int _serv_stayopen;
27:
28: setservent(f)
29: int f;
30: {
31: if (servf == NULL)
32: servf = fopen(SERVDB, "r" );
33: else
34: rewind(servf);
35: _serv_stayopen |= f;
36: }
37:
38: endservent()
39: {
40: if (servf) {
41: fclose(servf);
42: servf = NULL;
43: }
44: _serv_stayopen = 0;
45: }
46:
47: struct servent *
48: getservent()
49: {
50: char *p;
51: register char *cp, **q;
52:
53: if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
54: return (NULL);
55: again:
56: if ((p = fgets(line, sizeof(line)-1, servf)) == NULL)
57: return (NULL);
58: if (*p == '#')
59: goto again;
60: cp = any(p, "#\n");
61: if (cp == NULL)
62: goto again;
63: *cp = '\0';
64: serv.s_name = p;
65: p = any(p, " \t");
66: if (p == NULL)
67: goto again;
68: *p++ = '\0';
69: while (*p == ' ' || *p == '\t')
70: p++;
71: cp = any(p, ",/");
72: if (cp == NULL)
73: goto again;
74: *cp++ = '\0';
75: serv.s_port = htons((u_short)atoi(p));
76: serv.s_proto = cp;
77: q = serv.s_aliases = serv_aliases;
78: cp = any(cp, " \t");
79: if (cp != NULL)
80: *cp++ = '\0';
81: while (cp && *cp) {
82: if (*cp == ' ' || *cp == '\t') {
83: cp++;
84: continue;
85: }
86: if (q < &serv_aliases[MAXALIASES - 1])
87: *q++ = cp;
88: cp = any(cp, " \t");
89: if (cp != NULL)
90: *cp++ = '\0';
91: }
92: *q = NULL;
93: return (&serv);
94: }
95:
96: static char *
97: any(cp, match)
98: register char *cp;
99: char *match;
100: {
101: register char *mp, c;
102:
103: while (c = *cp) {
104: for (mp = match; *mp; mp++)
105: if (*mp == c)
106: return (cp);
107: cp++;
108: }
109: return ((char *)0);
110: }
Defined functions
any
defined in line
96; used 6 times
Defined variables
line
defined in line
22; used 2 times
sccsid
defined in line
8;
never used
serv
defined in line
23; used 5 times
Defined macros