Update bin2c.c

Fix behaviour under Windows if compiled with MinGW / TDM-GCC.
This commit is contained in:
Pablo Curiel 2021-02-08 14:10:38 -04:00
parent 4e7e5081a7
commit be23d1fa15

View file

@ -9,7 +9,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
@ -34,32 +33,39 @@ main ( int argc, char* argv[] )
{
unsigned char buf[BUFSIZ];
char* ident;
int fd, i, total, rd, need_comma;
FILE *fd;
size_t size, i, total, blksize = BUFSIZ;
int need_comma = 0;
if ( argc < 2 )
if ( argc != 2 )
{
fprintf ( stderr, "Usage: %s binary_file > output_file\n", argv[0] );
return -1;
}
fd = open ( argv[1], O_RDONLY );
if ( fd == -1 )
fd = fopen ( argv[1], "rb" );
if ( fd == NULL )
{
fprintf ( stderr, "%s: can't open %s for reading\n", argv[0], argv[1] );
return -1;
}
fseek(fd, 0, SEEK_END);
size = ftell(fd);
rewind(fd);
ident = make_ident ( argv[1] );
printf ( "static const unsigned char __attribute__((section (\"._%s\"))) %s[] = {", ident, ident );
for ( total = 0, need_comma = 0; ( rd = read ( fd, buf, BUFSIZ ) ) != 0; )
for ( total = 0; total < size; )
{
if ( rd == -1 )
if ( size - total < blksize ) blksize = size - total;
if ( fread ( buf, 1, blksize, fd ) != blksize )
{
fprintf ( stderr, "%s: file read error\n", argv[0] );
return -1;
}
for ( i = 0; i < rd; i++ )
for ( i = 0; i < blksize; i++ )
{
if ( need_comma ) printf ( ", " );
else need_comma = 1;
@ -70,7 +76,7 @@ main ( int argc, char* argv[] )
}
printf ( "\n};\n" );
close ( fd );
fclose ( fd );
free ( ident );
return 0;