This source file includes following definitions.
- die
- usage
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <a.out.h>
32 #include <linux/config.h>
33
34 #define GCC_HEADER 1024
35
36 #define STRINGIFY(x) #x
37
38 void die(char * str)
39 {
40 fprintf(stderr,"%s\n",str);
41 exit(1);
42 }
43
44 void usage(void)
45 {
46 die("Usage: xtract system [ | gzip | piggyback > piggy.s]");
47 }
48
49 int main(int argc, char ** argv)
50 {
51 int i,c,id, sz;
52 char buf[1024];
53 char major_root, minor_root;
54 struct stat sb;
55
56 struct exec *ex = (struct exec *)buf;
57
58 if (argc != 2)
59 usage();
60
61 if ((id=open(argv[1],O_RDONLY,0))<0)
62 die("Unable to open 'system'");
63 if (read(id,buf,GCC_HEADER) != GCC_HEADER)
64 die("Unable to read header of 'system'");
65 if (N_MAGIC(*ex) != ZMAGIC)
66 die("Non-GCC header of 'system'");
67
68 sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
69
70 fprintf(stderr, "System size is %d\n", sz);
71
72 while (sz)
73 {
74 int l, n;
75
76 l = sz;
77 if (l > sizeof(buf)) l = sizeof(buf);
78
79 if ((n=read(id, buf, l)) !=l)
80 {
81 if (n == -1)
82 perror(argv[1]);
83 else
84 fprintf(stderr, "Unexpected EOF\n");
85
86 die("Can't read system");
87 }
88
89 write(1, buf, l);
90 sz -= l;
91 }
92
93 close(id);
94 return(0);
95 }