X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=visor%2Fsrc%2Fmain_unix.c;fp=visor%2Fsrc%2Fmain_unix.c;h=96d59cb7a09e85c596ccd4877208f71bdc404a10;hp=64302728b3d6b5c5da102ce0bf0031641f2899e3;hb=94c867869ece77f27cf3c8b637d44e13d86f5b88;hpb=7357e1f153279a9d84d8671f02fa146575d4935e diff --git a/visor/src/main_unix.c b/visor/src/main_unix.c index 6430272..96d59cb 100644 --- a/visor/src/main_unix.c +++ b/visor/src/main_unix.c @@ -1,10 +1,14 @@ #include #include +#include +#include +#include +#include #include "term.h" #include "visor.h" struct file { - FILE *fp; + int fd; void *maddr; size_t msize; }; @@ -101,7 +105,10 @@ static int init(void) vi_set_fileops(vi, &fops); for(i=0; ifp = fopen(path, attr))) { + if((file->fd = open(path, flags)) == -1) { free(file); return 0; } - return file; + return (vi_file*)file; } -static void file_close(vi_file *file) +static void file_close(vi_file *vif) { + struct file *file = vif; + if(!file) return; + + if(file->fd >= 0) { + if(file->maddr) { + file_unmap(file); + } + close(file->fd); + } + free(file); } -static long file_size(vi_file *file) +static long file_size(vi_file *vif) { - return -1; + struct file *file = vif; + struct stat st; + + if(fstat(file->fd, &st) == -1) { + return -1; + } + return st.st_size; } -static void *file_map(vi_file *file) +static void *file_map(vi_file *vif) { - return 0; + struct file *file = vif; + long sz; + + if((sz = file_size(file)) == -1) { + return 0; + } + if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) { + return 0; + } + file->msize = sz; + return file->maddr; } -static void file_unmap(vi_file *file) +static void file_unmap(vi_file *vif) { + struct file *file = vif; + if(file->maddr) { + munmap(file->maddr, file->msize); + } + file->maddr = 0; } -static long file_read(vi_file *file, void *buf, long count) +static long file_read(vi_file *vif, void *buf, long count) { - return -1; + struct file *file = vif; + return read(file->fd, buf, count); } -static long file_write(vi_file *file, void *buf, long count) +static long file_write(vi_file *vif, void *buf, long count) { - return -1; + struct file *file = vif; + return write(file->fd, buf, count); } -static long file_seek(vi_file *file, long offs, int whence) +static long file_seek(vi_file *vif, long offs, int whence) { - return -1; + struct file *file = vif; + return lseek(file->fd, offs, whence); }