1135446Strhodes/*
2193149Sdougb * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 2001  Internet Software Consortium.
4135446Strhodes *
5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18234010Sdougb/* $Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp $ */
19135446Strhodes
20170222Sdougb/*! \file */
21170222Sdougb
22135446Strhodes#include <config.h>
23135446Strhodes
24135446Strhodes#include <stdio.h>
25135446Strhodes#include <string.h>
26135446Strhodes
27135446Strhodes#include <isc/mutex.h>
28135446Strhodes#include <isc/once.h>
29135446Strhodes#include <isc/print.h>
30135446Strhodes#include <isc/strerror.h>
31135446Strhodes#include <isc/util.h>
32135446Strhodes
33135446Strhodes#ifdef HAVE_STRERROR
34170222Sdougb/*%
35135446Strhodes * We need to do this this way for profiled locks.
36135446Strhodes */
37135446Strhodesstatic isc_mutex_t isc_strerror_lock;
38135446Strhodesstatic void init_lock(void) {
39135446Strhodes	RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
40135446Strhodes}
41135446Strhodes#else
42135446Strhodesextern const char * const sys_errlist[];
43135446Strhodesextern const int sys_nerr;
44135446Strhodes#endif
45135446Strhodes
46135446Strhodesvoid
47135446Strhodesisc__strerror(int num, char *buf, size_t size) {
48135446Strhodes#ifdef HAVE_STRERROR
49135446Strhodes	char *msg;
50193149Sdougb	unsigned int unum = (unsigned int)num;
51135446Strhodes	static isc_once_t once = ISC_ONCE_INIT;
52135446Strhodes
53135446Strhodes	REQUIRE(buf != NULL);
54135446Strhodes
55135446Strhodes	RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
56135446Strhodes
57135446Strhodes	LOCK(&isc_strerror_lock);
58135446Strhodes	msg = strerror(num);
59135446Strhodes	if (msg != NULL)
60135446Strhodes		snprintf(buf, size, "%s", msg);
61135446Strhodes	else
62135446Strhodes		snprintf(buf, size, "Unknown error: %u", unum);
63135446Strhodes	UNLOCK(&isc_strerror_lock);
64135446Strhodes#else
65193149Sdougb	unsigned int unum = (unsigned int)num;
66135446Strhodes
67135446Strhodes	REQUIRE(buf != NULL);
68135446Strhodes
69135446Strhodes	if (num >= 0 && num < sys_nerr)
70135446Strhodes		snprintf(buf, size, "%s", sys_errlist[num]);
71135446Strhodes	else
72135446Strhodes		snprintf(buf, size, "Unknown error: %u", unum);
73135446Strhodes#endif
74135446Strhodes}
75