Changeset View
Changeset View
Standalone View
Standalone View
head/stand/liblua/lstd.c
Show All 29 Lines | |||||
#include "lstd.h" | #include "lstd.h" | ||||
#include "math.h" | #include "math.h" | ||||
FILE * | FILE * | ||||
fopen(const char *filename, const char *mode) | fopen(const char *filename, const char *mode) | ||||
{ | { | ||||
struct stat st; | struct stat st; | ||||
int fd; | int fd, m, o; | ||||
FILE *f; | FILE *f; | ||||
if (mode == NULL || mode[0] != 'r') | if (mode == NULL) | ||||
return NULL; | return NULL; | ||||
fd = open(filename, O_RDONLY); | switch (*mode++) { | ||||
case 'r': /* open for reading */ | |||||
m = O_RDONLY; | |||||
o = 0; | |||||
break; | |||||
case 'w': /* open for writing */ | |||||
m = O_WRONLY; | |||||
/* These are not actually implemented yet */ | |||||
o = O_CREAT | O_TRUNC; | |||||
break; | |||||
default: /* illegal mode */ | |||||
return (NULL); | |||||
} | |||||
if (*mode == '+') | |||||
m = O_RDWR; | |||||
fd = open(filename, m | o); | |||||
if (fd < 0) | if (fd < 0) | ||||
return NULL; | return NULL; | ||||
f = malloc(sizeof(FILE)); | f = malloc(sizeof(FILE)); | ||||
if (f == NULL) { | if (f == NULL) { | ||||
close(fd); | close(fd); | ||||
return NULL; | return NULL; | ||||
} | } | ||||
Show All 25 Lines | fread(void *ptr, size_t size, size_t count, FILE *stream) | ||||
size_t r; | size_t r; | ||||
if (stream == NULL) | if (stream == NULL) | ||||
return 0; | return 0; | ||||
r = (size_t)read(stream->fd, ptr, size * count); | r = (size_t)read(stream->fd, ptr, size * count); | ||||
stream->offset += r; | stream->offset += r; | ||||
return (r); | return (r); | ||||
} | |||||
size_t | |||||
fwrite(const void *ptr, size_t size, size_t count, FILE *stream) | |||||
{ | |||||
ssize_t w; | |||||
if (stream == NULL || ptr == NULL) | |||||
return (0); | |||||
w = write(stream->fd, ptr, size * count); | |||||
if (w == -1) | |||||
return (0); | |||||
stream->offset += w; | |||||
return ((size_t)w); | |||||
} | } | ||||
int | int | ||||
fclose(FILE *stream) | fclose(FILE *stream) | ||||
{ | { | ||||
if (stream == NULL) | if (stream == NULL) | ||||
return EOF; | return EOF; | ||||
close(stream->fd); | close(stream->fd); | ||||
▲ Show 20 Lines • Show All 125 Lines • Show Last 20 Lines |