From be23d1fa15a8a7516cab40012020fb22a97caa1a Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Mon, 8 Feb 2021 14:10:38 -0400 Subject: [PATCH] Update bin2c.c Fix behaviour under Windows if compiled with MinGW / TDM-GCC. --- tools/bin2c/bin2c.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tools/bin2c/bin2c.c b/tools/bin2c/bin2c.c index 33f2687..0d820fc 100644 --- a/tools/bin2c/bin2c.c +++ b/tools/bin2c/bin2c.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -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;