]> mj.ucw.cz Git - libucw.git/commitdiff
Implemented mmap_file().
authorMartin Mares <mj@ucw.cz>
Sat, 27 Mar 1999 21:42:43 +0000 (21:42 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 27 Mar 1999 21:42:43 +0000 (21:42 +0000)
lib/lib.h
lib/mmap.c [new file with mode: 0644]

index e0613eddf1733376c9de66985167d852077d9fcc..e6ffb2b6cc1be5dd1b9492a7e3664c21f3ade25e 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -162,4 +162,8 @@ void scan_obj_tree(byte *, void (*)(ulg, byte *));
 
 uns random_max(uns);
 
+/* mmap.c */
+
+void *mmap_file(byte *name, unsigned *len);
+
 #endif
diff --git a/lib/mmap.c b/lib/mmap.c
new file mode 100644 (file)
index 0000000..4714731
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *     Sherlock Library -- Mapping of Files
+ *
+ *     (c) 1999 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+#include "lib.h"
+
+void *
+mmap_file(byte *name, unsigned *len)
+{
+  int fd = open(name, O_RDONLY);
+  struct stat st;
+  void *x;
+
+  if (fd < 0)
+    return NULL;
+  if (fstat(fd, &st) < 0)
+    x = NULL;
+  else
+    {
+      if (len)
+       *len = st.st_size;
+      x = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+      if (x == MAP_FAILED)
+       x = NULL;
+    }
+  close(fd);
+  return x;
+}