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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
** protocols.c                           /etc/protocols access functions
3
**
4
** This file is part of the NYS Library.
5
**
6
** The NYS Library is free software; you can redistribute it and/or
7
** modify it under the terms of the GNU Library General Public License as
8
** published by the Free Software Foundation; either version 2 of the
9
** License, or (at your option) any later version.
10
**
11
** The NYS Library is distributed in the hope that it will be useful,
12
** but WITHOUT ANY WARRANTY; without even the implied warranty of
13
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
** Library General Public License for more details.
15
**
16
** You should have received a copy of the GNU Library General Public
17
** License along with the NYS Library; see the file COPYING.LIB.  If
18
** not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19
** Cambridge, MA 02139, USA.
20
**
21
**
22
** Copyright (c) 1983 Regents of the University of California.
23
** All rights reserved.
24
**
25
** Redistribution and use in source and binary forms, with or without
26
** modification, are permitted provided that the following conditions
27
** are met:
28
** 1. Redistributions of source code must retain the above copyright
29
**    notice, this list of conditions and the following disclaimer.
30
** 2. Redistributions in binary form must reproduce the above copyright
31
**    notice, this list of conditions and the following disclaimer in the
32
**    documentation and/or other materials provided with the distribution.
33
** 3. All advertising materials mentioning features or use of this software
34
**    must display the following acknowledgement:
35
**      This product includes software developed by the University of
36
**      California, Berkeley and its contributors.
37
** 4. Neither the name of the University nor the names of its contributors
38
**    may be used to endorse or promote products derived from this software
39
**    without specific prior written permission.
40
**
41
** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
** SUCH DAMAGE.
52
*/
53
 
54
#define __FORCE_GLIBC
55
#define _GNU_SOURCE
56
#include <features.h>
57
#include <sys/types.h>
58
#include <sys/socket.h>
59
#include <netdb.h>
60
#include <stdio.h>
61
#include <stdlib.h>
62
#include <string.h>
63
#include <errno.h>
64
 
65
#ifdef __UCLIBC_HAS_THREADS__
66
#include <pthread.h>
67
static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
68
# define LOCK   __pthread_mutex_lock(&mylock)
69
# define UNLOCK __pthread_mutex_unlock(&mylock);
70
#else
71
# define LOCK
72
# define UNLOCK
73
#endif
74
 
75
 
76
 
77
#define MAXALIASES      35
78
 
79
static FILE *protof = NULL;
80
static struct protoent proto;
81
static char static_aliases[BUFSIZ+1 + sizeof(char *)*MAXALIASES];
82
static int proto_stayopen;
83
 
84
void setprotoent(int f)
85
{
86
    LOCK;
87
    if (protof == NULL)
88
        protof = fopen(_PATH_PROTOCOLS, "r" );
89
    else
90
        rewind(protof);
91
    proto_stayopen |= f;
92
    UNLOCK;
93
}
94
 
95
void endprotoent(void)
96
{
97
    LOCK;
98
    if (protof) {
99
        fclose(protof);
100
        protof = NULL;
101
    }
102
    proto_stayopen = 0;
103
    UNLOCK;
104
}
105
 
106
int getprotoent_r(struct protoent *result_buf,
107
                  char *buf, size_t buflen,
108
                  struct protoent **result)
109
{
110
    char *p;
111
    register char *cp, **q;
112
    char **proto_aliases;
113
    char *line;
114
 
115
    *result = NULL;
116
 
117
    if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
118
        errno=ERANGE;
119
        return errno;
120
    }
121
    LOCK;
122
    proto_aliases=(char **)buf;
123
    buf+=sizeof(*proto_aliases)*MAXALIASES;
124
    buflen-=sizeof(*proto_aliases)*MAXALIASES;
125
 
126
    if (buflen < BUFSIZ+1) {
127
        UNLOCK;
128
        errno=ERANGE;
129
        return errno;
130
    }
131
    line=buf;
132
    buf+=BUFSIZ+1;
133
    buflen-=BUFSIZ+1;
134
 
135
    if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
136
        UNLOCK;
137
        return errno;
138
    }
139
again:
140
    if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
141
        UNLOCK;
142
        return TRY_AGAIN;
143
    }
144
 
145
    if (*p == '#')
146
        goto again;
147
    cp = strpbrk(p, "#\n");
148
    if (cp == NULL)
149
        goto again;
150
    *cp = '\0';
151
    result_buf->p_name = p;
152
    cp = strpbrk(p, " \t");
153
    if (cp == NULL)
154
        goto again;
155
    *cp++ = '\0';
156
    while (*cp == ' ' || *cp == '\t')
157
        cp++;
158
    p = strpbrk(cp, " \t");
159
    if (p != NULL)
160
        *p++ = '\0';
161
    result_buf->p_proto = atoi(cp);
162
    q = result_buf->p_aliases = proto_aliases;
163
    if (p != NULL) {
164
        cp = p;
165
        while (cp && *cp) {
166
            if (*cp == ' ' || *cp == '\t') {
167
                cp++;
168
                continue;
169
            }
170
            if (q < &proto_aliases[MAXALIASES - 1])
171
                *q++ = cp;
172
            cp = strpbrk(cp, " \t");
173
            if (cp != NULL)
174
                *cp++ = '\0';
175
        }
176
    }
177
    *q = NULL;
178
    *result=result_buf;
179
    UNLOCK;
180
    return 0;
181
}
182
 
183
struct protoent * getprotoent(void)
184
{
185
    struct protoent *result;
186
    getprotoent_r(&proto, static_aliases, sizeof(static_aliases), &result);
187
    return result;
188
}
189
 
190
 
191
int getprotobyname_r(const char *name,
192
                    struct protoent *result_buf,
193
                    char *buf, size_t buflen,
194
                    struct protoent **result)
195
{
196
    register char **cp;
197
    int ret;
198
 
199
    LOCK;
200
    setprotoent(proto_stayopen);
201
    while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
202
        if (strcmp(result_buf->p_name, name) == 0)
203
            break;
204
        for (cp = result_buf->p_aliases; *cp != 0; cp++)
205
            if (strcmp(*cp, name) == 0)
206
                goto found;
207
    }
208
found:
209
    if (!proto_stayopen)
210
        endprotoent();
211
    UNLOCK;
212
    return *result?0:ret;
213
}
214
 
215
 
216
struct protoent * getprotobyname(const char *name)
217
{
218
    struct protoent *result;
219
    getprotobyname_r(name, &proto, static_aliases, sizeof(static_aliases), &result);
220
    return result;
221
}
222
 
223
 
224
int getprotobynumber_r (int proto_num,
225
                        struct protoent *result_buf,
226
                        char *buf, size_t buflen,
227
                        struct protoent **result)
228
{
229
    int ret;
230
 
231
    LOCK;
232
    setprotoent(proto_stayopen);
233
    while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
234
        if (result_buf->p_proto == proto_num)
235
            break;
236
    if (!proto_stayopen)
237
        endprotoent();
238
    UNLOCK;
239
    return *result?0:ret;
240
}
241
 
242
struct protoent * getprotobynumber(int proto_num)
243
{
244
    struct protoent *result;
245
    getprotobynumber_r(proto_num, &proto, static_aliases, sizeof(static_aliases), &result);
246
    return result;
247
}
248
 

powered by: WebSVN 2.1.0

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