fxlibc/src/libc/stdio/fopen.c

25 lines
435 B
C

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "fileutil.h"
FILE *fopen(char const * restrict filename, char const * restrict mode)
{
FILE *fp = calloc(1, sizeof *fp);
if(!fp) goto err;
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;
}