]>
Commit | Line | Data |
---|---|---|
5929563f | 1 | /* Convert Inet number to ASCII representation. |
8d9618b7 | 2 | Copyright (C) 1997, 1998 Free Software Foundation, Inc. |
5929563f UD |
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 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. | |
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 | Library General Public License for more details. | |
15 | ||
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. */ | |
20 | ||
28f540f4 | 21 | #include <stdio.h> |
5929563f UD |
22 | #include <stdlib.h> |
23 | #include <arpa/inet.h> | |
5107cf1d | 24 | #include <bits/libc-lock.h> |
5929563f UD |
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. */ | |
5929563f UD |
29 | |
30 | /* This is the key for the thread specific memory. */ | |
31 | static __libc_key_t key; | |
32 | ||
8f2ece69 UD |
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; | |
37 | ||
5929563f | 38 | /* Destructor for the thread-specific data. */ |
8f2ece69 | 39 | static void init (void); |
5929563f UD |
40 | static void free_key_mem (void *mem); |
41 | ||
28f540f4 RM |
42 | |
43 | char * | |
5929563f | 44 | inet_ntoa (struct in_addr in) |
28f540f4 | 45 | { |
0413b54c | 46 | __libc_once_define (static, once); |
8f2ece69 | 47 | char *buffer; |
5929563f UD |
48 | unsigned char *bytes; |
49 | ||
50 | /* If we have not yet initialized the buffer do it now. */ | |
8f2ece69 | 51 | __libc_once (once, init); |
5929563f | 52 | |
8f2ece69 | 53 | if (static_buf != NULL) |
0413b54c UD |
54 | buffer = static_buf; |
55 | else | |
5929563f | 56 | { |
0413b54c UD |
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); | |
5929563f | 60 | if (buffer == NULL) |
0413b54c UD |
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 | } | |
5929563f UD |
70 | } |
71 | ||
72 | bytes = (unsigned char *) ∈ | |
73 | snprintf (buffer, 18, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); | |
74 | ||
75 | return buffer; | |
76 | } | |
77 | ||
78 | ||
8f2ece69 UD |
79 | /* Initialize buffer. */ |
80 | static void | |
81 | init (void) | |
82 | { | |
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; | |
88 | } | |
89 | ||
90 | ||
8d9618b7 | 91 | /* Free the thread specific data, this is done if a thread terminates. */ |
5929563f UD |
92 | static void |
93 | free_key_mem (void *mem) | |
94 | { | |
95 | free (mem); | |
0413b54c | 96 | __libc_setspecific (key, NULL); |
28f540f4 | 97 | } |