1132451Sroberto/* 2132451Sroberto * Copyright (C) 2001 Internet Software Consortium. 3132451Sroberto * 4132451Sroberto * Permission to use, copy, modify, and distribute this software for any 5132451Sroberto * purpose with or without fee is hereby granted, provided that the above 6132451Sroberto * copyright notice and this permission notice appear in all copies. 7132451Sroberto * 8132451Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM 9132451Sroberto * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 10132451Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 11132451Sroberto * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, 12132451Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 13132451Sroberto * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 14132451Sroberto * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 15132451Sroberto * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16132451Sroberto */ 17132451Sroberto 18132451Sroberto/* $Id: strerror.c,v 1.3 2001/11/20 01:45:45 gson Exp $ */ 19132451Sroberto 20132451Sroberto#include <config.h> 21132451Sroberto 22132451Sroberto#include <stdio.h> 23132451Sroberto#include <string.h> 24132451Sroberto 25132451Sroberto#include <isc/mutex.h> 26132451Sroberto#include <isc/once.h> 27132451Sroberto#include <isc/print.h> 28132451Sroberto#include <isc/strerror.h> 29132451Sroberto#include <isc/util.h> 30132451Sroberto 31132451Sroberto#include "l_stdlib.h" 32132451Sroberto 33132451Sroberto#ifdef HAVE_STRERROR 34132451Sroberto/* 35132451Sroberto * We need to do this this way for profiled locks. 36132451Sroberto */ 37132451Srobertostatic isc_mutex_t isc_strerror_lock; 38132451Srobertostatic void init_lock(void) { 39132451Sroberto RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS); 40132451Sroberto} 41132451Sroberto#else 42132451Srobertoextern const char * const sys_errlist[]; 43132451Srobertoextern const int sys_nerr; 44132451Sroberto#endif 45132451Sroberto 46132451Srobertovoid 47132451Srobertoisc__strerror(int num, char *buf, size_t size) { 48132451Sroberto#ifdef HAVE_STRERROR 49132451Sroberto char *msg; 50132451Sroberto unsigned int unum = num; 51132451Sroberto static isc_once_t once = ISC_ONCE_INIT; 52132451Sroberto 53132451Sroberto REQUIRE(buf != NULL); 54132451Sroberto 55132451Sroberto RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS); 56132451Sroberto 57132451Sroberto LOCK(&isc_strerror_lock); 58132451Sroberto msg = strerror(num); 59132451Sroberto if (msg != NULL) 60132451Sroberto snprintf(buf, size, "%s", msg); 61132451Sroberto else 62132451Sroberto snprintf(buf, size, "Unknown error: %u", unum); 63132451Sroberto UNLOCK(&isc_strerror_lock); 64132451Sroberto#else 65132451Sroberto unsigned int unum = num; 66132451Sroberto 67132451Sroberto REQUIRE(buf != NULL); 68132451Sroberto 69132451Sroberto if (num >= 0 && num < sys_nerr) 70132451Sroberto snprintf(buf, size, "%s", sys_errlist[num]); 71132451Sroberto else 72132451Sroberto snprintf(buf, size, "Unknown error: %u", unum); 73132451Sroberto#endif 74132451Sroberto} 75