FreeCalypso > hg > freecalypso-reveng
annotate objgrep/lowlevel.c @ 347:21d38dce4d15
fluid-mnf: removed silly wait for RETURN from no-arguments display
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 13 Mar 2020 06:27:15 +0000 |
parents | c25367bb7656 |
children |
rev | line source |
---|---|
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
167
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
2 * This C module implements the low-level steps of file mapping and access. |
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 */ |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 #include <sys/types.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <sys/file.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <sys/stat.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/mman.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <stdio.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <stdlib.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <unistd.h> |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include "globals.h" |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 mmap_objfile() |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 int fd; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 struct stat st; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 fd = open(objfilename, O_RDONLY); |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 if (fd < 0) { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 perror(objfilename); |
167
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
22 exit(2); |
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 fstat(fd, &st); |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 if (!S_ISREG(st.st_mode)) { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 fprintf(stderr, "error: %s is not a regular file\n", |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 objfilename); |
167
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
28 exit(2); |
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 objfile_tot_size = st.st_size; |
167
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
31 objfilemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE, |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
32 fd, 0L); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
33 if (objfilemap == MAP_FAILED) { |
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 perror("mmap"); |
167
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
35 exit(2); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
36 } |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
37 close(fd); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
38 } |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
39 |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
40 mmap_binfile() |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
41 { |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
42 int fd; |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
43 struct stat st; |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
44 |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
45 fd = open(binfilename, O_RDONLY); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
46 if (fd < 0) { |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
47 perror(binfilename); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
48 exit(2); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
49 } |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
50 fstat(fd, &st); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
51 if (!S_ISREG(st.st_mode)) { |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
52 fprintf(stderr, "error: %s is not a regular file\n", |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
53 binfilename); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
54 exit(2); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
55 } |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
56 binfile_tot_size = st.st_size; |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
57 binfilemap = mmap(NULL, binfile_tot_size, PROT_READ, MAP_PRIVATE, |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
58 fd, 0L); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
59 if (binfilemap == MAP_FAILED) { |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
60 perror("mmap"); |
c25367bb7656
objgrep: written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
130
diff
changeset
|
61 exit(2); |
74
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 close(fd); |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 unsigned |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 get_u16(ptr) |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 u_char *ptr; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 return ptr[0] | ptr[1] << 8; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 get_s16(ptr) |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 u_char *ptr; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 int i; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 i = ptr[0] | ptr[1] << 8; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 if (i >= 32768) |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 i -= 65536; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 return(i); |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 } |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 unsigned |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 get_u32(ptr) |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 u_char *ptr; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 { |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24; |
2eef88395908
tiobjd: a little refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 } |