1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* Change Attributes program for GNU.
Copyright (C) 1988 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <a.out.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/file.h>
#include "ioutil.h"
#define forward extern
enum boolean { false, true };
typedef enum boolean boolean;
boolean change_to_shared_p;
boolean change_to_demand_loaded_p;
boolean silent_p;
int failures;
char *input_filename;
char output_filename [20];
int output_descriptor;
void
main (argc, argv)
int argc;
char *argv[];
{
register int argi;
register char **argp;
forward void usage ();
forward void process_file ();
forward void file_abort_handler ();
extern char *mktemp ();
iou_set_program_name (argv);
argi = 1;
argp = (& (argv [1]));
change_to_shared_p = false;
change_to_demand_loaded_p = false;
silent_p = false;
while ((argi < argc) && (((*argp) [0]) == '-'))
{
switch ((*argp) [1])
{
case 'n':
change_to_shared_p = true;
break;
case 'q':
change_to_demand_loaded_p = true;
break;
case 's':
silent_p = true;
break;
default:
usage ();
}
if (((*argp) [2]) != '\0')
usage ();
argi += 1;
argp += 1;
}
if (change_to_shared_p && change_to_demand_loaded_p)
iou_error ("conflicting options: -n and -q");
if ((! change_to_shared_p) && (! change_to_demand_loaded_p) && silent_p)
exit (0);
if (argi == argc)
exit (255);
strcpy (output_filename, "/tmp/chatrXXXXXX");
if (output_filename != (mktemp (output_filename)))
iou_error ("mktemp failure");
failures = 0;
for (; (argi < argc); argi += 1)
{
input_filename = (*argp++);
output_descriptor =
(iou_open (output_filename, (O_RDWR | O_CREAT | O_TRUNC), 0666));
iou_abort_handler_bind (process_file, file_abort_handler);
iou_close (output_descriptor);
}
iou_unlink (output_filename);
exit (failures);
}
void
usage ()
{
fprintf (stderr, "usage: %s [-n] [-q] [-s] file ...\n", iou_program_name);
iou_error ();
}
void
file_abort_handler ()
{
failures += 1;
return;
}
void
file_error (message)
char *message;
{
char buffer [256];
sprintf (buffer, "%s: \"%s\"", message, input_filename);
iou_error (buffer);
}
void
file_copy (input_descriptor, output_descriptor)
int input_descriptor;
int output_descriptor;
{
char buffer [8192];
register int bytes_read;
while (1)
{
bytes_read = (iou_read (input_descriptor, buffer, 8192));
if (bytes_read == 0) break;
iou_write (output_descriptor, buffer, bytes_read);
}
return;
}
void
process_file ()
{
int input_descriptor;
struct exec input_exec;
struct exec output_exec;
input_descriptor = (iou_open (input_filename, O_RDONLY));
if ((iou_read (input_descriptor, (& input_exec), (sizeof (input_exec)))) !=
(sizeof (input_exec)))
file_error ("unable to read file header");
switch (N_MAGIC (input_exec))
{
case NMAGIC:
if (change_to_shared_p)
file_error ("file not demand load executable");
break;
case ZMAGIC:
if (change_to_demand_loaded_p)
file_error ("file not shared executable");
break;
default:
file_error ("file not executable format");
}
if (! silent_p)
{
printf ("%s:\n", input_filename);
if (change_to_shared_p || change_to_demand_loaded_p)
printf (" current values:\n");
printf
(" %s executable\n",
(((N_MAGIC (input_exec)) == NMAGIC) ? "shared" : "demand loaded"));
fflush (stdout);
}
if ((! change_to_shared_p) && (! change_to_demand_loaded_p))
return;
output_exec = input_exec;
N_SET_MAGIC (output_exec, (change_to_shared_p ? NMAGIC : ZMAGIC));
iou_write (output_descriptor, (& output_exec), (sizeof (output_exec)));
iou_lseek (input_descriptor, (N_TXTOFF (input_exec)), 0);
iou_lseek (output_descriptor, (N_TXTOFF (output_exec)), 0);
file_copy (input_descriptor, output_descriptor);
/* Now copy the temporary output file back into the input file. */
iou_close (input_descriptor);
iou_lseek (output_descriptor, 0, 0);
input_descriptor = (iou_open (input_filename, (O_WRONLY | O_TRUNC), 0777));
file_copy (output_descriptor, input_descriptor);
iou_close (input_descriptor);
if (! silent_p)
{
printf (" new values:\n");
printf
(" %s executable\n",
(((N_MAGIC (output_exec)) == NMAGIC) ? "shared" : "demand loaded"));
fflush (stdout);
}
return;
}
|