pang-o-lin/src/io.c

45 lines
796 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)
{
return pwrite(io->fd, buf, count, offset);
}
ssize_t pang_read(struct pang_io *io, off_t offset, void *buf, size_t count)
{
return pread(io->fd, buf, count, offset);
}
struct pang_io *pang_open(const char *pathname)
{
int fd = open(pathname, O_RDWR | 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