2019-06-09 12:32:53 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-06-03 01:57:25 +00:00
|
|
|
#include "io.h"
|
|
|
|
|
|
|
|
#ifdef unix
|
|
|
|
|
2019-06-12 10:22:12 +00:00
|
|
|
struct pang_io {
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t pang_write(struct pang_io *io, off_t offset, const void *buf, size_t count) {
|
2019-06-12 10:55:15 +00:00
|
|
|
return pwrite(io->fd, buf, count, offset);
|
2019-06-03 01:57:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 10:22:12 +00:00
|
|
|
ssize_t pang_read(struct pang_io *io, off_t offset, void *buf, size_t count) {
|
2019-06-12 10:55:15 +00:00
|
|
|
return pread(io->fd, buf, count, offset);
|
2019-06-03 01:57:25 +00:00
|
|
|
}
|
2019-06-09 12:32:53 +00:00
|
|
|
|
2019-06-12 10:22:12 +00:00
|
|
|
struct pang_io *pang_open(const char *pathname) {
|
2019-06-12 10:55:15 +00:00
|
|
|
int fd = open(pathname, O_RDWR | O_CREAT, 0777);
|
2019-06-12 10:22:12 +00:00
|
|
|
if (-1 == fd)
|
|
|
|
return NULL;
|
|
|
|
struct pang_io *io = malloc(sizeof(struct pang_io));
|
|
|
|
io->fd = fd;
|
|
|
|
return io;
|
2019-06-09 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 10:22:12 +00:00
|
|
|
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;
|
2019-06-09 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 01:57:25 +00:00
|
|
|
#endif
|