OSDN Git Service

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