Merge pull request #575 from DarkMatterCore/master

Fix bin2c behaviour under Windows if compiled with MinGW / TDM-GCC.
This commit is contained in:
CTCaer 2021-02-08 23:32:51 +02:00 committed by GitHub
commit e8cf85bd65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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