summaryrefslogtreecommitdiffstats
path: root/lib/f_map.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/f_map.c')
-rw-r--r--lib/f_map.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/f_map.c b/lib/f_map.c
new file mode 100644
index 0000000..e60c2f4
--- /dev/null
+++ b/lib/f_map.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+
+#ifdef MAP_FILE /* 44BSD defines this & requires it to mmap files */
+# define DAO_MAP (MAP_SHARED | MAP_FILE)
+#else
+# define DAO_MAP (MAP_SHARED)
+#endif
+
+
+char *
+f_map(fpath, fsize)
+ char *fpath;
+ int *fsize;
+{
+ int fd, size;
+ struct stat st;
+
+ if ((fd = open(fpath, O_RDONLY)) < 0)
+ return (char *) -1;
+
+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || (size = st.st_size) <= 0)
+ {
+ close(fd);
+ return (char *) -1;
+ }
+
+ fpath = (char *) mmap(NULL, size, PROT_READ, DAO_MAP, fd, 0);
+ close(fd);
+ *fsize = size;
+ return fpath;
+}