(HAVE_DWARF2_UNWIND_INFO_STATIC): Define it if gcc uses static
[glibc/history.git] / inet / inet_ntoa.c
blobfc2c3dd9b8e0dc7290fc9f5be2145fcba066d55f
1 /* Convert Inet number to ASCII representation.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C 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.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <arpa/inet.h>
24 #include <bits/libc-lock.h>
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. */
30 /* This is the key for the thread specific memory. */
31 static __libc_key_t key;
33 /* If nonzero the key allocation failed and we should better use a
34 static buffer than fail. */
35 static char local_buf[18];
36 static char *static_buf;
38 /* Destructor for the thread-specific data. */
39 static void init (void);
40 static void free_key_mem (void *mem);
43 char *
44 inet_ntoa (struct in_addr in)
46 __libc_once_define (static, once);
47 char *buffer;
48 unsigned char *bytes;
50 /* If we have not yet initialized the buffer do it now. */
51 __libc_once (once, init);
53 if (static_buf != NULL)
54 buffer = static_buf;
55 else
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)
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);
72 bytes = (unsigned char *) &in;
73 snprintf (buffer, 18, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
75 return buffer;
79 /* Initialize buffer. */
80 static void
81 init (void)
83 if (__libc_key_create (&key, free_key_mem))
84 /* Creating the key failed. This means something really went
85 wrong. In any case use a static buffer which is better than
86 nothing. */
87 static_buf = local_buf;
91 /* free the thread specific data, this is done if a thread terminates. */
92 static void
93 free_key_mem (void *mem)
95 free (mem);
96 __libc_setspecific (key, NULL);