Commit | Line | Data |
---|---|---|
8ea4199d DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
6b2f9dd0 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
f4f66d2c KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
8ea4199d DF |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
ca67e7b4 | 19 | static char sccsid[] = "@(#)getservent.c 5.6 (Berkeley) 6/27/88"; |
6b2f9dd0 | 20 | #endif /* LIBC_SCCS and not lint */ |
229225cd SL |
21 | |
22 | #include <stdio.h> | |
4533a593 | 23 | #include <sys/param.h> |
62fc8b9c | 24 | #include <sys/types.h> |
229225cd SL |
25 | #include <sys/socket.h> |
26 | #include <netdb.h> | |
27 | #include <ctype.h> | |
28 | ||
29 | #define MAXALIASES 35 | |
30 | ||
62fc8b9c | 31 | static char SERVDB[] = "/etc/services"; |
229225cd SL |
32 | static FILE *servf = NULL; |
33 | static char line[BUFSIZ+1]; | |
34 | static struct servent serv; | |
35 | static char *serv_aliases[MAXALIASES]; | |
229225cd | 36 | static char *any(); |
5442f2c7 | 37 | int _serv_stayopen; |
229225cd SL |
38 | |
39 | setservent(f) | |
40 | int f; | |
41 | { | |
42 | if (servf == NULL) | |
43 | servf = fopen(SERVDB, "r" ); | |
44 | else | |
45 | rewind(servf); | |
5442f2c7 | 46 | _serv_stayopen |= f; |
229225cd SL |
47 | } |
48 | ||
49 | endservent() | |
50 | { | |
5442f2c7 | 51 | if (servf) { |
229225cd SL |
52 | fclose(servf); |
53 | servf = NULL; | |
54 | } | |
5442f2c7 | 55 | _serv_stayopen = 0; |
229225cd SL |
56 | } |
57 | ||
58 | struct servent * | |
59 | getservent() | |
60 | { | |
61 | char *p; | |
62 | register char *cp, **q; | |
63 | ||
64 | if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL) | |
65 | return (NULL); | |
66 | again: | |
67 | if ((p = fgets(line, BUFSIZ, servf)) == 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 | serv.s_name = p; | |
76 | p = any(p, " \t"); | |
77 | if (p == NULL) | |
78 | goto again; | |
79 | *p++ = '\0'; | |
80 | while (*p == ' ' || *p == '\t') | |
81 | p++; | |
82 | cp = any(p, ",/"); | |
83 | if (cp == NULL) | |
84 | goto again; | |
85 | *cp++ = '\0'; | |
62fc8b9c | 86 | serv.s_port = htons((u_short)atoi(p)); |
229225cd | 87 | serv.s_proto = cp; |
9243aabf | 88 | q = serv.s_aliases = serv_aliases; |
229225cd | 89 | cp = any(cp, " \t"); |
463dee20 | 90 | if (cp != NULL) |
229225cd | 91 | *cp++ = '\0'; |
463dee20 SL |
92 | while (cp && *cp) { |
93 | if (*cp == ' ' || *cp == '\t') { | |
94 | cp++; | |
95 | continue; | |
229225cd | 96 | } |
463dee20 SL |
97 | if (q < &serv_aliases[MAXALIASES - 1]) |
98 | *q++ = cp; | |
99 | cp = any(cp, " \t"); | |
100 | if (cp != NULL) | |
101 | *cp++ = '\0'; | |
229225cd SL |
102 | } |
103 | *q = NULL; | |
104 | return (&serv); | |
105 | } | |
106 | ||
107 | static char * | |
108 | any(cp, match) | |
109 | register char *cp; | |
110 | char *match; | |
111 | { | |
112 | register char *mp, c; | |
113 | ||
114 | while (c = *cp) { | |
115 | for (mp = match; *mp; mp++) | |
116 | if (*mp == c) | |
117 | return (cp); | |
118 | cp++; | |
119 | } | |
120 | return ((char *)0); | |
121 | } |