fix file IO: void* instead of char*
[libjh.git] / io.c
diff --git a/io.c b/io.c
index d3b9a6d..d2b3197 100644 (file)
--- a/io.c
+++ b/io.c
@@ -1,3 +1,6 @@
+// Copyright (2013) Jann Horn <jann@thejh.net>
+// This code is licensed under the AGPLv3.
+
 HEADER #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
 HEADER #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
@@ -6,6 +9,8 @@ HEADER #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
+#include <assert.h>
+#include <sys/mman.h>
 
 // Wrapper for `read` that retries on partial reads.
 // If last_res is non-NULL, it will be filled with the
 
 // Wrapper for `read` that retries on partial reads.
 // If last_res is non-NULL, it will be filled with the
@@ -61,7 +66,8 @@ PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
 // this and can specify len_out as NULL.
 PUBLIC_CONST JH_SLURP_NO_STAT 1
 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
 // this and can specify len_out as NULL.
 PUBLIC_CONST JH_SLURP_NO_STAT 1
 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
-PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
+PUBLIC_CONST JH_SLURP_8BYTE_PAD 4 /* pad buffer with eight nullbytes, not one */
+PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) {
   int errno_;
   
   // Let's just guess that the file is 1023 bytes. Will become 1024 with
   int errno_;
   
   // Let's just guess that the file is 1023 bytes. Will become 1024 with
@@ -69,6 +75,8 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
   size_t size_guess = 1023;
   bool trusted_guess = false; /* can we rely on the guess? */
   
   size_t size_guess = 1023;
   bool trusted_guess = false; /* can we rely on the guess? */
   
+  int padlen = (flags&JH_SLURP_8BYTE_PAD)?8:1;
+  
   // If we can determine the exact size, we don't have to guess. So try
   // to determine the exact size.
   if (!(flags&JH_SLURP_NO_STAT)) {
   // If we can determine the exact size, we don't have to guess. So try
   // to determine the exact size.
   if (!(flags&JH_SLURP_NO_STAT)) {
@@ -85,7 +93,7 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
   int done = 0;
   
   while (1) {
   int done = 0;
   
   while (1) {
-    buf = realloc(buf, size_guess+1);
+    buf = realloc(buf, size_guess+padlen);
     if (buf == NULL) return NULL;
     int last_res;
     ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
     if (buf == NULL) return NULL;
     int last_res;
     ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
@@ -93,12 +101,16 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
     done += read_res;
     if (last_res == 0 || trusted_guess) {
       // out
     done += read_res;
     if (last_res == 0 || trusted_guess) {
       // out
-      buf[done] = '\0';
+      if (padlen == 1) {
+        buf[done] = '\0';
+      } else {
+        *(uint64_t*)(buf+done) = 0;
+      }
       if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
         // Well, it'd be weird if shrinking could fail... but meh, I can't find
         // an explicit statement about this being disallowed, so try staying on
         // the safe side.
       if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
         // Well, it'd be weird if shrinking could fail... but meh, I can't find
         // an explicit statement about this being disallowed, so try staying on
         // the safe side.
-        char *buf_ = realloc(buf, done+1);
+        char *buf_ = realloc(buf, done+padlen);
         if (buf_) buf = buf_;
       }
       if (len_out) *len_out = done;
         if (buf_) buf = buf_;
       }
       if (len_out) *len_out = done;
@@ -108,8 +120,8 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
   }
 }
 
   }
 }
 
-PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
-  int fd = open(path, O_RDONLY);
+PUBLIC_FN void *slurp_file(char *path, size_t *len_out, int flags) {
+  int fd = open(path, O_RDONLY|O_CLOEXEC);
   if (fd == -1) return NULL;
   char *res = slurp_fd(fd, len_out, flags);
   int errno_ = errno;
   if (fd == -1) return NULL;
   char *res = slurp_fd(fd, len_out, flags);
   int errno_ = errno;
@@ -120,10 +132,10 @@ PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
 
 // Write data into a file. len can be -1; that means that
 // the real length is strlen(buf).
 
 // Write data into a file. len can be -1; that means that
 // the real length is strlen(buf).
-PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
+PUBLIC_FN int write_file(char *path, void *buf, ssize_t len, int open_flags) {
   if (len == -1) len = strlen(buf);
   
   if (len == -1) len = strlen(buf);
   
-  int fd = open(path, open_flags, 0777);
+  int fd = open(path, open_flags|O_CLOEXEC|O_WRONLY, 0777);
   if (fd == -1) return 1;
   ssize_t write_res = write_nointr(fd, buf, len, NULL);
   int write_errno = errno;
   if (fd == -1) return 1;
   ssize_t write_res = write_nointr(fd, buf, len, NULL);
   int write_errno = errno;
@@ -135,3 +147,19 @@ PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
   if (close_res) return 1;
   return 0;
 }
   if (close_res) return 1;
   return 0;
 }
+
+PUBLIC_FN void *mmap_file(void *addr, size_t length, int prot, int flags, char *path, off_t offset) {
+  assert((flags&MAP_ANONYMOUS) == 0);
+  assert(prot&(PROT_EXEC|PROT_READ|PROT_WRITE));
+  int open_flags = O_CLOEXEC;
+  if ((prot & (PROT_READ|PROT_EXEC)) && !(prot&PROT_WRITE)) open_flags |= O_RDONLY;
+  else if ((prot & (PROT_READ|PROT_EXEC))) open_flags |= O_RDWR;
+  else open_flags |= O_WRONLY;
+  int fd = open(path, open_flags);
+  if (fd == -1) return NULL;
+  void *r = mmap(addr, length, prot, flags, fd, offset);
+  int errno_ = errno;
+  close(fd);
+  errno = errno_;
+  return (r==MAP_FAILED) ? NULL : r;
+}