fxlibc/src/libc/stdio/freopen.c

27 lines
476 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "fileutil.h"
FILE *freopen(char const * restrict filename, char const * restrict mode,
FILE * restrict fp)
{
__fp_close(fp, false);
memset(fp, 0, sizeof *fp);
fp->fd = -1;
int flags = __fp_parse_mode(mode, fp);
if(flags < 0) goto err;
int fd = open(filename, flags, 0755);
if(fd < 0) goto err;
__fp_open(fp, fd, true);
return fp;
err:
__fp_close(fp, true);
return NULL;
}