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#ifdef HAVE_STRERROR
32132451Sroberto/*
33132451Sroberto * We need to do this this way for profiled locks.
34132451Sroberto */
35132451Srobertostatic isc_mutex_t isc_strerror_lock;
36132451Srobertostatic void init_lock(void) {
37132451Sroberto	RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
38132451Sroberto}
39132451Sroberto#else
40132451Srobertoextern const char * const sys_errlist[];
41132451Srobertoextern const int sys_nerr;
42132451Sroberto#endif
43132451Sroberto
44132451Srobertovoid
45132451Srobertoisc__strerror(int num, char *buf, size_t size) {
46132451Sroberto#ifdef HAVE_STRERROR
47132451Sroberto	char *msg;
48132451Sroberto	unsigned int unum = num;
49132451Sroberto	static isc_once_t once = ISC_ONCE_INIT;
50132451Sroberto
51132451Sroberto	REQUIRE(buf != NULL);
52132451Sroberto
53132451Sroberto	RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
54132451Sroberto
55132451Sroberto	LOCK(&isc_strerror_lock);
56132451Sroberto	msg = strerror(num);
57132451Sroberto	if (msg != NULL)
58132451Sroberto		snprintf(buf, size, "%s", msg);
59132451Sroberto	else
60132451Sroberto		snprintf(buf, size, "Unknown error: %u", unum);
61132451Sroberto	UNLOCK(&isc_strerror_lock);
62132451Sroberto#else
63132451Sroberto	unsigned int unum = num;
64132451Sroberto
65132451Sroberto	REQUIRE(buf != NULL);
66132451Sroberto
67132451Sroberto	if (num >= 0 && num < sys_nerr)
68132451Sroberto		snprintf(buf, size, "%s", sys_errlist[num]);
69132451Sroberto	else
70132451Sroberto		snprintf(buf, size, "Unknown error: %u", unum);
71132451Sroberto#endif
72132451Sroberto}
73