Commit | Line | Data |
---|---|---|
8ea4199d DF |
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 | ||
2ce81398 | 7 | #if defined(LIBC_SCCS) && !defined(lint) |
4533a593 | 8 | static char sccsid[] = "@(#)getservent.c 5.4 (Berkeley) %G%"; |
2ce81398 | 9 | #endif LIBC_SCCS and not lint |
229225cd SL |
10 | |
11 | #include <stdio.h> | |
4533a593 | 12 | #include <sys/param.h> |
62fc8b9c | 13 | #include <sys/types.h> |
229225cd SL |
14 | #include <sys/socket.h> |
15 | #include <netdb.h> | |
16 | #include <ctype.h> | |
17 | ||
18 | #define MAXALIASES 35 | |
19 | ||
62fc8b9c | 20 | static char SERVDB[] = "/etc/services"; |
229225cd SL |
21 | static FILE *servf = NULL; |
22 | static char line[BUFSIZ+1]; | |
23 | static struct servent serv; | |
24 | static char *serv_aliases[MAXALIASES]; | |
229225cd | 25 | static char *any(); |
5442f2c7 | 26 | int _serv_stayopen; |
229225cd SL |
27 | |
28 | setservent(f) | |
29 | int f; | |
30 | { | |
31 | if (servf == NULL) | |
32 | servf = fopen(SERVDB, "r" ); | |
33 | else | |
34 | rewind(servf); | |
5442f2c7 | 35 | _serv_stayopen |= f; |
229225cd SL |
36 | } |
37 | ||
38 | endservent() | |
39 | { | |
5442f2c7 | 40 | if (servf) { |
229225cd SL |
41 | fclose(servf); |
42 | servf = NULL; | |
43 | } | |
5442f2c7 | 44 | _serv_stayopen = 0; |
229225cd SL |
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, BUFSIZ, 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'; | |
62fc8b9c | 75 | serv.s_port = htons((u_short)atoi(p)); |
229225cd | 76 | serv.s_proto = cp; |
9243aabf | 77 | q = serv.s_aliases = serv_aliases; |
229225cd | 78 | cp = any(cp, " \t"); |
463dee20 | 79 | if (cp != NULL) |
229225cd | 80 | *cp++ = '\0'; |
463dee20 SL |
81 | while (cp && *cp) { |
82 | if (*cp == ' ' || *cp == '\t') { | |
83 | cp++; | |
84 | continue; | |
229225cd | 85 | } |
463dee20 SL |
86 | if (q < &serv_aliases[MAXALIASES - 1]) |
87 | *q++ = cp; | |
88 | cp = any(cp, " \t"); | |
89 | if (cp != NULL) | |
90 | *cp++ = '\0'; | |
229225cd SL |
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 | } |