* iconv/gconv_conf.c (builtin_aliases): Mark as const.
[glibc.git] / inet / inet_ntoa.c
blob889435dd10261753147330f553871d1282a94c34
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.
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.
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.
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. */
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",
74 bytes[0], bytes[1], bytes[2], bytes[3]);
76 return buffer;
80 /* Initialize buffer. */
81 static void
82 init (void)
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;
92 /* Free the thread specific data, this is done if a thread terminates. */
93 static void
94 free_key_mem (void *mem)
96 free (mem);
97 __libc_setspecific (key, NULL);