xref: /minix3/external/bsd/bind/dist/lib/isc/unix/strerror.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: strerror.c,v 1.4 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: strerror.c,v 1.10 2009/02/16 23:48:04 tbox Exp  */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <stdio.h>
27 #include <string.h>
28 
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>
34 
35 #ifdef HAVE_STRERROR
36 /*%
37  * We need to do this this way for profiled locks.
38  */
39 static isc_mutex_t isc_strerror_lock;
init_lock(void)40 static void init_lock(void) {
41 	RUNTIME_CHECK(isc_mutex_init(&isc_strerror_lock) == ISC_R_SUCCESS);
42 }
43 #else
44 extern const char * const sys_errlist[];
45 extern const int sys_nerr;
46 #endif
47 
48 void
isc__strerror(int num,char * buf,size_t size)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;
54 
55 	REQUIRE(buf != NULL);
56 
57 	RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
58 
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;
68 
69 	REQUIRE(buf != NULL);
70 
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
76 }
77