]> git.saurik.com Git - apple/libc.git/blobdiff - string/FreeBSD/strerror.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / string / FreeBSD / strerror.c
index 63e81019947f0f37873fedbbc145c34c20e50c4b..968943414e99f750202fd2fe7e14ec6dae1e1e6c 100644 (file)
@@ -41,6 +41,7 @@ __FBSDID("$FreeBSD: src/lib/libc/string/strerror.c,v 1.16 2007/01/09 00:28:12 im
 #include <errno.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #define        UPREFIX         "Unknown error"
 
@@ -52,7 +53,6 @@ __FBSDID("$FreeBSD: src/lib/libc/string/strerror.c,v 1.16 2007/01/09 00:28:12 im
  */
 #define        EBUFSIZE        (20 + 2 + sizeof(UPREFIX))
 
-#ifndef BUILDING_VARIANT
 /*
  * Doing this by hand instead of linking with stdio(3) avoids bloat for
  * statically linked binaries.
@@ -115,20 +115,27 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen)
 
        return (retval);
 }
-#else /* BUILDING_VARIANT */
-__private_extern__ void __errstr(int, char *, size_t);
-#endif /* !BUILDING_VARIANT */
+
+static char *__strerror_ebuf = NULL;
 
 char *
 strerror(int num)
 {
-       static char ebuf[NL_TEXTMAX];
-
-#if !__DARWIN_UNIX03
-       if (strerror_r(num, ebuf, sizeof(ebuf)) != 0)
-       errno = EINVAL;
-#else
-       (void)strerror_r(num, ebuf, sizeof(ebuf));
+#if !defined(NLS)
+       if (num >= 0 && num < sys_nerr) {
+               return (char*)sys_errlist[num];
+       }
 #endif
-       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;
 }