]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - historic/makehole.c
1 /* makehole.c - original by HJ Lu */
3 /* Patched by faith@cs.unc.edu, Wed Oct 6 18:01:39 1993 based on
4 information from Michael Bischoff <mbi@mo.math.nat.tu-bs.de> (Fri, 18
5 Jun 93 10:10:19 +0200). */
17 void usage(char *name
, char *message
)
20 fprintf(stderr
, "%s: %s\n", name
, message
);
25 fprintf(stderr
, "Usage:%s Imagefile\n", name
);
29 int ishole(char *buf
, int size
)
33 for (i
= 0; i
< size
; i
++)
40 void main(int argc
, char *argv
[])
48 struct exec
*header
= (struct exec
*) buf
;
52 fprintf(stderr
, "%s: must be root to run!\n", *argv
);
66 sprintf( tmp_file
, "hole%d", getpid() );
67 if (tmp_file
== NULL
) {
68 usage(*argv
, "Unable to get a temporary image filename!");
72 fprintf(stderr
, "Temparory image file: %s\n", tmp_file
);
77 fdin
= open(argv
[1], O_RDONLY
);
79 usage(*argv
, "unable to open file.");
81 fprintf(stderr
, "Making holes in %s...\n", argv
[1]);
85 if ((ret
= read(fdin
, header
, BUFSIZE
)) != BUFSIZE
86 || N_MAGIC(*header
) != ZMAGIC
) {
87 usage(*argv
, "file must be pure executable.");
90 fdout
= creat(tmp_file
, 0555);
92 perror("Unable to create the temparory image file!");
95 if (write(fdout
, header
, ret
) != ret
) {
96 perror("Fail to write header to the temparory image file!");
102 while ((ret
= read(fdin
, buf
, BUFSIZE
)) > 0) {
104 if (ishole(buf
, ret
)) {
106 fprintf(stderr
, "There is a %d byte hole from 0x%x to 0x%x.\n", ret
, abs_offset
- ret
, abs_offset
);
109 if (lseek(fdout
, abs_offset
, SEEK_SET
) != abs_offset
) {
110 perror("Fail to make a hole in the temparory image file!");
116 fprintf(stderr
, "Writing %d bytes from 0x%x to 0x%x.\n", ret
, abs_offset
- ret
, abs_offset
);
118 if (write(fdout
, buf
, ret
) != ret
) {
119 perror("Fail to write the temparory image file!");
126 if (ftruncate(fdout
, abs_offset
)) {
127 perror("Fail to truncate the temparory image file!");
134 if (rename(tmp_file
, argv
[1])) {
135 perror("Fail to rename the temparory image file to the old image file!");
139 fprintf(stderr
, "There are %d byte holes out of %d bytes in `%s'.\n", hole
, abs_offset
, argv
[1]);