* as/z80/z80mch.c: fixed bug #1704376: missing as-z80 errors
[fw/sdcc] / support / makebin / makebin.c
index c44c6c079ec6576660e058f5af7652ac286f3f5a..83d4ec6990991627f4fd0a36cbfd6f1a230e881d 100644 (file)
@@ -3,6 +3,13 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
+
+#if defined(__BORLANDC__) || defined(__MINGW32__) || defined(__CYGWIN__)
+  #include <fcntl.h>
+  #include <io.h>
+#endif
+
 
 typedef unsigned char BYTE;
 
@@ -22,30 +29,61 @@ 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, pack = 0, real_size = 0;
     BYTE *rom;
     char line[256];
     char *p;
 
-    while ((opt = getopt(argc, argv, "ps: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 [-p] [-s romsize] [-h]\n", argv[0]);
+            usage();
            return 0;
        case 'p':
            pack = 1;
            break;
        default:
-           return 1;
+            usage();
+            return -1;
        }
+        argv++;
     }
+
     rom = malloc(size);
     if (rom == NULL) {
        fprintf(stderr, "error: couldn't allocate room for the image.\n");