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