00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #include <stdio.h>
00025 #include <string.h>
00026
00027 #include <isc/mutex.h>
00028 #include <isc/once.h>
00029 #include <isc/print.h>
00030 #include <isc/strerror.h>
00031 #include <isc/util.h>
00032
00033 #ifdef HAVE_STRERROR
00034
00035
00036
00037 static isc_mutex_t isc_strerror_lock;
00038 static void init_lock(void) {
00039 RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
00040 }
00041 #else
00042 extern const char * const sys_errlist[];
00043 extern const int sys_nerr;
00044 #endif
00045
00046 void
00047 isc__strerror(int num, char *buf, size_t size) {
00048 #ifdef HAVE_STRERROR
00049 char *msg;
00050 unsigned int unum = (unsigned int)num;
00051 static isc_once_t once = ISC_ONCE_INIT;
00052
00053 REQUIRE(buf != NULL);
00054
00055 RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
00056
00057 LOCK(&isc_strerror_lock);
00058 msg = strerror(num);
00059 if (msg != NULL)
00060 snprintf(buf, size, "%s", msg);
00061 else
00062 snprintf(buf, size, "Unknown error: %u", unum);
00063 UNLOCK(&isc_strerror_lock);
00064 #else
00065 unsigned int unum = (unsigned int)num;
00066
00067 REQUIRE(buf != NULL);
00068
00069 if (num >= 0 && num < sys_nerr)
00070 snprintf(buf, size, "%s", sys_errlist[num]);
00071 else
00072 snprintf(buf, size, "Unknown error: %u", unum);
00073 #endif
00074 }