Skip to content

Commit

Permalink
wutnewlib: implement abort, assert and assert_func
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Nov 20, 2023
1 parent 951ac01 commit 6f0659d
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions libraries/wutnewlib/wut_newlib.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
#include "wut_newlib.h"
#include <coreinit/exit.h>
#include <coreinit/debug.h>
#include <stdio.h>
#include <string.h>

void(*__wut_exit)(int rc);
extern void __fini_wut(void);

void abort(void) {
OSFatal("Abort called.\n");
/* NOTREACHED */
while (1);
}

void
__assert_func(const char *file,
int line,
const char *func,
const char *failedexpr)
{
char tmp[512] = {};
char buffer[512] = {};

snprintf(tmp, sizeof(tmp), "assertion \"%s\" failed:\n file \"%s\", line %d%s%s",
failedexpr, file, line,
func ? ", function: " : "", func ? func : "");

// make sure to add a \n every 64 characters to fit on the DRC screen.
char *target_ptr = buffer;
int i = 0, j = 0, lineLength = 0;
while (tmp[i] != '\0' && j < sizeof(buffer) - 1) {
if (tmp[i] == '\n') {
lineLength = 0;
} else if (lineLength >= 64) {
target_ptr[j++] = '\n';
lineLength = 0;
}
target_ptr[j++] = tmp[i++];
lineLength++;
}

OSFatal(buffer);
abort();
/* NOTREACHED */
}

void
__assert(const char *file,
int line,
const char *failedexpr)
{
__assert_func(file, line, NULL, failedexpr);
/* NOTREACHED */
}

void *_sbrk_r(struct _reent *ptr, ptrdiff_t incr) {
return __wut_sbrk_r(ptr, incr);
}
Expand Down

0 comments on commit 6f0659d

Please sign in to comment.