BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionLibC / blob / 03048a95d88cc7a78171393371f5c22a0250a014 / stdio / fopen.c

// Related

OrionLibC

Barry Importing existing Orion LibC 03048a9 (2 years, 2 months ago)
#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;
}