xref: /original-bsd/lib/libc/net/getservent.c (revision 7dc4431a)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
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.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)getservent.c	5.7 (Berkeley) 05/21/90";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <stdio.h>
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netdb.h>
27 #include <ctype.h>
28 
29 #define	MAXALIASES	35
30 
31 static FILE *servf = NULL;
32 static char line[BUFSIZ+1];
33 static struct servent serv;
34 static char *serv_aliases[MAXALIASES];
35 static char *any();
36 int _serv_stayopen;
37 
38 setservent(f)
39 	int f;
40 {
41 	if (servf == NULL)
42 		servf = fopen(_PATH_SERVICES, "r" );
43 	else
44 		rewind(servf);
45 	_serv_stayopen |= f;
46 }
47 
48 endservent()
49 {
50 	if (servf) {
51 		fclose(servf);
52 		servf = NULL;
53 	}
54 	_serv_stayopen = 0;
55 }
56 
57 struct servent *
58 getservent()
59 {
60 	char *p;
61 	register char *cp, **q;
62 
63 	if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
64 		return (NULL);
65 again:
66 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
67 		return (NULL);
68 	if (*p == '#')
69 		goto again;
70 	cp = any(p, "#\n");
71 	if (cp == NULL)
72 		goto again;
73 	*cp = '\0';
74 	serv.s_name = p;
75 	p = any(p, " \t");
76 	if (p == NULL)
77 		goto again;
78 	*p++ = '\0';
79 	while (*p == ' ' || *p == '\t')
80 		p++;
81 	cp = any(p, ",/");
82 	if (cp == NULL)
83 		goto again;
84 	*cp++ = '\0';
85 	serv.s_port = htons((u_short)atoi(p));
86 	serv.s_proto = cp;
87 	q = serv.s_aliases = serv_aliases;
88 	cp = any(cp, " \t");
89 	if (cp != NULL)
90 		*cp++ = '\0';
91 	while (cp && *cp) {
92 		if (*cp == ' ' || *cp == '\t') {
93 			cp++;
94 			continue;
95 		}
96 		if (q < &serv_aliases[MAXALIASES - 1])
97 			*q++ = cp;
98 		cp = any(cp, " \t");
99 		if (cp != NULL)
100 			*cp++ = '\0';
101 	}
102 	*q = NULL;
103 	return (&serv);
104 }
105 
106 static char *
107 any(cp, match)
108 	register char *cp;
109 	char *match;
110 {
111 	register char *mp, c;
112 
113 	while (c = *cp) {
114 		for (mp = match; *mp; mp++)
115 			if (*mp == c)
116 				return (cp);
117 		cp++;
118 	}
119 	return ((char *)0);
120 }
121