-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbufferize.c
46 lines (39 loc) · 1.02 KB
/
bufferize.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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main( int argc, char ** argv )
{
if( argc != 3 )
{
fprintf( stderr, "Usage: %s file typename\n", argv[0] );
return EXIT_FAILURE;
}
struct stat st;
if( stat( argv[1], &st ) < 0 )
{
fprintf( stderr, "Failed to open file '%s': %s\n", argv[1], strerror( errno ) );
return EXIT_FAILURE;
}
int fd = open( argv[1], 0 );
if( fd < 0 )
{
fprintf( stderr, "Failed to open file '%s': %s\n", argv[1], strerror( errno ) );
return EXIT_FAILURE;
}
unsigned char * buf = malloc( (size_t)st.st_size );
read( fd, buf, (size_t)st.st_size );
printf( "static const u8 %s[%ld] = {", argv[2], st.st_size );
for( int i = 0; i < st.st_size; i++ )
{
printf( "%d", buf[i] );
if( i != st.st_size - 1 )
printf( "," );
}
printf( "};\n" );
close( fd );
free( buf );
}