1 /*
2 Copyright (c) 1990 Regents of the University of California.
3 All rights reserved.
4 */
5 /*
6 FUNCTION
7 <<assert>>---macro for debugging diagnostics
8
9 INDEX
10 assert
11
12 SYNOPSIS
13 #include <assert.h>
14 void assert(int <[expression]>);
15
16 DESCRIPTION
17 Use this macro to embed debuggging diagnostic statements in
18 your programs. The argument <[expression]> should be an
19 expression which evaluates to true (nonzero) when your program
20 is working as you intended.
21
22 When <[expression]> evaluates to false (zero), <<assert>>
23 calls <<abort>>, after first printing a message showing what
24 failed and where:
25
26 . Assertion failed: <[expression]>, file <[filename]>, line <[lineno]>, function: <[func]>
27
28 If the name of the current function is not known (for example,
29 when using a C89 compiler that does not understand __func__),
30 the function location is omitted.
31
32 The macro is defined to permit you to turn off all uses of
33 <<assert>> at compile time by defining <<NDEBUG>> as a
34 preprocessor variable. If you do this, the <<assert>> macro
35 expands to
36
37 . (void(0))
38
39 RETURNS
40 <<assert>> does not return a value.
41
42 PORTABILITY
43 The <<assert>> macro is required by ANSI, as is the behavior
44 when <<NDEBUG>> is defined.
45
46 Supporting OS subroutines required (only if enabled): <<close>>, <<fstat>>,
47 <<getpid>>, <<isatty>>, <<kill>>, <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
48 */
49
50 #include <assert.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53
54 #ifndef _HAVE_ASSERT_FUNC
55 /* func can be NULL, in which case no function information is given. */
56 void
__assert_func(const char * file,int line,const char * func,const char * failedexpr)57 __assert_func (const char *file,
58 int line,
59 const char *func,
60 const char *failedexpr)
61 {
62 fprintf(stderr,
63 "assertion \"%s\" failed: file \"%s\", line %d%s%s\n",
64 failedexpr, file, line,
65 func ? ", function: " : "", func ? func : "");
66 abort();
67 /* NOTREACHED */
68 }
69 #endif /* _HAVE_ASSERT_FUNC */
70
71 void
__assert(const char * failedexpr,const char * file,int line)72 __assert (const char *failedexpr,
73 const char *file,
74 int line)
75 {
76 __assert_func (file, line, NULL, failedexpr);
77 /* NOTREACHED */
78 }
79