17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib * Copyright (C) 1997, 1999, 2001 Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
5*9525b14bSRao Shoaib * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include <errno.h>
257c478bd9Sstevel@tonic-gate #include <stdio.h>
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include "port_after.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * Forward.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate static void default_assertion_failed(const char *, int, assertion_type,
387c478bd9Sstevel@tonic-gate const char *, int);
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * Public.
427c478bd9Sstevel@tonic-gate */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate assertion_failure_callback __assertion_failed = default_assertion_failed;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate void
set_assertion_failure_callback(assertion_failure_callback f)477c478bd9Sstevel@tonic-gate set_assertion_failure_callback(assertion_failure_callback f) {
487c478bd9Sstevel@tonic-gate if (f == NULL)
497c478bd9Sstevel@tonic-gate __assertion_failed = default_assertion_failed;
507c478bd9Sstevel@tonic-gate else
517c478bd9Sstevel@tonic-gate __assertion_failed = f;
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate const char *
assertion_type_to_text(assertion_type type)557c478bd9Sstevel@tonic-gate assertion_type_to_text(assertion_type type) {
567c478bd9Sstevel@tonic-gate const char *result;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate switch (type) {
597c478bd9Sstevel@tonic-gate case assert_require:
607c478bd9Sstevel@tonic-gate result = "REQUIRE";
617c478bd9Sstevel@tonic-gate break;
627c478bd9Sstevel@tonic-gate case assert_ensure:
637c478bd9Sstevel@tonic-gate result = "ENSURE";
647c478bd9Sstevel@tonic-gate break;
657c478bd9Sstevel@tonic-gate case assert_insist:
667c478bd9Sstevel@tonic-gate result = "INSIST";
677c478bd9Sstevel@tonic-gate break;
687c478bd9Sstevel@tonic-gate case assert_invariant:
697c478bd9Sstevel@tonic-gate result = "INVARIANT";
707c478bd9Sstevel@tonic-gate break;
717c478bd9Sstevel@tonic-gate default:
727c478bd9Sstevel@tonic-gate result = NULL;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate return (result);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Private.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate
81*9525b14bSRao Shoaib /* coverity[+kill] */
827c478bd9Sstevel@tonic-gate static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)837c478bd9Sstevel@tonic-gate default_assertion_failed(const char *file, int line, assertion_type type,
847c478bd9Sstevel@tonic-gate const char *cond, int print_errno)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
877c478bd9Sstevel@tonic-gate file, line, assertion_type_to_text(type), cond,
887c478bd9Sstevel@tonic-gate (print_errno) ? ": " : "",
897c478bd9Sstevel@tonic-gate (print_errno) ? strerror(errno) : "");
907c478bd9Sstevel@tonic-gate abort();
917c478bd9Sstevel@tonic-gate /* NOTREACHED */
927c478bd9Sstevel@tonic-gate }
93*9525b14bSRao Shoaib
94*9525b14bSRao Shoaib /*! \file */
95