-
Notifications
You must be signed in to change notification settings - Fork 0
/
bincut.cpp
52 lines (45 loc) · 1.07 KB
/
bincut.cpp
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
#include <stdio.h>
#include <stdlib.h>
#define VERSION "V0.2"
// v0.2 プログラム終了時のリターンコードがおかしかったのを修正
void Usage( void )
{
puts("Usage: bincut infile outfile start_adr end_adr");
}
int main( int argc, char *argv[] )
{
if(argc!=5) {
Usage();
exit(-1);
}
puts("Binary cutter " VERSION);
puts("Programmed by an XM7 supporter");
FILE *fpi, *fpo;
if((fpi=fopen(argv[1], "rb"))==NULL) {
perror("Input file open");
}
if((fpo=fopen(argv[2], "wb"))==NULL) {
perror("Output file open");
}
unsigned long start, end;
sscanf(argv[3], "%lx", &start);
sscanf(argv[4], "%lx", &end);
if(start>end) {
printf("The end address specified is lower than start address.(ST=%lx, END=%lx)\n", start, end);
exit(-1);
}
if(fseek(fpi, start, SEEK_SET)) {
perror("File pointer seek error(The input file may be shorter than you expected)");
exit(-1);
}
for(unsigned long i=0; i<=end-start; i++) {
int ch = fgetc(fpi);
if(ch==-1) {
puts("Reached to file end.");
exit(-2);
}
fputc(ch, fpo);
}
fcloseall();
return 0;
}