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