Loading...
string/FreeBSD/strerror.c Libc-1725.40.4 Libc-997.90.3
--- Libc/Libc-1725.40.4/string/FreeBSD/strerror.c
+++ Libc/Libc-997.90.3/string/FreeBSD/strerror.c
@@ -53,6 +53,7 @@
  */
 #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.
@@ -116,26 +117,31 @@
 	return (retval);
 }
 
-static char *__strerror_ebuf = NULL;
+__private_extern__ char *__strerror_ebuf = NULL;
+#else /* BUILDING_VARIANT */
+__private_extern__ void __errstr(int, char *, size_t);
+
+extern char *__strerror_ebuf;
+#endif /* !BUILDING_VARIANT */
 
 char *
 strerror(int num)
 {
-#if !defined(NLS)
-	if (num >= 0 && num < sys_nerr) {
-		return (char*)sys_errlist[num];
-	}
-#endif
+	// Dynamically allocate a big buffer to receive the text then shrink it
+	// down to the actual size needed.
+	size_t ebufsiz = NL_TEXTMAX;
 
 	if (__strerror_ebuf == NULL) {
-		__strerror_ebuf = calloc(1, NL_TEXTMAX);
+		__strerror_ebuf = calloc(1, ebufsiz);
 		if (__strerror_ebuf == NULL) {
 			return NULL;
 		}
 	}
-
-	if (strerror_r(num, __strerror_ebuf, NL_TEXTMAX) != 0) {
+	
+	if (strerror_r(num, __strerror_ebuf, ebufsiz) != 0) {
+#if !__DARWIN_UNIX03
 		errno = EINVAL;
+#endif
 	}
 	return __strerror_ebuf;
 }