strerror.c revision dafcb997e390efa4423883dafd100c975c4095d6
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews * copyright notice and this permission notice appear in all copies.
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews *
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * PERFORMANCE OF THIS SOFTWARE.
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews */
b186f1ab91faf6d46d102ecbfd55cbdb7e24feeeMark Andrews
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews/* $Id: strerror.c,v 1.4 2004/03/05 05:11:47 marka Exp $ */
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#include <config.h>
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews
b186f1ab91faf6d46d102ecbfd55cbdb7e24feeeMark Andrews#include <stdio.h>
b186f1ab91faf6d46d102ecbfd55cbdb7e24feeeMark Andrews#include <string.h>
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence#include <isc/mutex.h>
#include <isc/once.h>
#include <isc/print.h>
#include <isc/strerror.h>
#include <isc/util.h>
#ifdef HAVE_STRERROR
/*
* We need to do this this way for profiled locks.
*/
static isc_mutex_t isc_strerror_lock;
static void init_lock(void) {
RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
}
#else
extern const char * const sys_errlist[];
extern const int sys_nerr;
#endif
void
isc__strerror(int num, char *buf, size_t size) {
#ifdef HAVE_STRERROR
char *msg;
unsigned int unum = num;
static isc_once_t once = ISC_ONCE_INIT;
REQUIRE(buf != NULL);
RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
LOCK(&isc_strerror_lock);
msg = strerror(num);
if (msg != NULL)
snprintf(buf, size, "%s", msg);
else
snprintf(buf, size, "Unknown error: %u", unum);
UNLOCK(&isc_strerror_lock);
#else
unsigned int unum = num;
REQUIRE(buf != NULL);
if (num >= 0 && num < sys_nerr)
snprintf(buf, size, "%s", sys_errlist[num]);
else
snprintf(buf, size, "Unknown error: %u", unum);
#endif
}