From 31ef43753e9e7f16e3737a7e71286561c1421dd2 Mon Sep 17 00:00:00 2001 From: phantom Date: Sun, 27 Feb 2005 16:58:28 +0000 Subject: Add NLS catalogs support to strerror(), strerror_r() and strsignal(). Controlled by NLS define, currently disabled by default. Idea obtained from: NetBSD --- lib/libc/string/strerror.c | 61 +++++++++++++++++++++++++++++++----------- lib/libc/string/strsignal.c | 65 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 97 insertions(+), 29 deletions(-) diff --git a/lib/libc/string/strerror.c b/lib/libc/string/strerror.c index 02fba0c..30894aa 100644 --- a/lib/libc/string/strerror.c +++ b/lib/libc/string/strerror.c @@ -37,25 +37,31 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD$"); +#if defined(NLS) +#include +#include +#endif + #include -#include #include +#include -#define UPREFIX "Unknown error: " +#define UPREFIX "Unknown error" /* * Define a buffer size big enough to describe a 64-bit signed integer * converted to ASCII decimal (19 bytes), with an optional leading sign - * (1 byte); finally, we get the prefix and a trailing NUL from UPREFIX. + * (1 byte); finally, we get the prefix, delimiter (": ") and a trailing + * NUL from UPREFIX. */ -#define EBUFSIZE (20 + sizeof(UPREFIX)) +#define EBUFSIZE (20 + 2 + sizeof(UPREFIX)) /* * Doing this by hand instead of linking with stdio(3) avoids bloat for * statically linked binaries. */ static void -errstr(int num, char *buf, size_t len) +errstr(int num, char *uprefix, char *buf, size_t len) { char *t; unsigned int uerr; @@ -69,31 +75,56 @@ errstr(int num, char *buf, size_t len) } while (uerr /= 10); if (num < 0) *--t = '-'; - strlcpy(buf, UPREFIX, len); + *--t = ' '; + *--t = ':'; + strlcpy(buf, uprefix, len); strlcat(buf, t, len); } int strerror_r(int errnum, char *strerrbuf, size_t buflen) { + int retval = 0; +#if defined(NLS) + int saved_errno = errno; + nl_catd catd; + catd = catopen("libc", NL_CAT_LOCALE); +#endif if (errnum < 1 || errnum >= sys_nerr) { - errstr(errnum, strerrbuf, buflen); - return (EINVAL); + errstr(errnum, +#if defined(NLS) + catgets(catd, 1, 0xffff, UPREFIX), +#else + UPREFIX, +#endif + strerrbuf, buflen); + retval = EINVAL; + } else { + if (strlcpy(strerrbuf, +#if defined(NLS) + catgets(catd, 1, errnum, sys_errlist[errnum]), +#else + sys_errlist[errnum], +#endif + buflen) >= buflen) + retval = ERANGE; } - if (strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen) - return (ERANGE); - return (0); + +#if defined(NLS) + catclose(catd); + errno = saved_errno; +#endif + + return (retval); } char * strerror(int num) { - static char ebuf[EBUFSIZE]; + static char ebuf[NL_TEXTMAX]; - if (num > 0 && num < sys_nerr) - return ((char *)sys_errlist[num]); + if (strerror_r(num, ebuf, sizeof(ebuf)) != 0) errno = EINVAL; - errstr(num, ebuf, sizeof(ebuf)); return (ebuf); } diff --git a/lib/libc/string/strsignal.c b/lib/libc/string/strsignal.c index f839e44..ed8a6e6 100644 --- a/lib/libc/string/strsignal.c +++ b/lib/libc/string/strsignal.c @@ -37,38 +37,75 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD$"); -#include +#if defined(NLS) +#include +#include +#endif + +#include #include #include +#define UPREFIX "Unknown signal" + +/* XXX: negative 'num' ? (REGR) */ char * -strsignal(num) - int num; +strsignal(int num) { -#define UPREFIX "Unknown signal: " - static char ebuf[40] = UPREFIX; /* 64-bit number + slop */ - unsigned int signum; - char *p, *t; - char tmp[40]; + static char ebuf[NL_TEXTMAX]; + char tmp[20]; + int signum, n; + char *t, *p; - signum = num; /* convert to unsigned */ - if (signum < sys_nsig) - return ((char *)sys_siglist[signum]); +#if defined(NLS) + int saved_errno = errno; + nl_catd catd; + catd = catopen("libc", NL_CAT_LOCALE); +#endif - /* Do this by hand, so we don't link to stdio(3). */ - t = tmp; + if (num > 0 && num < sys_nsig) { + strlcpy(ebuf, +#if defined(NLS) + catgets(catd, 2, num, sys_siglist[num]), +#else + sys_siglist[num], +#endif + sizeof(ebuf)); + } else { + n = strlcpy(ebuf, +#if defined(NLS) + catgets(catd, 2, 0xffff, UPREFIX), +#else + UPREFIX, +#endif + sizeof(ebuf)); + } + + signum = num; if (num < 0) signum = -signum; + + t = tmp; do { *t++ = "0123456789"[signum % 10]; } while (signum /= 10); if (num < 0) *t++ = '-'; - for (p = ebuf + sizeof(UPREFIX) - 1;;) { + + p = (ebuf + n); + *p++ = ':'; + *p++ = ' '; + + for (;;) { *p++ = *--t; if (t <= tmp) break; } *p = '\0'; + +#if defined(NLS) + catclose(catd); + errno = saved_errno; +#endif return (ebuf); } -- cgit v1.1