(U-0002195D): Add `ideographic-structure'; add `sound@ja/on'; add
[chise/xemacs-chise.git.1] / src / unexencap.c
1 /* Waiting for papers!  */
2
3 /* Synched up with: FSF 19.31. */
4
5 /*
6  * Do an unexec() for coff encapsulation. Uses the approach I took
7  * for AKCL, so don't be surprised if it doesn't look too much like
8  * the other unexec() routines. Assumes NO_REMAP. Should be easy to
9  * adapt to the emacs style unexec() if that is desired, but this works
10  * just fine for me with GCC/GAS/GLD under System V.  - Jordan
11  */
12
13 #include <sys/types.h>
14 #include <sys/fcntl.h>
15 #include <sys/file.h>
16 #include <stdio.h>
17 #include "/usr/gnu/lib/gcc/gcc-include/a.out.h"
18
19 filecpy(to, from, n)
20 FILE *to, *from;
21 int n;
22 {
23         char buffer[BUFSIZ];
24
25         for (;;)
26                 if (n > BUFSIZ) {
27                         fread(buffer, BUFSIZ, 1, from);
28                         fwrite(buffer, BUFSIZ, 1, to);
29                         n -= BUFSIZ;
30                 } else if (n > 0) {
31                         fread(buffer, 1, n, from);
32                         fwrite(buffer, 1, n, to);
33                         break;
34                 } else
35                         break;
36 }
37 /* ****************************************************************
38  * unexec
39  *
40  * driving logic.
41  * ****************************************************************/
42 unexec (new_name, a_name, data_start, bss_start, entry_address)
43 char *new_name, *a_name;
44 unsigned data_start, bss_start, entry_address;
45 {       
46         struct coffheader header1;
47         struct coffscn *tp, *dp, *bp;
48         struct exec header;
49         int stsize;
50         char *original_file = a_name;
51         char *save_file = new_name;
52
53         char *data_begin, *data_end;
54         int original_data;
55         FILE *original, *save;
56         int n;
57         char *p;
58         extern char *sbrk();
59         char stdin_buf[BUFSIZ], stdout_buf[BUFSIZ];
60
61
62         fclose(stdin);
63         original = fopen(original_file, "r");
64         if (stdin != original || original->_file != 0) {
65                 fprintf(stderr, "unexec: Can't open the original file.\n");
66                 exit(1);
67         }
68         setbuf(original, stdin_buf);
69         fclose(stdout);
70         unlink(save_file);
71         n = open (save_file, O_CREAT|O_WRONLY, 0777);
72         if (n != 1 || (save = fdopen(n, "w")) != stdout) {
73                 fprintf(stderr, "unexec: Can't open the save file.\n");
74                 exit(1);
75         }
76         setbuf(save, stdout_buf);
77
78         fread(&header1, sizeof(header1), 1, original);
79         tp = &header1.scns[0];
80         dp = &header1.scns[1];
81         bp = &header1.scns[2];
82         fread(&header, sizeof(header), 1, original);
83         data_begin=(char *)N_DATADDR(header);
84         data_end = sbrk(0);
85         original_data = header.a_data;
86         header.a_data = data_end - data_begin;
87         header.a_bss = 0;
88         dp->s_size = header.a_data;
89         bp->s_paddr = dp->s_vaddr + dp->s_size;
90         bp->s_vaddr = bp->s_paddr; 
91         bp->s_size = 0; 
92         header1.tsize = tp->s_size;
93         header1.dsize = dp->s_size;
94         header1.bsize = bp->s_size;
95         fwrite(&header1, sizeof(header1), 1, save);
96         fwrite(&header, sizeof(header), 1, save);
97
98         filecpy(save, original, header.a_text);
99
100         for (n = header.a_data, p = data_begin;  ;  n -= BUFSIZ, p += BUFSIZ)
101                 if (n > BUFSIZ)
102                         fwrite(p, BUFSIZ, 1, save);
103                 else if (n > 0) {
104                         fwrite(p, 1, n, save);
105                         break;
106                 } else
107                         break;
108
109         fseek(original, original_data, 1);
110
111         filecpy(save, original, header.a_syms+header.a_trsize+header.a_drsize);
112         fread(&stsize, sizeof(stsize), 1, original);
113         fwrite(&stsize, sizeof(stsize), 1, save);
114         filecpy(save, original, stsize - sizeof(stsize));
115
116         fclose(original);
117         fclose(save);
118 }