source: trunk/libs/newlib/src/newlib/libc/sys/linux/net/inet_ntoa.c

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

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

File size: 2.8 KB
Line 
1/* Convert Inet number to ASCII representation.
2   Copyright (C) 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C 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   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library; if not, write to the Free
18   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   02111-1307 USA.  */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <arpa/inet.h>
24#include <bits/libc-lock.h>
25
26/* The interface of this function is completely stupid, it requires a
27   static buffer.  We relax this a bit in that we allow at least one
28   buffer for each thread.  */
29
30/* This is the key for the thread specific memory.  */
31static __libc_key_t key;
32
33/* If nonzero the key allocation failed and we should better use a
34   static buffer than fail.  */
35static char local_buf[18];
36static char *static_buf;
37
38/* Destructor for the thread-specific data.  */
39static void init (void);
40static void free_key_mem (void *mem);
41
42
43char *
44inet_ntoa (struct in_addr in)
45{
46  __libc_once_define (static, once);
47  char *buffer;
48  unsigned char *bytes;
49
50  /* If we have not yet initialized the buffer do it now.  */
51  __libc_once (once, init);
52
53  if (static_buf != NULL)
54    buffer = static_buf;
55  else
56    {
57      /* We don't use the static buffer and so we have a key.  Use it
58         to get the thread-specific buffer.  */
59      buffer = __libc_getspecific (key);
60      if (buffer == NULL)
61        {
62          /* No buffer allocated so far.  */
63          buffer = malloc (18);
64          if (buffer == NULL)
65            /* No more memory available.  We use the static buffer.  */
66            buffer = local_buf;
67          else
68            __libc_setspecific (key, buffer);
69        }
70    }
71
72  bytes = (unsigned char *) &in;
73  snprintf (buffer, 18, "%d.%d.%d.%d",
74              bytes[0], bytes[1], bytes[2], bytes[3]);
75
76  return buffer;
77}
78
79
80/* Initialize buffer.  */
81static void
82init (void)
83{
84  if (__libc_key_create (&key, free_key_mem))
85    /* Creating the key failed.  This means something really went
86       wrong.  In any case use a static buffer which is better than
87       nothing.  */
88    static_buf = local_buf;
89}
90
91
92/* Free the thread specific data, this is done if a thread terminates.  */
93static void
94free_key_mem (void *mem)
95{
96  free (mem);
97  __libc_setspecific (key, NULL);
98}
Note: See TracBrowser for help on using the repository browser.