OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [getnetent.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.