4619a4fa889b50890268da386e0a4754049dfc28
[reactos.git] / reactos / lib / sdk / crt / misc / assert.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <precomp.h>
3 #include <assert.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <signal.h>
7
8 static const char formatstr[] =
9 "Assertion failed!\n\n"
10 "Program: %s\n"
11 "File: %s\n"
12 "Line: %ld\n\n"
13 "Expression: %s\n"
14 "Press Retry to debug the application\n";
15
16
17 /*
18 * @implemented
19 */
20 void _assert(const char *exp, const char *file, unsigned line)
21 {
22 int (WINAPI *pMessageBoxA)(HWND, LPCTSTR, LPCTSTR, UINT);
23 HMODULE hmodUser32;
24 char achProgram[40];
25 char *pszBuffer;
26 size_t len;
27 int iResult;
28
29 /* Assertion failed at foo.c line 45: x<y */
30 fprintf(stderr, "Assertion failed at %s line %d: %s\n", file, line, exp);
31 FIXME("Assertion failed at %s line %d: %s\n", file, line, exp);
32
33 /* Get MessageBoxA function pointer */
34 hmodUser32 = LoadLibrary("user32.dll");
35 pMessageBoxA = (PVOID)GetProcAddress(hmodUser32, "MessageBoxA");
36 if (!pMessageBoxA)
37 {
38 abort();
39 }
40
41 /* Get the file name of the module */
42 len = GetModuleFileNameA(NULL, achProgram, 40);
43
44 /* Calculate full length of the message */
45 len += sizeof(formatstr) + len + strlen(exp) + strlen(file);
46
47 /* Allocate a buffer */
48 pszBuffer = malloc(len + 1);
49
50 /* Format a message */
51 _snprintf(pszBuffer, len, formatstr, achProgram, file, line, exp);
52
53 /* Display a message box */
54 iResult = pMessageBoxA(NULL,
55 pszBuffer,
56 "ReactOS C Runtime Library",
57 MB_ABORTRETRYIGNORE | MB_ICONERROR);
58
59 free(pszBuffer);
60
61 /* Does the user want to abort? */
62 if (iResult == IDABORT)
63 {
64 abort();
65 }
66
67 /* Does the user want to debug? */
68 if (iResult == IDRETRY)
69 {
70 DbgRaiseAssertionFailure();
71 }
72 }