#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "../include/checkfds.h"

void checkfds(void)
{
    struct stat st;
    int fd;

    if (fstat(1, &st) == -1) {
        fd = open("/dev/null", O_WRONLY);
        if (fd == -1) exit(1);
        if (fd != 1) {
            if (dup2(fd, 1) == -1) exit(1);
            if (close(fd) == -1) exit(1);
        }
    }
    if (fstat(2, &st) == -1) {
        fd = open("/dev/null", O_WRONLY);
        if (fd == -1) exit(1);
        if (fd != 2) {
            if (dup2(fd, 2) == -1) exit(1);
            if (close(fd) == -1) exit(1);
        }
    }
}
