FreeCalypso > hg > fc-am-toolkit
annotate bootmatch/comp_readbin.c @ 25:afd760eb1f8d
doc/C1xx-updates: new article
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 11 Jun 2023 20:57:23 +0000 |
parents | bfcc8180cf3c |
children |
rev | line source |
---|---|
9
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Bootmatch compiler: here we read the binary file. |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/file.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/stat.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <unistd.h> |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include "comp_defs.h" |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 extern u_char boot_image[BOOT_BLOCK_SIZE]; |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 void |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 read_bin_file(filename) |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 char *filename; |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 { |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 int fd; |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 struct stat st; |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 fd = open(filename, O_RDONLY); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 if (fd < 0) { |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 perror(filename); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 exit(1); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 fstat(fd, &st); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 if (!S_ISREG(st.st_mode)) { |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 fprintf(stderr, "error: %s is not a regular file\n", filename); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 exit(1); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 if (st.st_size != BOOT_BLOCK_SIZE) { |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 fprintf(stderr, "error: %s has wrong length\n", filename); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 exit(1); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 read(fd, boot_image, BOOT_BLOCK_SIZE); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 close(fd); |
bfcc8180cf3c
bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |