annotate src/unexenix.c @ 158:558dfa75ffb3

Added tag r20-3b5 for changeset 6b37e6ddd302
author cvs
date Mon, 13 Aug 2007 09:40:48 +0200
parents 376386a54a3c
children 04bc9d2f42c7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Unexec for Xenix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1988, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.31. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 On 80386 Xenix, segmentation screws prevent us from modifying the text
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 segment at all. We basically just plug a new value for "data segment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 size" into the countless headers and copy the other records straight
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 through. The data segment is ORG'ed at the xs_rbase value of the data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 segment's xseg record (always @ 0x1880000, thanks to the "sophisticated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 memory management hardware" of the chip) and extends to sbrk(0), exactly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 This code is afraid to malloc (should it be?), and alloca has to be the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 wimpy, malloc-based version; consequently, data is usually copied in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 smallish chunks.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 gb@entity.com
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #include <varargs.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 static void fatal_unexec ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 errno = EEOF; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 if (read(_fd, _buffer, _size) != _size) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 fatal_unexec(_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 if (write(_fd, _buffer, _size) != _size) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 fatal_unexec(_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #define SEEK(_fd, _position, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 errno = EEOF; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 if (lseek(_fd, _position, L_SET) != _position) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 fatal_unexec(_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 extern char *strerror ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 #define EEOF -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 #ifndef L_SET
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #define L_SET 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 /* Should check the magic number of the old executable;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 not yet written. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 check_exec (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 struct xexec *x;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 unexec (new_name, a_name, data_start, bss_start, entry_address)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 char *new_name, *a_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 unsigned data_start, bss_start, entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 char *sbrk (), *datalim = sbrk (0), *data_org;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 long segpos, textseen, textpos, textlen, datapos, datadiff, datalen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 struct xexec u_xexec, /* a.out header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 *u_xexecp = &u_xexec;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 struct xext u_xext, /* extended header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 *u_xextp = &u_xext;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 struct xseg u_xseg, /* segment table entry */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 *u_xsegp = &u_xseg;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 int i, nsegs, isdata = 0, infd, outfd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 infd = open (a_name, O_RDONLY, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 if (infd < 0) fatal_unexec ("opening %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 outfd = creat (new_name, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 if (outfd < 0) fatal_unexec ("creating %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 READ (infd, u_xexecp, sizeof (struct xexec),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 "error reading %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 check_exec (u_xexecp);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 READ (infd, u_xextp, sizeof (struct xext),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 "error reading %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 segpos = u_xextp->xe_segpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 nsegs = u_xextp->xe_segsize / sizeof (struct xseg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 SEEK (infd, segpos, "seek error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 for (i = 0; i < nsegs; i ++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 READ (infd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 "error reading %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 switch (u_xsegp->xs_type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 case XS_TTEXT:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 if (i == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 textpos = u_xsegp->xs_filpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 textlen = u_xsegp->xs_psize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 fatal_unexec ("invalid text segment in %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 case XS_TDATA:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 if (i == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 datapos = u_xsegp->xs_filpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 datalen = datalim - (data_org = (char *)(u_xsegp->xs_rbase));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 datadiff = datalen - u_xsegp->xs_psize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 fatal_unexec ("invalid data segment in %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 if (i > 1) break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 fatal_unexec ("invalid segment record in %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 u_xexecp->x_data = datalen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 u_xexecp->x_bss = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 WRITE (outfd, u_xexecp, sizeof (struct xexec),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 "error writing %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 WRITE (outfd, u_xextp, sizeof (struct xext),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 "error writing %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 SEEK (infd, segpos, "seek error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 SEEK (outfd, segpos, "seek error on %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 /* Copy the text segment record verbatim. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 copyrec (infd, outfd, sizeof (struct xseg), a_name, new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 /* Read, modify, write the data segment record. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 READ (infd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 "error reading %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 u_xsegp->xs_psize = u_xsegp->xs_vsize = datalen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 u_xsegp->xs_attr &= (~XS_AITER & ~XS_ABSS);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 WRITE (outfd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 "error writing %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 /* Now copy any additional segment records, adjusting their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 file position field */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 for (i = 2; i < nsegs; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 READ (infd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 "error reading %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 u_xsegp->xs_filpos += datadiff;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 WRITE (outfd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 "error writing %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 SEEK (infd, textpos, "seek error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 SEEK (outfd, textpos, "seek error on %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 copyrec (infd, outfd, textlen, a_name, new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 SEEK (outfd, datapos, "seek error on %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 WRITE (outfd, data_org, datalen,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 "write error on %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 for (i = 2, segpos += (2 * sizeof (struct xseg));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 i < nsegs;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 i++, segpos += sizeof (struct xseg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 SEEK (infd, segpos, "seek error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 READ (infd, u_xsegp, sizeof (struct xseg),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 "read error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 SEEK (infd, u_xsegp->xs_filpos, "seek error on %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 /* We should be at eof in the output file here, but we must seek
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 because the xs_filpos and xs_psize fields in symbol table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 segments are inconsistent. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 SEEK (outfd, u_xsegp->xs_filpos + datadiff, "seek error on %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 copyrec (infd, outfd, u_xsegp->xs_psize, a_name, new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 close (infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 close (outfd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 mark_x (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 copyrec (infd, outfd, len, in_name, out_name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 int infd, outfd, len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 char *in_name, *out_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 char buf[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 int chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 while (len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 chunk = BUFSIZ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 if (chunk > len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 chunk = len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 READ (infd, buf, chunk, "error reading %s", in_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 WRITE (outfd, buf, chunk, "error writing %s", out_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 len -= chunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 * mark_x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 * After successfully building the new a.out, mark it executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 static
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 mark_x (name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 char *name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 struct stat sbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 int um = umask (777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 umask (um);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 if (stat (name, &sbuf) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 fatal_unexec ("getting protection on %s", name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 sbuf.st_mode |= 0111 & ~um;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 if (chmod (name, sbuf.st_mode) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 fatal_unexec ("setting protection on %s", name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 fatal_unexec (s, va_alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 va_dcl
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 va_list ap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 if (errno == EEOF)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 fputs ("unexec: unexpected end of file, ", stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 fprintf (stderr, "unexec: %s, ", strerror (errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 va_start (ap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 _doprnt (s, ap, stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 fputs (".\n", stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 }