xref: /netbsd-src/lib/libc/isc/assertions.c (revision 67f518f4961c7998344cd1aa9dc77d264d7bb5b7)
1*67f518f4Sjoerg /*	$NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $	*/
2330989eeSchristos 
3330989eeSchristos /*
459a755a4Schristos  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
559a755a4Schristos  * Copyright (C) 1997, 1999, 2001  Internet Software Consortium.
6330989eeSchristos  *
759a755a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8330989eeSchristos  * purpose with or without fee is hereby granted, provided that the above
9330989eeSchristos  * copyright notice and this permission notice appear in all copies.
10330989eeSchristos  *
1159a755a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
1259a755a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1359a755a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1459a755a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1559a755a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
1659a755a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1759a755a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18330989eeSchristos  */
19330989eeSchristos 
2049a363f1Schristos #include <sys/cdefs.h>
2149a363f1Schristos #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
2249a363f1Schristos #ifdef notdef
2359a755a4Schristos static const char rcsid[] = "Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp";
2449a363f1Schristos #else
25*67f518f4Sjoerg __RCSID("$NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $");
2649a363f1Schristos #endif
27330989eeSchristos #endif
28330989eeSchristos 
29330989eeSchristos #include "port_before.h"
30330989eeSchristos 
31330989eeSchristos #include <errno.h>
32330989eeSchristos #include <stdio.h>
33330989eeSchristos #include <stdlib.h>
34330989eeSchristos #include <string.h>
35330989eeSchristos 
36330989eeSchristos #include <isc/assertions.h>
37330989eeSchristos 
38330989eeSchristos #include "port_after.h"
39330989eeSchristos 
40330989eeSchristos /*
41330989eeSchristos  * Forward.
42330989eeSchristos  */
43330989eeSchristos 
44*67f518f4Sjoerg __dead static void default_assertion_failed(const char *, int, assertion_type,
45330989eeSchristos 				     const char *, int);
46330989eeSchristos 
47330989eeSchristos /*
48330989eeSchristos  * Public.
49330989eeSchristos  */
50330989eeSchristos 
51330989eeSchristos assertion_failure_callback __assertion_failed = default_assertion_failed;
52330989eeSchristos 
53330989eeSchristos void
set_assertion_failure_callback(assertion_failure_callback f)54330989eeSchristos set_assertion_failure_callback(assertion_failure_callback f) {
55330989eeSchristos 	if (f == NULL)
56330989eeSchristos 		__assertion_failed = default_assertion_failed;
57330989eeSchristos 	else
58330989eeSchristos 		__assertion_failed = f;
59330989eeSchristos }
60330989eeSchristos 
61330989eeSchristos const char *
assertion_type_to_text(assertion_type type)62330989eeSchristos assertion_type_to_text(assertion_type type) {
63330989eeSchristos 	const char *result;
64330989eeSchristos 
65330989eeSchristos 	switch (type) {
66330989eeSchristos 	case assert_require:
67330989eeSchristos 		result = "REQUIRE";
68330989eeSchristos 		break;
69330989eeSchristos 	case assert_ensure:
70330989eeSchristos 		result = "ENSURE";
71330989eeSchristos 		break;
72330989eeSchristos 	case assert_insist:
73330989eeSchristos 		result = "INSIST";
74330989eeSchristos 		break;
75330989eeSchristos 	case assert_invariant:
76330989eeSchristos 		result = "INVARIANT";
77330989eeSchristos 		break;
78330989eeSchristos 	default:
79330989eeSchristos 		result = NULL;
80330989eeSchristos 	}
81330989eeSchristos 	return (result);
82330989eeSchristos }
83330989eeSchristos 
84330989eeSchristos /*
85330989eeSchristos  * Private.
86330989eeSchristos  */
87330989eeSchristos 
8859a755a4Schristos /* coverity[+kill] */
89330989eeSchristos static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)90330989eeSchristos default_assertion_failed(const char *file, int line, assertion_type type,
91330989eeSchristos 			 const char *cond, int print_errno)
92330989eeSchristos {
93330989eeSchristos 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
94330989eeSchristos 		file, line, assertion_type_to_text(type), cond,
95330989eeSchristos 		(print_errno) ? ": " : "",
96330989eeSchristos 		(print_errno) ? strerror(errno) : "");
97330989eeSchristos 	abort();
98330989eeSchristos 	/* NOTREACHED */
99330989eeSchristos }
100d73eb73dSchristos 
101d73eb73dSchristos /*! \file */
102