#include <system_private.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

int system_init_os(void)
{
    /* Windows GUI applications do not print stderr or stdout anywhere.
     * Therefore, it makes sense to redirect them to plaintext files. */
    if (!(freopen("stderr.txt", "wb", stderr)))
    {
        fprintf(stderr, "%s: freopen(3) stderr failed: %s\n",
            __func__, strerror(errno));
        return -1;
    }
    else if (setvbuf(stderr, NULL, _IONBF, 0))
    {
        fprintf(stderr, "%s: setvbuf(3) stderr failed: %s\n",
            __func__, strerror(errno));
        return -1;
    }
    else if (!(freopen("stdout.txt", "wb", stdout)))
    {
        fprintf(stderr, "%s: freopen(3) stdout failed: %s\n",
            __func__, strerror(errno));
        return -1;
    }
    else if (setvbuf(stdout, NULL, _IONBF, 0))
    {
        fprintf(stderr, "%s: setvbuf(3) stdout failed: %s\n",
            __func__, strerror(errno));
        return -1;
    }

    return 0;
}
