-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinker.c
75 lines (68 loc) · 1.73 KB
/
linker.c
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
#include <stdio.h>
#include <string.h>
int filesize(FILE *fp) {
fseek(fp,0,SEEK_END);
int fsize=ftell(fp);
fseek(fp,0, SEEK_SET);
return fsize;
}
char ver[5] = "0.1";
int main (int argc, char *argv[]) {
printf("Linker v%s\n-----------\n\n",ver);
if (argc<3) {
printf("linker failed - %d parameters passed but 2 required. \n",argc-1);
return 0;
}
char *ipos_ptr = strrchr(argv[0],'\\');
if (ipos_ptr==NULL) {
ipos_ptr=argv[0];
}
else {
ipos_ptr=ipos_ptr+1;
}
char infile[strlen(argv[0])];
strcpy(infile,ipos_ptr);
printf("%s %s %s\n\n",infile, argv[1], argv[2]);
FILE *vmfile;
FILE *bcfile;
FILE *outfile;
vmfile=fopen(argv[1],"rb");
if (vmfile==0) {
printf("linker failed - %s missing or unable to open.", argv[1]);
return 0;
}
bcfile=fopen(argv[2],"rb");
if (bcfile==0) {
printf("linker failed - %s missing or unable to open.", argv[2]);
return 0;
}
int len = strlen(argv[2]) + 5; // allow room for a ".exe" and a null terminating character.
char output[len];
char *pos_ptr = strrchr(argv[2],'.');
if (pos_ptr != NULL) {
int pos = pos_ptr-argv[2];
strncpy(output,argv[2],pos);
}
strcat(output,".exe");
outfile=fopen(output,"wb");
char buf[255];
int eof=1;
while (eof==1) {
eof=fread(buf,1,1,vmfile);
if (eof==0) break;
fwrite(buf,1,1,outfile);
}
long bc_index=ftell(vmfile); //capture bytecode index point.
fclose(vmfile);
eof=1;
while (eof==1) {
eof=fread(buf,1,1,bcfile);
if (eof==0) break;
fwrite(buf,1,1,outfile);
}
fwrite(&bc_index,sizeof(bc_index),1,outfile); //writes index point in little endian format - lowest digits first.
fclose(bcfile);
fclose(outfile);
printf("success : %s\n",output);
return 1;
}