pang-o-lin/src/io.c

44 lines
920 B
C

#include <sys/types.h>
#include <unistd.h>
#include "io.h"
#ifdef unix
struct pang_io {
int fd;
};
ssize_t pang_write(struct pang_io *io, off_t offset, const void *buf, size_t count) {
if (-1 == lseek(io->fd, offset, SEEK_SET))
return -1;
return write(io->fd, buf, count);
}
ssize_t pang_read(struct pang_io *io, off_t offset, void *buf, size_t count) {
if (-1 == lseek(io->fd, offset, SEEK_SET))
return -1;
return read(io->fd, buf, count);
}
struct pang_io *pang_open(const char *pathname) {
int fd = open(pathname, O_RDWR | O_TRUNC | O_CREAT, 0777);
if (-1 == fd)
return NULL;
struct pang_io *io = malloc(sizeof(struct pang_io));
io->fd = fd;
return io;
}
int pang_close(struct pang_io **io) {
if (!io)
return -1;
if (!*io)
return -1;
int ret = close((*io)->fd);
free(*io);
*io = NULL;
return ret;
}
#endif