source: trunk/libs/newlib/src/newlib/libc/sys/linux/net/getXXbyYY.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.2 KB
Line 
1/* Copyright (C) 1996-2001,2003, 2004 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with the GNU C Library; if not, write to the Free
16   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17   02111-1307 USA.  */
18
19#include <assert.h>
20#include <errno.h>
21#define _IO_MTSAFE_IO
22#include <bits/libc-lock.h>
23#include <stdlib.h>
24#include <resolv.h>
25
26#include "nsswitch.h"
27
28/*******************************************************************\
29|* Here we assume several symbols to be defined:                   *|
30|*                                                                 *|
31|* LOOKUP_TYPE   - the return type of the function                 *|
32|*                                                                 *|
33|* FUNCTION_NAME - name of the non-reentrant function              *|
34|*                                                                 *|
35|* DATABASE_NAME - name of the database the function accesses      *|
36|*                 (e.g., host, services, ...)                     *|
37|*                                                                 *|
38|* ADD_PARAMS    - additional parameter, can vary in number        *|
39|*                                                                 *|
40|* ADD_VARIABLES - names of additional parameter                   *|
41|*                                                                 *|
42|* BUFLEN        - length of buffer allocated for the non          *|
43|*                 reentrant version                               *|
44|*                                                                 *|
45|* Optionally the following vars can be defined:                   *|
46|*                                                                 *|
47|* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
48|*                 the global `h_errno' variable.                  *|
49|*                                                                 *|
50\*******************************************************************/
51
52/* To make the real sources a bit prettier.  */
53#define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
54#define APPEND_R(name) APPEND_R1 (name)
55#define APPEND_R1(name) name##_r
56#define INTERNAL(name) INTERNAL1 (name)
57#define INTERNAL1(name) __##name
58
59/* Sometimes we need to store error codes in the `h_errno' variable.  */
60#ifdef NEED_H_ERRNO
61# define H_ERRNO_PARM , int *h_errnop
62# define H_ERRNO_VAR , &h_errno_tmp
63# define H_ERRNO_VAR_P &h_errno_tmp
64#else
65# define H_ERRNO_PARM
66# define H_ERRNO_VAR
67# define H_ERRNO_VAR_P NULL
68#endif
69
70#ifdef HAVE_AF
71# define AF_VAL af
72#else
73# define AF_VAL AF_INET
74#endif
75
76/* Prototype for reentrant version we use here.  */
77extern int INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf,
78                                      char *buffer, size_t buflen,
79                                      LOOKUP_TYPE **result H_ERRNO_PARM);
80
81/* We need to protect the dynamic buffer handling.  */
82__libc_lock_define_initialized (static, lock);
83
84/* This points to the static buffer used.  */
85libc_freeres_ptr (static char *buffer);
86
87
88LOOKUP_TYPE *
89FUNCTION_NAME (ADD_PARAMS)
90{
91  static size_t buffer_size;
92  static LOOKUP_TYPE resbuf;
93  LOOKUP_TYPE *result;
94#ifdef NEED_H_ERRNO
95  int h_errno_tmp = 0;
96#endif
97
98  /* Get lock.  */
99  __libc_lock_lock (lock);
100
101  if (buffer == NULL)
102    {
103      buffer_size = BUFLEN;
104      buffer = (char *) malloc (buffer_size);
105    }
106
107#ifdef HANDLE_DIGITS_DOTS
108  if (buffer != NULL)
109    {
110      if (__nss_hostname_digits_dots (name, &resbuf, &buffer,
111                                      &buffer_size, 0, &result, NULL, AF_VAL,
112                                      H_ERRNO_VAR_P))
113        goto done;
114    }
115#endif
116
117  while (buffer != NULL
118         && (INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, &resbuf, buffer,
119                                        buffer_size, &result H_ERRNO_VAR)
120             == ERANGE)
121#ifdef NEED_H_ERRNO
122         && h_errno_tmp == NETDB_INTERNAL
123#endif
124         )
125    {
126      char *new_buf;
127      buffer_size *= 2;
128      new_buf = (char *) realloc (buffer, buffer_size);
129      if (new_buf == NULL)
130        {
131          /* We are out of memory.  Free the current buffer so that the
132             process gets a chance for a normal termination.  */
133          free (buffer);
134          errno = (ENOMEM);
135        }
136      buffer = new_buf;
137    }
138
139  if (buffer == NULL)
140    result = NULL;
141
142#ifdef HANDLE_DIGITS_DOTS
143done:
144#endif
145  /* Release lock.  */
146  __libc_lock_unlock (lock);
147
148#ifdef NEED_H_ERRNO
149  if (h_errno_tmp != 0)
150    h_errno = (h_errno_tmp);
151#endif
152
153  return result;
154}
155
156static_link_warning (FUNCTION_NAME)
Note: See TracBrowser for help on using the repository browser.