strerror.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp $ */
00019 
00020 /*! \file */
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  * We need to do this this way for profiled locks.
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 }

Generated on Tue Apr 28 17:41:06 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha