Back to index

glibc  2.9
assert.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1994-1996,1998,2001,2002,2005
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <libintl.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <sysdep.h>
00025 #include <unistd.h>
00026 
00027 
00028 extern const char *__progname;
00029 
00030 #ifdef USE_IN_LIBIO
00031 # include <wchar.h>
00032 # include <libio/iolibio.h>
00033 # define fflush(s) INTUSE(_IO_fflush) (s)
00034 #endif
00035 
00036 /* This function, when passed a string containing an asserted
00037    expression, a filename, and a line number, prints a message
00038    on the standard error stream of the form:
00039        a.c:10: foobar: Assertion `a == b' failed.
00040    It then aborts program execution via a call to `abort'.  */
00041 
00042 #ifdef FATAL_PREPARE_INCLUDE
00043 # include FATAL_PREPARE_INCLUDE
00044 #endif
00045 
00046 #undef __assert_fail
00047 void
00048 __assert_fail (const char *assertion, const char *file, unsigned int line,
00049               const char *function)
00050 {
00051   char *buf;
00052 
00053 #ifdef FATAL_PREPARE
00054   FATAL_PREPARE;
00055 #endif
00056 
00057   if (__asprintf (&buf, _("%s%s%s:%u: %s%sAssertion `%s' failed.\n"),
00058                 __progname, __progname[0] ? ": " : "",
00059                 file, line,
00060                 function ? function : "", function ? ": " : "",
00061                 assertion) >= 0)
00062     {
00063       /* Print the message.  */
00064       (void) __fxprintf (NULL, "%s", buf);
00065       (void) fflush (stderr);
00066 
00067       /* We have to free the buffer since the application might catch the
00068         SIGABRT.  */
00069       free (buf);
00070     }
00071   else
00072     {
00073       /* At least print a minimal message.  */
00074       static const char errstr[] = "Unexpected error.\n";
00075       __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
00076     }
00077 
00078   abort ();
00079 }
00080 hidden_def(__assert_fail)