File: bc_to_h.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (49 lines) | stat: -rw-r--r-- 859 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main (int argc, char *argv[])
{
    int fd;
    int nread;
    unsigned char buf[1024];
    int done = 0;
    int i;
    int count = 0;
    
    if (argc != 2) {
	printf ("Usage: comp <filename>\n");
	exit (0);
    }


    fd = open (argv[1], O_RDONLY);
    if (fd < 0) {
	fprintf (stderr, "Unable to open file %s: %s\n", argv[1], strerror (errno));
	exit (1);
    }
  
    printf ("char compiler_bytecode[] = \n\"");
    do {
	nread = read (fd, buf, 1024);
	if (nread < 1024)
	    done = 1;

	for (i = 0; i < nread; i++) {
	    printf ("\\x%.2x", (unsigned int) buf[i]);
	    count++;
	    if (!(count % 20)) {
		printf ("\"\n\"");
	    }
	}
    } while (!done);
    printf ("\";\n\n");

    return (0);
}