1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 2001 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <stdio.h>
25*a466cc55SCy Schubert #include <string.h>
26*a466cc55SCy Schubert
27*a466cc55SCy Schubert #include <isc/mutex.h>
28*a466cc55SCy Schubert #include <isc/once.h>
29*a466cc55SCy Schubert #include <isc/print.h>
30*a466cc55SCy Schubert #include <isc/strerror.h>
31*a466cc55SCy Schubert #include <isc/util.h>
32*a466cc55SCy Schubert
33*a466cc55SCy Schubert #include "l_stdlib.h" /* NTP local change */
34*a466cc55SCy Schubert
35*a466cc55SCy Schubert #ifdef HAVE_STRERROR
36*a466cc55SCy Schubert /*%
37*a466cc55SCy Schubert * We need to do this this way for profiled locks.
38*a466cc55SCy Schubert */
39*a466cc55SCy Schubert static isc_mutex_t isc_strerror_lock;
init_lock(void)40*a466cc55SCy Schubert static void init_lock(void) {
41*a466cc55SCy Schubert RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
42*a466cc55SCy Schubert }
43*a466cc55SCy Schubert #else
44*a466cc55SCy Schubert extern const char * const sys_errlist[];
45*a466cc55SCy Schubert extern const int sys_nerr;
46*a466cc55SCy Schubert #endif
47*a466cc55SCy Schubert
48*a466cc55SCy Schubert void
isc__strerror(int num,char * buf,size_t size)49*a466cc55SCy Schubert isc__strerror(int num, char *buf, size_t size) {
50*a466cc55SCy Schubert #ifdef HAVE_STRERROR
51*a466cc55SCy Schubert char *msg;
52*a466cc55SCy Schubert unsigned int unum = (unsigned int)num;
53*a466cc55SCy Schubert static isc_once_t once = ISC_ONCE_INIT;
54*a466cc55SCy Schubert
55*a466cc55SCy Schubert REQUIRE(buf != NULL);
56*a466cc55SCy Schubert
57*a466cc55SCy Schubert RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
58*a466cc55SCy Schubert
59*a466cc55SCy Schubert LOCK(&isc_strerror_lock);
60*a466cc55SCy Schubert msg = strerror(num);
61*a466cc55SCy Schubert if (msg != NULL)
62*a466cc55SCy Schubert snprintf(buf, size, "%s", msg);
63*a466cc55SCy Schubert else
64*a466cc55SCy Schubert snprintf(buf, size, "Unknown error: %u", unum);
65*a466cc55SCy Schubert UNLOCK(&isc_strerror_lock);
66*a466cc55SCy Schubert #else
67*a466cc55SCy Schubert unsigned int unum = (unsigned int)num;
68*a466cc55SCy Schubert
69*a466cc55SCy Schubert REQUIRE(buf != NULL);
70*a466cc55SCy Schubert
71*a466cc55SCy Schubert if (num >= 0 && num < sys_nerr)
72*a466cc55SCy Schubert snprintf(buf, size, "%s", sys_errlist[num]);
73*a466cc55SCy Schubert else
74*a466cc55SCy Schubert snprintf(buf, size, "Unknown error: %u", unum);
75*a466cc55SCy Schubert #endif
76*a466cc55SCy Schubert }
77