Add NLS catalogs support to strerror(), strerror_r() and strsignal().

Controlled by NLS define, currently disabled by default.

Idea obtained from:	NetBSD
This commit is contained in:
Alexey Zelkin 2005-02-27 16:58:28 +00:00
parent 8c4346e3e8
commit fba5c5fa09
2 changed files with 99 additions and 31 deletions

View File

@ -37,25 +37,31 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <errno.h>
#include <stdio.h>
#include <string.h>
#if defined(NLS)
#include <limits.h>
#include <nl_types.h>
#endif
#define UPREFIX "Unknown error: "
#include <errno.h>
#include <string.h>
#include <stdio.h>
#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);
}

View File

@ -37,38 +37,75 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <stdio.h>
#if defined(NLS)
#include <limits.h>
#include <nl_types.h>
#endif
#include <errno.h>
#include <string.h>
#include <signal.h>
#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);
}