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: # include <sccs.h>
9:
10: SCCSID(@(#)getservent.c 8.1 12/31/84)
11:
12: #define MAXALIASES 35
13:
14: static char SERVDB[] = "/etc/services";
15: static FILE *servf = NULL;
16: static char line[BUFSIZ+1];
17: static struct servent serv;
18: static char *serv_aliases[MAXALIASES];
19: static int stayopen = 0;
20: static char *any();
21:
22: setservent(f)
23: int f;
24: {
25: if (servf == NULL)
26: servf = fopen(SERVDB, "r" );
27: else
28: rewind(servf);
29: stayopen |= f;
30: }
31:
32: endservent()
33: {
34: if (servf && !stayopen) {
35: fclose(servf);
36: servf = NULL;
37: }
38: }
39:
40: struct servent *
41: getservent()
42: {
43: char *p;
44: register char *cp, **q;
45:
46:
47: if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
48: return (NULL);
49: again:
50: if ((p = fgets(line, BUFSIZ, servf)) == NULL)
51: return (NULL);
52: if (*p == '#')
53: goto again;
54: cp = any(p, "#\n");
55: if (cp == NULL)
56: goto again;
57: *cp = '\0';
58: serv.s_name = p;
59: p = any(p, " \t");
60: if (p == NULL)
61: goto again;
62: *p++ = '\0';
63: while (*p == ' ' || *p == '\t')
64: p++;
65: cp = any(p, ",/");
66: if (cp == NULL)
67: goto again;
68: *cp++ = '\0';
69: serv.s_port = htons((u_short)myatoi(p));
70: serv.s_proto = cp;
71: q = serv.s_aliases = serv_aliases;
72: cp = any(cp, " \t");
73: if (cp != NULL)
74: *cp++ = '\0';
75: while (cp && *cp) {
76: if (*cp == ' ' || *cp == '\t') {
77: cp++;
78: continue;
79: }
80: if (q < &serv_aliases[MAXALIASES - 1])
81: *q++ = cp;
82: cp = any(cp, " \t");
83: if (cp != NULL)
84: *cp++ = '\0';
85: }
86: *q = NULL;
87: return (&serv);
88: }
89:
90: static char *
91: any(cp, match)
92: register char *cp;
93: char *match;
94: {
95: register char *mp, c;
96:
97: while (c = *cp) {
98: for (mp = match; *mp; mp++)
99: if (*mp == c)
100: return (cp);
101: cp++;
102: }
103: return ((char *)0);
104: }
105:
106: static int
107: myatoi(p)
108: register char *p;
109: {
110: register int n;
111: register int f;
112:
113: n = 0;
114: f = 0;
115: for(;;p++) {
116: switch(*p) {
117: case ' ':
118: case '\t':
119: continue;
120: case '-':
121: f++;
122: case '+':
123: p++;
124: }
125: break;
126: }
127: while(*p >= '0' && *p <= '9')
128: n = n*10 + *p++ - '0';
129: return(f? -n: n);
130: }
Defined functions
any
defined in line
90; used 6 times
Defined variables
line
defined in line
16; used 1 times
serv
defined in line
17; used 5 times
Defined macros