__assert.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * An __assert() function compatible with the modified glibc assert.h
  3. * that is used by uClibc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free
  17. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Oct 28, 2002
  20. *
  21. * ANSI/ISO C99 requires assert() to write to stderr. This means that
  22. * writing to STDERR_FILENO is insufficient, as the user could freopen
  23. * stderr. It is also insufficient to output to fileno(stderr) since
  24. * this would fail in the custom stream case. I didn't remove the
  25. * old code though, as it doesn't use stdio stream functionality
  26. * and is useful in debugging the stdio code.
  27. */
  28. #define _STDIO_UTILITY /* For _stdio_fdout and _int10tostr. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. /* Get the prototype from assert.h as a double-check. */
  33. #undef NDEBUG
  34. #include <assert.h>
  35. #undef assert
  36. #define ASSERT_SHOW_PROGNAME 1
  37. #ifdef ASSERT_SHOW_PROGNAME
  38. extern const char *__progname;
  39. #endif
  40. #if 1
  41. static int in_assert; /* bss inits to 0. */
  42. void __assert(const char *assertion, const char * filename,
  43. int linenumber, register const char * function)
  44. {
  45. if (!in_assert) {
  46. in_assert = 1;
  47. fprintf(stderr,
  48. #ifdef ASSERT_SHOW_PROGNAME
  49. "%s: %s: %d: %s: Assertion `%s' failed.\n", __progname,
  50. #else
  51. "%s: %d: %s: Assertion `%s' failed.\n",
  52. #endif
  53. filename,
  54. linenumber,
  55. /* Function name isn't available with some compilers. */
  56. ((function == NULL) ? "?function?" : function),
  57. assertion
  58. );
  59. }
  60. abort();
  61. }
  62. #else
  63. void __assert(const char *assertion, const char * filename,
  64. int linenumber, register const char * function)
  65. {
  66. char buf[__BUFLEN_INT10TOSTR];
  67. _stdio_fdout(STDERR_FILENO,
  68. #ifdef ASSERT_SHOW_PROGNAME
  69. __progname,
  70. ": ",
  71. #endif
  72. filename,
  73. ":",
  74. _int10tostr(buf+sizeof(buf)-1, linenumber),
  75. ": ",
  76. /* Function name isn't available with some compilers. */
  77. ((function == NULL) ? "?function?" : function),
  78. ": Assertion `",
  79. assertion,
  80. "' failed.\n",
  81. NULL
  82. );
  83. abort();
  84. }
  85. #endif