summaryrefslogtreecommitdiffstats
path: root/lib/libc/net/gai_strerror.c
diff options
context:
space:
mode:
authorume <ume@FreeBSD.org>2009-11-09 12:46:59 +0000
committerume <ume@FreeBSD.org>2009-11-09 12:46:59 +0000
commita9dc4dbb6208ebff265d4470aa4d32abe2907222 (patch)
tree8c49f7e4e5b83c1a5dd5a3167bddb097ea94216b /lib/libc/net/gai_strerror.c
parent916f88dc5a30d26b53dcd17f3f9e551bcba53e05 (diff)
downloadFreeBSD-src-a9dc4dbb6208ebff265d4470aa4d32abe2907222.zip
FreeBSD-src-a9dc4dbb6208ebff265d4470aa4d32abe2907222.tar.gz
Add NLS catalogs support to gai_strerror(3).
Controlled by NLS define.
Diffstat (limited to 'lib/libc/net/gai_strerror.c')
-rw-r--r--lib/libc/net/gai_strerror.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/libc/net/gai_strerror.c b/lib/libc/net/gai_strerror.c
index 5611559..4f60f2d 100644
--- a/lib/libc/net/gai_strerror.c
+++ b/lib/libc/net/gai_strerror.c
@@ -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";
OpenPOWER on IntegriCloud