OSDN Git Service

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