forked from LuaDist/srlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglue.c
58 lines (54 loc) · 1.32 KB
/
glue.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
/*
* glue.c
* glue exe and script
* Luiz Henrique de Figueiredo <[email protected]>
* 19 Feb 2005 09:14:06
* This code is hereby placed in the public domain.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glue.h"
static void cannot(const char* what, const char* name)
{
fprintf(stderr,"cannot %s %s: %s\n",what,name,strerror(errno));
exit(EXIT_FAILURE);
}
static long copy(const char* name, FILE* out, const char* outname)
{
FILE* f;
long size;
f=fopen(name,"rb");
if (f==NULL) cannot("open",name);
if (fseek(f,0,SEEK_END)!=0) cannot("seek",name);
size=ftell(f);
if (fseek(f,0,SEEK_SET)!=0) cannot("seek",name);
for (;;)
{
char b[BUFSIZ];
int n=fread(&b,1,sizeof(b),f);
if (n==0) { if (ferror(f)) cannot("read",name); else break; }
if (fwrite(&b,n,1,out)!=1) cannot("write",outname);
}
fclose(f);
return size;
}
int main(int argc, char* argv[])
{
Glue t= { GLUESIG, 0, 0 };
FILE* f;
if (argc<4)
{
fprintf(stderr,"usage: glue c.exe prog.lua prog.exe\n");
return 1;
}
f=fopen(argv[3],"wb");
if (f==NULL) cannot("open",argv[3]);
t.size1=copy(argv[1],f,argv[3]);
t.size2=copy(argv[2],f,argv[3]);
t.sig[GLUETYP]= (argv[4]!=NULL) ? argv[4][0] : 'L';
if (fwrite(&t,sizeof(t),1,f)!=1) cannot("write",argv[3]);
if (fclose(f)!=0) cannot("close",argv[3]);
return 0;
}