Expose the portable "strerror_r" used by jniThrowIOException.
Also add one example caller I used to test this, and update the
libnativehelper README file.
diff --git a/libnativehelper/JNIHelp.c b/libnativehelper/JNIHelp.c
index aacecb6..fa6b532 100644
--- a/libnativehelper/JNIHelp.c
+++ b/libnativehelper/JNIHelp.c
@@ -59,30 +59,30 @@
*/
int jniThrowIOException(JNIEnv* env, int errnum)
{
- // note: glibc has a nonstandard
- // strerror_r that looks like this:
- // char *strerror_r(int errnum, char *buf, size_t n);
-
- const char* message;
char buffer[80];
- char* ret;
-
- buffer[0] = 0;
- ret = (char*) strerror_r(errnum, buffer, sizeof(buffer));
-
- if (((int)ret) == 0) {
- //POSIX strerror_r, success
- message = buffer;
- } else if (((int)ret) == -1) {
- //POSIX strerror_r, failure
-
- snprintf (buffer, sizeof(buffer), "errno %d", errnum);
- message = buffer;
- } else {
- //glibc strerror_r returning a string
- message = ret;
- }
-
+ const char* message = jniStrError(errnum, buffer, sizeof(buffer));
return jniThrowException(env, "java/io/IOException", message);
}
+const char* jniStrError(int errnum, char* buf, size_t buflen)
+{
+ // note: glibc has a nonstandard strerror_r that returns char* rather
+ // than POSIX's int.
+ // char *strerror_r(int errnum, char *buf, size_t n);
+ char* ret = (char*) strerror_r(errnum, buf, buflen);
+ if (((int)ret) == 0) {
+ //POSIX strerror_r, success
+ return buf;
+ } else if (((int)ret) == -1) {
+ //POSIX strerror_r, failure
+ // (Strictly, POSIX only guarantees a value other than 0. The safest
+ // way to implement this function is to use C++ and overload on the
+ // type of strerror_r to accurately distinguish GNU from POSIX. But
+ // realistic implementations will always return -1.)
+ snprintf(buf, buflen, "errno %d", errnum);
+ return buf;
+ } else {
+ //glibc strerror_r returning a string
+ return ret;
+ }
+}