Linux specific internet socket definitions.
[glibc/history.git] / inet / inet_ntoa.c
blobb6087a982f00041d1d1bd3679006487b6cc644d8
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 <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. */
29 __libc_lock_define_initialized (static, lock);
31 /* This is the key for the thread specific memory. */
32 static __libc_key_t key;
34 /* Destructor for the thread-specific data. */
35 static void free_key_mem (void *mem);
38 char *
39 inet_ntoa (struct in_addr in)
41 static char static_buf[18];
42 static int initialized = 0;
43 char *buffer = NULL;
44 unsigned char *bytes;
46 /* If we have not yet initialized the buffer do it now. */
47 if (!initialized)
49 /* Make sure there is only one process doing the initialization. */
50 __libc_lock_lock (lock);
52 if (!initialized)
54 if (__libc_key_create (&key, free_key_mem))
55 /* Creating the key failed. This either means we run
56 have only a single-threaded application or something
57 really went wrong. In any case use a static buffer
58 which is better than nothing. */
59 buffer = static_buf;
62 __libc_lock_unlock (lock);
65 if (buffer == NULL)
67 /* We don't use the static buffer and so we have a key. Use it
68 to get the thread-specific buffer. */
69 buffer = __libc_getspecific (key);
70 if (buffer == NULL)
72 /* No buffer allocated so far. */
73 buffer = malloc (18);
74 if (buffer == NULL)
75 /* No more memory available. We use the static buffer. */
76 buffer = static_buf;
77 else
78 __libc_setspecific (key, buffer);
82 bytes = (unsigned char *) &in;
83 snprintf (buffer, 18, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
85 return buffer;
89 static void
90 free_key_mem (void *mem)
92 free (mem);
94 /* And we must set the data to NULL so that the destructor is not
95 called again. */
96 __libc_setspecific (key, NULL);