comparison 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
comparison
equal deleted inserted replaced
166:861f5ca49581 167:c25367bb7656
1 /*
2 * This C module implements the low-level steps of file mapping and access.
3 */
4
5 #include <sys/types.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include "globals.h"
13
14 mmap_objfile()
15 {
16 int fd;
17 struct stat st;
18
19 fd = open(objfilename, O_RDONLY);
20 if (fd < 0) {
21 perror(objfilename);
22 exit(2);
23 }
24 fstat(fd, &st);
25 if (!S_ISREG(st.st_mode)) {
26 fprintf(stderr, "error: %s is not a regular file\n",
27 objfilename);
28 exit(2);
29 }
30 objfile_tot_size = st.st_size;
31 objfilemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE,
32 fd, 0L);
33 if (objfilemap == MAP_FAILED) {
34 perror("mmap");
35 exit(2);
36 }
37 close(fd);
38 }
39
40 mmap_binfile()
41 {
42 int fd;
43 struct stat st;
44
45 fd = open(binfilename, O_RDONLY);
46 if (fd < 0) {
47 perror(binfilename);
48 exit(2);
49 }
50 fstat(fd, &st);
51 if (!S_ISREG(st.st_mode)) {
52 fprintf(stderr, "error: %s is not a regular file\n",
53 binfilename);
54 exit(2);
55 }
56 binfile_tot_size = st.st_size;
57 binfilemap = mmap(NULL, binfile_tot_size, PROT_READ, MAP_PRIVATE,
58 fd, 0L);
59 if (binfilemap == MAP_FAILED) {
60 perror("mmap");
61 exit(2);
62 }
63 close(fd);
64 }
65
66 unsigned
67 get_u16(ptr)
68 u_char *ptr;
69 {
70 return ptr[0] | ptr[1] << 8;
71 }
72
73 get_s16(ptr)
74 u_char *ptr;
75 {
76 int i;
77
78 i = ptr[0] | ptr[1] << 8;
79 if (i >= 32768)
80 i -= 65536;
81 return(i);
82 }
83
84 unsigned
85 get_u32(ptr)
86 u_char *ptr;
87 {
88 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
89 }