diff objgrep/lowlevel.c @ 167:c25367bb7656

objgrep: written, compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 04 Jul 2014 00:54:33 +0000
parents leo-obj/tool/lowlevel.c@87b82398a08b
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/objgrep/lowlevel.c	Fri Jul 04 00:54:33 2014 +0000
@@ -0,0 +1,89 @@
+/*
+ * This C module implements the low-level steps of file mapping and access.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "globals.h"
+
+mmap_objfile()
+{
+	int fd;
+	struct stat st;
+
+	fd = open(objfilename, O_RDONLY);
+	if (fd < 0) {
+		perror(objfilename);
+		exit(2);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n",
+			objfilename);
+		exit(2);
+	}
+	objfile_tot_size = st.st_size;
+	objfilemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE,
+			  fd, 0L);
+	if (objfilemap == MAP_FAILED) {
+		perror("mmap");
+		exit(2);
+	}
+	close(fd);
+}
+
+mmap_binfile()
+{
+	int fd;
+	struct stat st;
+
+	fd = open(binfilename, O_RDONLY);
+	if (fd < 0) {
+		perror(binfilename);
+		exit(2);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n",
+			binfilename);
+		exit(2);
+	}
+	binfile_tot_size = st.st_size;
+	binfilemap = mmap(NULL, binfile_tot_size, PROT_READ, MAP_PRIVATE,
+			  fd, 0L);
+	if (binfilemap == MAP_FAILED) {
+		perror("mmap");
+		exit(2);
+	}
+	close(fd);
+}
+
+unsigned
+get_u16(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8;
+}
+
+get_s16(ptr)
+	u_char *ptr;
+{
+	int i;
+
+	i = ptr[0] | ptr[1] << 8;
+	if (i >= 32768)
+		i -= 65536;
+	return(i);
+}
+
+unsigned
+get_u32(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
+}