OSDN Git Service

6224243d1f4859b59925f4746bc4471632bab6c4
[uclinux-h8/uClibc.git] / libc / inet / getnetent.c
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 #define __FORCE_GLIBC
19 #include <features.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <malloc.h>
23 #include <netdb.h>
24 #include <arpa/inet.h>
25 #include <unistd.h>
26
27 libc_hidden_proto(fopen)
28 libc_hidden_proto(fclose)
29 libc_hidden_proto(inet_network)
30 libc_hidden_proto(rewind)
31 libc_hidden_proto(fgets)
32 libc_hidden_proto(abort)
33 libc_hidden_proto(__uc_malloc)
34
35
36 #include <bits/uClibc_mutex.h>
37 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
38
39
40
41 #define MAXALIASES      35
42 static const char NETDB[] = _PATH_NETWORKS;
43 static FILE *netf = NULL;
44 static char *line = NULL;
45 static struct netent net;
46 static char *net_aliases[MAXALIASES];
47
48 smallint _net_stayopen attribute_hidden;
49
50 libc_hidden_proto(setnetent)
51 void setnetent(int f)
52 {
53     __UCLIBC_MUTEX_LOCK(mylock);
54     if (netf == NULL)
55         netf = fopen(NETDB, "r" );
56     else
57         rewind(netf);
58     if (f) _net_stayopen = 1;
59     __UCLIBC_MUTEX_UNLOCK(mylock);
60     return;
61 }
62 libc_hidden_def(setnetent)
63
64 libc_hidden_proto(endnetent)
65 void endnetent(void)
66 {
67     __UCLIBC_MUTEX_LOCK(mylock);
68     if (netf) {
69         fclose(netf);
70         netf = NULL;
71     }
72     _net_stayopen = 0;
73     __UCLIBC_MUTEX_UNLOCK(mylock);
74 }
75 libc_hidden_def(endnetent)
76
77 static char * any(register char *cp, char *match)
78 {
79     register char *mp, c;
80
81     while ((c = *cp)) {
82         for (mp = match; *mp; mp++)
83             if (*mp == c)
84                 return (cp);
85         cp++;
86     }
87     return ((char *)0);
88 }
89
90 libc_hidden_proto(getnetent)
91 struct netent *getnetent(void)
92 {
93     char *p;
94     register char *cp, **q;
95     struct netent *rv = NULL;
96
97     __UCLIBC_MUTEX_LOCK(mylock);
98     if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
99         goto DONE;
100     }
101 again:
102
103     if (!line) {
104         line = __uc_malloc(BUFSIZ + 1);
105     }
106
107     p = fgets(line, BUFSIZ, netf);
108     if (p == NULL) {
109         goto DONE;
110     }
111     if (*p == '#')
112         goto again;
113     cp = any(p, "#\n");
114     if (cp == NULL)
115         goto again;
116     *cp = '\0';
117     net.n_name = p;
118     cp = any(p, " \t");
119     if (cp == NULL)
120         goto again;
121     *cp++ = '\0';
122     while (*cp == ' ' || *cp == '\t')
123         cp++;
124     p = any(cp, " \t");
125     if (p != NULL)
126         *p++ = '\0';
127     net.n_net = inet_network(cp);
128     net.n_addrtype = AF_INET;
129     q = net.n_aliases = net_aliases;
130     if (p != NULL)
131         cp = p;
132     while (cp && *cp) {
133         if (*cp == ' ' || *cp == '\t') {
134             cp++;
135             continue;
136         }
137         if (q < &net_aliases[MAXALIASES - 1])
138             *q++ = cp;
139         cp = any(cp, " \t");
140         if (cp != NULL)
141             *cp++ = '\0';
142     }
143     *q = NULL;
144     rv = &net;
145 DONE:
146     __UCLIBC_MUTEX_UNLOCK(mylock);
147     return rv;
148 }
149 libc_hidden_def(getnetent)