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