Better describe what this macro does (comment fix.)
[syslinux.git] / bin2c.pl
blobf50e2e7f4ed4445f3a8ef6aba0b54de58e2667c3
1 #!/usr/bin/perl
2 ## $Id$
3 ## -----------------------------------------------------------------------
4 ##
5 ## Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
6 ##
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 ## Boston MA 02111-1307, USA; either version 2 of the License, or
11 ## (at your option) any later version; incorporated herein by reference.
13 ## -----------------------------------------------------------------------
16 # bin2c.pl: binary file to C source converter
19 eval { use bytes; };
20 eval { binmode STDIN; };
22 if ( $#ARGV != 0 ) {
23 print STDERR "Usage: $0 table_name < input_file > output_file\n";
24 exit 1;
27 ($table_name) = @ARGV;
29 printf "unsigned char %s[] = {\n", $table_name;
31 $pos = 0;
32 $linelen = 8;
34 $total_len = 0;
36 while ( ($n = read(STDIN, $data, 4096)) > 0 ) {
37 $total_len += $n;
38 for ( $i = 0 ; $i < $n ; $i++ ) {
39 $byte = substr($data, $i, 1);
40 if ( $pos >= $linelen ) {
41 print ",\n\t";
42 $pos = 0;
43 } elsif ( $pos > 0 ) {
44 print ", ";
45 } else {
46 print "\t";
48 printf("0x%02x", unpack("C", $byte));
49 $pos++;
53 printf "\n};\n\nunsigned int %s_len = %u;\n", $table_name, $total_len;
55 @st = stat STDIN;
57 printf "\nint %s_mtime = %d;\n", $table_name, $st[9];
59 exit 0;