xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/assertions.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1997,1999 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #if !defined(LINT) && !defined(CODECENTER)
26 static const char rcsid[] = "$Id: assertions.c,v 8.4 2001/05/29 05:49:22 marka Exp $";
27 #endif
28 
29 #include "port_before.h"
30 
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <isc/assertions.h>
37 
38 #include "port_after.h"
39 
40 /*
41  * Forward.
42  */
43 
44 static void default_assertion_failed(const char *, int, assertion_type,
45 				     const char *, int);
46 
47 /*
48  * Public.
49  */
50 
51 assertion_failure_callback __assertion_failed = default_assertion_failed;
52 
53 void
54 set_assertion_failure_callback(assertion_failure_callback f) {
55 	if (f == NULL)
56 		__assertion_failed = default_assertion_failed;
57 	else
58 		__assertion_failed = f;
59 }
60 
61 const char *
62 assertion_type_to_text(assertion_type type) {
63 	const char *result;
64 
65 	switch (type) {
66 	case assert_require:
67 		result = "REQUIRE";
68 		break;
69 	case assert_ensure:
70 		result = "ENSURE";
71 		break;
72 	case assert_insist:
73 		result = "INSIST";
74 		break;
75 	case assert_invariant:
76 		result = "INVARIANT";
77 		break;
78 	default:
79 		result = NULL;
80 	}
81 	return (result);
82 }
83 
84 /*
85  * Private.
86  */
87 
88 static void
89 default_assertion_failed(const char *file, int line, assertion_type type,
90 			 const char *cond, int print_errno)
91 {
92 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
93 		file, line, assertion_type_to_text(type), cond,
94 		(print_errno) ? ": " : "",
95 		(print_errno) ? strerror(errno) : "");
96 	abort();
97 	/* NOTREACHED */
98 }
99