Back to index

glibc  2.9
assert-perr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994-1998,2001,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <assert.h>
00020 #include <libintl.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.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 an error number, a filename, and a line
00037    number, prints a message on the standard error stream of the form:
00038        a.c:10: foobar: Unexpected error: Computer bought the farm
00039    It then aborts program execution via a call to `abort'.  */
00040 
00041 #ifdef FATAL_PREPARE_INCLUDE
00042 # include FATAL_PREPARE_INCLUDE
00043 #endif
00044 
00045 void
00046 __assert_perror_fail (int errnum,
00047                     const char *file, unsigned int line,
00048                     const char *function)
00049 {
00050   char errbuf[1024];
00051   char *buf;
00052 
00053 #ifdef FATAL_PREPARE
00054   FATAL_PREPARE;
00055 #endif
00056 
00057   if (__asprintf (&buf, _("%s%s%s:%u: %s%sUnexpected error: %s.\n"),
00058                 __progname, __progname[0] ? ": " : "",
00059                 file, line,
00060                 function ? function : "", function ? ": " : "",
00061                 __strerror_r (errnum, errbuf, sizeof errbuf)) >= 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 appplication 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 libc_hidden_def (__assert_perror_fail)