secondary cache feature in vm.
[minix.git] / lib / libc / ip / getservent.c
blob9e89c3823248ce247f60f83ce7100856f11b8841
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)getservent.c 5.8 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include <net/hton.h>
29 #include <net/gen/netdb.h>
31 #define MAXALIASES 35
33 static FILE *servf = NULL;
34 static char line[BUFSIZ+1];
35 static struct servent serv;
36 static char *serv_aliases[MAXALIASES];
37 int _serv_stayopen;
39 static char *any _ARGS(( char *cp, char *match ));
41 void
42 setservent(f)
43 int f;
45 if (servf == NULL)
46 servf = fopen(_PATH_SERVICES, "r" );
47 else
48 rewind(servf);
49 _serv_stayopen |= f;
52 void
53 endservent()
55 if (servf) {
56 fclose(servf);
57 servf = NULL;
59 _serv_stayopen = 0;
62 struct servent *
63 getservent()
65 char *p;
66 register char *cp, **q;
68 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
69 return (NULL);
70 again:
71 if ((p = fgets(line, BUFSIZ, servf)) == NULL)
72 return (NULL);
73 if (*p == '#')
74 goto again;
75 cp = any(p, "#\n");
76 if (cp == NULL)
77 goto again;
78 *cp = '\0';
79 serv.s_name = p;
80 p = any(p, " \t");
81 if (p == NULL)
82 goto again;
83 *p++ = '\0';
84 while (*p == ' ' || *p == '\t')
85 p++;
86 cp = any(p, ",/");
87 if (cp == NULL)
88 goto again;
89 *cp++ = '\0';
90 serv.s_port = htons((u16_t)atoi(p));
91 serv.s_proto = cp;
92 q = serv.s_aliases = serv_aliases;
93 cp = any(cp, " \t");
94 if (cp != NULL)
95 *cp++ = '\0';
96 while (cp && *cp) {
97 if (*cp == ' ' || *cp == '\t') {
98 cp++;
99 continue;
101 if (q < &serv_aliases[MAXALIASES - 1])
102 *q++ = cp;
103 cp = any(cp, " \t");
104 if (cp != NULL)
105 *cp++ = '\0';
107 *q = NULL;
108 return (&serv);
111 static char *
112 any(cp, match)
113 register char *cp;
114 char *match;
116 register char *mp, c;
118 while (c = *cp) {
119 for (mp = match; *mp; mp++)
120 if (*mp == c)
121 return (cp);
122 cp++;
124 return ((char *)0);