source: trunk/minix/commands/aal/rd.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.5 KB
RevLine 
[9]1/* $Header: /cvsup/minix/src/commands/aal/rd.c,v 1.1.1.1 2005/04/21 14:53:58 beng Exp $ */
2/*
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 */
6#include <out.h>
7#include "object.h"
8
9extern long lseek();
10
11/*
12 * Parts of the output file.
13 */
14#undef PARTEMIT
15#undef PARTRELO
16#undef PARTNAME
17#undef PARTCHAR
18#undef PARTDBUG
19#undef NPARTS
20
21#define PARTEMIT 0
22#define PARTRELO 1
23#define PARTNAME 2
24#define PARTCHAR 3
25#ifdef SYMDBUG
26#define PARTDBUG 4
27#else
28#define PARTDBUG 3
29#endif
30#define NPARTS (PARTDBUG + 1)
31
32static long offset[MAXSECT];
33
34static int outfile;
35static long outseek[NPARTS];
36static long currpos;
37static long rd_base;
38#define OUTSECT(i) \
39 (outseek[PARTEMIT] = offset[i])
40#define BEGINSEEK(p, o) \
41 (outseek[(p)] = (o))
42
43static int sectionnr;
44
45static
46OUTREAD(p, b, n)
47 char *b;
48 long n;
49{
50 register long l = outseek[p];
51
52 if (currpos != l) {
53 lseek(outfile, l, 0);
54 }
55 rd_bytes(outfile, b, n);
56 l += n;
57 currpos = l;
58 outseek[p] = l;
59}
60
61/*
62 * Open the output file according to the chosen strategy.
63 */
64int
65rd_open(f)
66 char *f;
67{
68
69 if ((outfile = open(f, 0)) < 0)
70 return 0;
71 return rd_fdopen(outfile);
72}
73
74static int offcnt;
75
76rd_fdopen(fd)
77{
78 register int i;
79
80 for (i = 0; i < NPARTS; i++) outseek[i] = 0;
81 offcnt = 0;
82 rd_base = lseek(fd, 0L, 1);
83 if (rd_base < 0) {
84 return 0;
85 }
86 currpos = rd_base;
87 outseek[PARTEMIT] = currpos;
88 outfile = fd;
89 sectionnr = 0;
90 return 1;
91}
92
93rd_close()
94{
95
96 close(outfile);
97 outfile = -1;
98}
99
100rd_fd()
101{
102 return outfile;
103}
104
105rd_ohead(head)
106 register struct outhead *head;
107{
108 register long off;
109
110 OUTREAD(PARTEMIT, (char *) head, (long) SZ_HEAD);
111#if ! (BYTES_REVERSED || WORDS_REVERSED)
112 if (sizeof(struct outhead) != SZ_HEAD)
113#endif
114 {
115 register char *c = (char *) head + (SZ_HEAD-4);
116
117 head->oh_nchar = get4(c);
118 c -= 4; head->oh_nemit = get4(c);
119 c -= 2; head->oh_nname = uget2(c);
120 c -= 2; head->oh_nrelo = uget2(c);
121 c -= 2; head->oh_nsect = uget2(c);
122 c -= 2; head->oh_flags = uget2(c);
123 c -= 2; head->oh_stamp = uget2(c);
124 c -= 2; head->oh_magic = uget2(c);
125 }
126 off = OFF_RELO(*head) + rd_base;
127 BEGINSEEK(PARTRELO, off);
128 off += (long) head->oh_nrelo * SZ_RELO;
129 BEGINSEEK(PARTNAME, off);
130 off += (long) head->oh_nname * SZ_NAME;
131 BEGINSEEK(PARTCHAR, off);
132#ifdef SYMDBUG
133 off += head->oh_nchar;
134 BEGINSEEK(PARTDBUG, off);
135#endif
136}
137
138rd_rew_relos(head)
139 register struct outhead *head;
140{
141 register long off = OFF_RELO(*head) + rd_base;
142
143 BEGINSEEK(PARTRELO, off);
144}
145
146rd_sect(sect, cnt)
147 register struct outsect *sect;
148 register unsigned int cnt;
149{
150 register char *c = (char *) sect + cnt * SZ_SECT;
151
152 OUTREAD(PARTEMIT, (char *) sect, (long)cnt * SZ_SECT);
153 sect += cnt;
154 offcnt += cnt;
155 while (cnt--) {
156 sect--;
157#if ! (BYTES_REVERSED || WORDS_REVERSED)
158 if (sizeof(struct outsect) != SZ_SECT)
159#endif
160 {
161 c -= 4; sect->os_lign = get4(c);
162 c -= 4; sect->os_flen = get4(c);
163 c -= 4; sect->os_foff = get4(c);
164 }
165 offset[--offcnt] = sect->os_foff + rd_base;
166#if ! (BYTES_REVERSED || WORDS_REVERSED)
167 if (sizeof(struct outsect) != SZ_SECT)
168#endif
169 {
170 c -= 4; sect->os_size = get4(c);
171 c -= 4; sect->os_base = get4(c);
172 }
173 }
174}
175
176rd_outsect(s)
177{
178 OUTSECT(s);
179 sectionnr = s;
180}
181
182/*
183 * We don't have to worry about byte order here.
184 */
185rd_emit(emit, cnt)
186 char *emit;
187 long cnt;
188{
189 OUTREAD(PARTEMIT, emit, cnt);
190 offset[sectionnr] += cnt;
191}
192
193rd_relo(relo, cnt)
194 register struct outrelo *relo;
195 register unsigned int cnt;
196{
197
198 OUTREAD(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
199#if ! (BYTES_REVERSED || WORDS_REVERSED)
200 if (sizeof(struct outrelo) != SZ_RELO)
201#endif
202 {
203 register char *c = (char *) relo + (long) cnt * SZ_RELO;
204
205 relo += cnt;
206 while (cnt--) {
207 relo--;
208 c -= 4; relo->or_addr = get4(c);
209 c -= 2; relo->or_nami = uget2(c);
210 relo->or_sect = *--c;
211 relo->or_type = *--c;
212 }
213 }
214}
215
216rd_name(name, cnt)
217 register struct outname *name;
218 register unsigned int cnt;
219{
220
221 OUTREAD(PARTNAME, (char *) name, (long) cnt * SZ_NAME);
222#if ! (BYTES_REVERSED || WORDS_REVERSED)
223 if (sizeof(struct outname) != SZ_NAME)
224#endif
225 {
226 register char *c = (char *) name + (long) cnt * SZ_NAME;
227
228 name += cnt;
229 while (cnt--) {
230 name--;
231 c -= 4; name->on_valu = get4(c);
232 c -= 2; name->on_desc = uget2(c);
233 c -= 2; name->on_type = uget2(c);
234 c -= 4; name->on_foff = get4(c);
235 }
236 }
237}
238
239rd_string(addr, len)
240 char *addr;
241 long len;
242{
243
244 OUTREAD(PARTCHAR, addr, len);
245}
246
247#ifdef SYMDBUG
248rd_dbug(buf, size)
249 char *buf;
250 long size;
251{
252 OUTREAD(PARTDBUG, buf, size);
253}
254#endif
Note: See TracBrowser for help on using the repository browser.