#include #include #include #include /* 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; }