Add NLS catalogs support to gai_strerror(3).

Controlled by NLS define.
This commit is contained in:
Hajimu UMEMOTO 2009-11-09 12:46:59 +00:00
parent 42f4692f52
commit 0d384326cd
2 changed files with 94 additions and 0 deletions

View File

@ -30,7 +30,17 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <netdb.h>
#if defined(NLS)
#include <nl_types.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "reentrant.h"
#endif
#include "un-namespace.h"
/* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */
/* for backward compatibility with userland code prior to 2553bis-02 */
@ -52,9 +62,57 @@ static const char *ai_errlist[] = {
"Argument buffer overflow" /* EAI_OVERFLOW */
};
#if defined(NLS)
static char gai_buf[NL_TEXTMAX];
static once_t gai_init_once = ONCE_INITIALIZER;
static thread_key_t gai_key;
static int gai_keycreated = 0;
static void
gai_keycreate(void)
{
gai_keycreated = (thr_keycreate(&gai_key, free) == 0);
}
#endif
const char *
gai_strerror(int ecode)
{
#if defined(NLS)
nl_catd catd;
char *buf;
if (thr_main() != 0)
buf = gai_buf;
else {
if (thr_once(&gai_init_once, gai_keycreate) != 0 ||
!gai_keycreated)
goto thr_err;
if ((buf = thr_getspecific(gai_key)) == NULL) {
if ((buf = malloc(sizeof(gai_buf))) == NULL)
goto thr_err;
if (thr_setspecific(gai_key, buf) != 0) {
free(buf);
goto thr_err;
}
}
}
catd = catopen("libc", NL_CAT_LOCALE);
if (ecode > 0 && ecode < EAI_MAX)
strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]),
sizeof(gai_buf));
else if (ecode == 0)
strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"),
sizeof(gai_buf));
else
strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"),
sizeof(gai_buf));
catclose(catd);
return buf;
thr_err:
#endif
if (ecode >= 0 && ecode < EAI_MAX)
return ai_errlist[ecode];
return "Unknown error";

View File

@ -257,3 +257,39 @@ $ SIGUSR1
30 User defined signal 1
$ SIGUSR2
31 User defined signal 2
$
$ gai_strerror() support catalog
$
$set 3
$ 1 (obsolete)
1 Address family for hostname not supported
$ EAI_AGAIN
2 Temporary failure in name resolution
$ EAI_BADFLAGS
3 Invalid value for ai_flags
$ EAI_FAIL
4 Non-recoverable failure in name resolution
$ EAI_FAMILY
5 ai_family not supported
$ EAI_MEMORY
6 Memory allocation failure
$ 7 (obsolete)
7 No address associated with hostname
$ EAI_NONAME
8 hostname nor servname provided, or not known
$ EAI_SERVICE
9 servname not supported for ai_socktype
$ EAI_SOCKTYPE
10 ai_socktype not supported
$ EAI_SYSTEM
11 System error returned in errno
$ EAI_BADHINTS
12 Invalid value for hints
$ EAI_PROTOCOL
13 Resolved protocol is unknown
$ EAI_OVERFLOW
14 Argument buffer overflow
$ 0
32766 Success
$ NL_MSGMAX
32767 Unknown error