1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7#include <assert.h>
8#include <fcntl.h>
9#include <io.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13/* This is import symbol name for "_assert" from CRT DLL library */
14extern void (__cdecl *__MINGW_IMP_SYMBOL(__msvcrt_assert))(const char *message, const char *file, unsigned line);
15
16/* Turn off _O_WTEXT, _O_U16TEXT or _O_U8TEXT mode on stderr stream
17 * by changing mode to _O_TEXT, because fprintf (called by __msvcrt_assert)
18 * does not work (and does nothing) on FILE* stream in some of those modes.
19 * Only fwprintf works with those modes, but _assert uses fprintf.
20 * Before changing the FILE* stream mode, it is required to flush buffers. */
21void __cdecl _assert(const char *message, const char *file, unsigned line)
22{
23 /* stderr expands to function call */
24 FILE *stream = stderr;
25 /* Cache fd used by `stderr` */
26 int fd = _fileno (stream);
27 /* We need to restore previous mode in case `_assert` returns; it can happen
28 * if program has called _set_error_mode(_OUT_TO_MSGBOX) and user pressed
29 * "Ignore" button in popped up message box. */
30 int oldmode;
31
32 /* Change `stderr` mode to `_O_TEXT` */
33 fflush(stream);
34 oldmode = _setmode(fd, _O_TEXT);
35
36 /* Call CRT `_assert` */
37 __MINGW_IMP_SYMBOL(__msvcrt_assert)(message, file, line);
38
39 /* Restore `stderr` mode to `oldmode` */
40 fflush (stream);
41 if (_setmode (fd, oldmode) != _O_TEXT) {
42 abort ();
43 }
44}
45
46void (__cdecl *__MINGW_IMP_SYMBOL(_assert))(const char *message, const char *file, unsigned line) = _assert;