1258945Sroberto/* 2258945Sroberto * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 2001 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18280849Scy/* $Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp $ */ 19258945Sroberto 20258945Sroberto/*! \file */ 21258945Sroberto 22258945Sroberto#include <config.h> 23258945Sroberto 24258945Sroberto#include <stdio.h> 25258945Sroberto#include <string.h> 26258945Sroberto 27258945Sroberto#include <isc/mutex.h> 28258945Sroberto#include <isc/once.h> 29258945Sroberto#include <isc/print.h> 30258945Sroberto#include <isc/strerror.h> 31258945Sroberto#include <isc/util.h> 32258945Sroberto 33258945Sroberto#include "l_stdlib.h" /* NTP local change */ 34258945Sroberto 35258945Sroberto#ifdef HAVE_STRERROR 36258945Sroberto/*% 37258945Sroberto * We need to do this this way for profiled locks. 38258945Sroberto */ 39258945Srobertostatic isc_mutex_t isc_strerror_lock; 40258945Srobertostatic void init_lock(void) { 41258945Sroberto RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS); 42258945Sroberto} 43258945Sroberto#else 44258945Srobertoextern const char * const sys_errlist[]; 45258945Srobertoextern const int sys_nerr; 46258945Sroberto#endif 47258945Sroberto 48258945Srobertovoid 49258945Srobertoisc__strerror(int num, char *buf, size_t size) { 50258945Sroberto#ifdef HAVE_STRERROR 51258945Sroberto char *msg; 52258945Sroberto unsigned int unum = (unsigned int)num; 53258945Sroberto static isc_once_t once = ISC_ONCE_INIT; 54258945Sroberto 55258945Sroberto REQUIRE(buf != NULL); 56258945Sroberto 57258945Sroberto RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS); 58258945Sroberto 59258945Sroberto LOCK(&isc_strerror_lock); 60258945Sroberto msg = strerror(num); 61258945Sroberto if (msg != NULL) 62258945Sroberto snprintf(buf, size, "%s", msg); 63258945Sroberto else 64258945Sroberto snprintf(buf, size, "Unknown error: %u", unum); 65258945Sroberto UNLOCK(&isc_strerror_lock); 66258945Sroberto#else 67258945Sroberto unsigned int unum = (unsigned int)num; 68258945Sroberto 69258945Sroberto REQUIRE(buf != NULL); 70258945Sroberto 71258945Sroberto if (num >= 0 && num < sys_nerr) 72258945Sroberto snprintf(buf, size, "%s", sys_errlist[num]); 73258945Sroberto else 74258945Sroberto snprintf(buf, size, "Unknown error: %u", unum); 75258945Sroberto#endif 76258945Sroberto} 77