X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/1f2f436a38f7ae2d39a943ad2898d8fed4ed2e58..a9aaacca3a68bb8d74fec09d8d8681a0efda2581:/string/FreeBSD/strerror.c diff --git a/string/FreeBSD/strerror.c b/string/FreeBSD/strerror.c index 7da9796..9689434 100644 --- a/string/FreeBSD/strerror.c +++ b/string/FreeBSD/strerror.c @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD: src/lib/libc/string/strerror.c,v 1.16 2007/01/09 00:28:12 im #include #include #include +#include #define UPREFIX "Unknown error" @@ -56,8 +57,8 @@ __FBSDID("$FreeBSD: src/lib/libc/string/strerror.c,v 1.16 2007/01/09 00:28:12 im * Doing this by hand instead of linking with stdio(3) avoids bloat for * statically linked binaries. */ -static void -errstr(int num, char *uprefix, char *buf, size_t len) +__private_extern__ void +__errstr(int num, char *uprefix, char *buf, size_t len) { char *t; unsigned int uerr; @@ -87,8 +88,8 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen) catd = catopen("libc", NL_CAT_LOCALE); #endif - if (errnum < 1 || errnum >= sys_nerr) { - errstr(errnum, + if (errnum < 0 || errnum >= sys_nerr) { + __errstr(errnum, #if defined(NLS) catgets(catd, 1, 0xffff, UPREFIX), #else @@ -115,12 +116,26 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen) return (retval); } +static char *__strerror_ebuf = NULL; + char * strerror(int num) { - static char ebuf[NL_TEXTMAX]; +#if !defined(NLS) + if (num >= 0 && num < sys_nerr) { + return (char*)sys_errlist[num]; + } +#endif - if (strerror_r(num, ebuf, sizeof(ebuf)) != 0) - errno = EINVAL; - return (ebuf); + if (__strerror_ebuf == NULL) { + __strerror_ebuf = calloc(1, NL_TEXTMAX); + if (__strerror_ebuf == NULL) { + return NULL; + } + } + + if (strerror_r(num, __strerror_ebuf, NL_TEXTMAX) != 0) { + errno = EINVAL; + } + return __strerror_ebuf; }