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