OrionLibC
Barry Importing existing Orion LibC 03048a9 (2 years, 2 months ago)diff --git a/stdio/fopen.c b/stdio/fopen.c new file mode 100644 index 0000000..69aecca --- /dev/null +++ b/stdio/fopen.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> + +/* Open a file */ +FILE * +fopen(const char *name, const char *mode) +{ + int fd; + FILE *fp; + + if (*mode != 'r' && *mode != 'w' && *mode != 'a') + return NULL; + + fp = malloc(sizeof(FILE)); + if (!fp) + return NULL; + + if (*mode == 'w') + fd = create(name, 0666); + else if (*mode == 'a') + fd = open(name, O_CREATE | O_WRONLY, 0); + else + fd = open(name, O_RDONLY, 0); + if (fd < 0) { + free(fp); + return NULL; + } + if (*mode == 'a') + lseek(fd, 0, SEEK_END); + + fp->fd = fd; + fp->count = 0; + fp->buf = NULL; + fp->flags = (*mode == 'r') ? FILE_READ : FILE_WRITE; + + return fp; +}