X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=support%2Fmakebin%2Fmakebin.c;h=b6acaf4c872dde008a089962a2be76a78aa29499;hb=f3a44fc094dc5ce393e2029d3c7c6587a4b1a835;hp=f650b3d2e4e793df5a2bbd9ad1dcbd2828fd99ee;hpb=da5ade526c6f614e337918af7d56e9fcd9ccb180;p=fw%2Fsdcc diff --git a/support/makebin/makebin.c b/support/makebin/makebin.c index f650b3d2..b6acaf4c 100644 --- a/support/makebin/makebin.c +++ b/support/makebin/makebin.c @@ -1,8 +1,16 @@ /** @name makebin - turn a .ihx file into a binary image. */ +#include #include #include #include +#include + +#if defined(__BORLANDC__) || defined(__MINGW32__) || defined(__CYGWIN__) + #include + #include +#endif + typedef unsigned char BYTE; @@ -22,25 +30,62 @@ int getbyte(char **p) return (getnibble(p) << 4) | getnibble(p); } +void usage(void) +{ + fprintf(stderr, + "makebin: convert a Intel IHX file to binary.\n" + "Usage: makebin [-p] [-s romsize] [-h]\n"); +} + +void fixStdout(void) +{ + #if defined(__BORLANDC__) || defined(__MINGW32__) || defined(__CYGWIN__) + setmode(fileno(stdout), O_BINARY); + #endif +} + + int main(int argc, char **argv) { - int opt; - int size = 32768; + int size = 32768, pack = 0, real_size = 0; BYTE *rom; + size_t res; char line[256]; char *p; - while ((opt = getopt(argc, argv, "s:h"))!=-1) { - switch (opt) { + argc--; + argv++; + + fixStdout(); + + while (argc--) { + if (**argv != '-') { + usage(); + return -1; + } + switch (argv[0][1]) { case 's': - size = atoi(optarg); + if (argc < 1) { + usage(); + return -1; + } + argc--; + argv++; + size = atoi(*argv); break; case 'h': - printf("makebin: convert a Intel IHX file to binary.\n" - "Usage: %s [-s romsize] [-h]\n", argv[0]); + usage(); return 0; + case 'p': + pack = 1; + break; + default: + usage(); + return -1; } + argv++; } + rom = malloc(size); if (rom == NULL) { fprintf(stderr, "error: couldn't allocate room for the image.\n"); @@ -64,7 +109,18 @@ int main(int argc, char **argv) if (addr < size) rom[addr++] = getbyte(&p); } + + if (addr > real_size) + real_size = addr; + } + + if (pack) { + res = fwrite(rom, 1, real_size, stdout); + assert(res == real_size); + } else { + res = fwrite(rom, 1, size, stdout); + assert(res == size); } - fwrite(rom, 1, size, stdout); + return 0; }