Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / gai_strerror.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 2001 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <port_before.h>
19 #include <netdb.h>
20 #include <port_after.h>
21
22 #ifdef DO_PTHREADS
23 #include <pthread.h>
24 #include <stdlib.h>
25 #endif
26
27 static const char *gai_errlist[] = {
28         "no error",
29         "address family not supported for name",/* EAI_ADDRFAMILY */
30         "temporary failure",                    /* EAI_AGAIN */
31         "invalid flags",                        /* EAI_BADFLAGS */
32         "permanent failure",                    /* EAI_FAIL */
33         "address family not supported",         /* EAI_FAMILY */
34         "memory failure",                       /* EAI_MEMORY */
35         "no address",                           /* EAI_NODATA */
36         "unknown name or service",              /* EAI_NONAME */
37         "service not supported for socktype",   /* EAI_SERVICE */
38         "socktype not supported",               /* EAI_SOCKTYPE */
39         "system failure",                       /* EAI_SYSTEM */
40         "bad hints",                            /* EAI_BADHINTS */
41         "bad protocol",                         /* EAI_PROTOCOL */
42
43         "unknown error"                         /* Must be last. */
44 };
45
46 static const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist));
47
48 #define EAI_BUFSIZE 128
49
50 const char *
51 gai_strerror(int ecode) {
52 #ifndef DO_PTHREADS
53         static char buf[EAI_BUFSIZE];
54 #else   /* DO_PTHREADS */
55 #ifndef LIBBIND_MUTEX_INITIALIZER
56 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
57 #endif
58         static pthread_mutex_t lock = LIBBIND_MUTEX_INITIALIZER;
59         static pthread_key_t key;
60         static int once = 0;
61         char *buf;
62 #endif
63
64         if (ecode >= 0 && ecode < (gai_nerr - 1))
65                 return (gai_errlist[ecode]);
66
67 #ifdef DO_PTHREADS
68         if (!once) {
69                 if (pthread_mutex_lock(&lock) != 0)
70                         goto unknown;
71                 if (!once) {
72                         if (pthread_key_create(&key, free) != 0)
73                                 goto unknown;
74                         once = 1;
75                 }
76                 if (pthread_mutex_unlock(&lock) != 0)
77                         goto unknown;
78         }
79
80         buf = pthread_getspecific(key);
81         if (buf == NULL) {
82                 buf = malloc(EAI_BUFSIZE);
83                 if (buf == NULL)
84                         goto unknown;
85                 if (pthread_setspecific(key, buf) != 0) {
86                         free(buf);
87                         goto unknown;
88                 }
89         }
90 #endif
91         /* 
92          * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...).
93          * It is safe until message catalogs are used.
94          */
95         sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode);
96         return (buf);
97
98 #ifdef DO_PTHREADS
99  unknown:
100         return ("unknown error");
101 #endif
102 }