~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/.svn/text-base/getprotobyname.c.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 
2
 *
 
3
 * portable implementation of getprotobyname
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#include <stdio.h>
 
23
#include <impl/framework.h>
 
24
 
 
25
 
 
26
//#define GETPROTOBYNAME_DEBUG
 
27
 
 
28
/* locals */
 
29
 
 
30
#define MAXALIASES      35
 
31
 
 
32
static char line[BUFSIZ+1];
 
33
static char *proto_aliases[MAXALIASES];
 
34
static struct pgm_protoent_t proto;
 
35
 
 
36
static
 
37
struct pgm_protoent_t*
 
38
_pgm_native_getprotobyname (
 
39
        const char*     name
 
40
        )
 
41
{
 
42
        struct protoent* pe;
 
43
        char **q, **r;
 
44
        size_t len;
 
45
 
 
46
        if (NULL == name)
 
47
                return NULL;
 
48
 
 
49
#ifdef CONFIG_HAVE_GETPROTOBYNAME_R
 
50
        char buf[BUFSIZ];
 
51
        struct protoent protobuf;
 
52
        if (NULL == (pe = getprotobyname_r (name, &protobuf, buf, BUFSIZ)))
 
53
                return NULL;
 
54
#elif defined(CONFIG_HAVE_GETPROTOBYNAME_R2)
 
55
        char buf[BUFSIZ];
 
56
        struct protoent protobuf;
 
57
        if (0 != getprotobyname_r (name, &protobuf, buf, BUFSIZ, &pe) || NULL == pe)
 
58
                return NULL;
 
59
#else
 
60
        if (NULL == (pe = getprotobyname (name)))
 
61
                return NULL;
 
62
#endif
 
63
        len = strlen (pe->p_name) + 1;
 
64
        if (len > BUFSIZ)
 
65
                return NULL;
 
66
        proto.p_name = memcpy (line, pe->p_name, len);
 
67
        q = proto.p_aliases = proto_aliases;
 
68
        r = pe->p_aliases;
 
69
        while (*r) {
 
70
                const size_t alias_len = strlen (*r) + 1;
 
71
                if ((len + alias_len) > BUFSIZ)
 
72
                        break;
 
73
                *q++ = memcpy (line + len, *r++, alias_len);
 
74
                len += alias_len;
 
75
        }
 
76
        *q = NULL;
 
77
        proto.p_proto = pe->p_proto;
 
78
        return &proto;
 
79
}
 
80
 
 
81
struct pgm_protoent_t*
 
82
pgm_getprotobyname (
 
83
        const char*     name
 
84
        )
 
85
{
 
86
        return _pgm_native_getprotobyname (name);
 
87
}
 
88
 
 
89
/* eof */
 
90