home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / www / lemur / binary / 2b_bin2asm.lzx / Bin2Asm / Bin2Asm.c next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  2.0 KB  |  120 lines

  1. /*
  2.     $VER: Bin2Asm 0.5
  3.  
  4.     Bin2Asm by LeMUr/ Fire
  5.                  /& blabla
  6.  
  7.     FreeWare.
  8.  
  9.         This programm writes bin-data as assembler source-code.
  10.  
  11.         If You have only KickStart 1.3 then change all "Printf" (dos.library
  12.     v37++) to "printf" (linked from amiga.lib), but it'll make longer
  13.     program.
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. #include <proto/dos.h>
  21. #include <proto/exec.h>
  22.  
  23. #include <exec/memory.h>
  24.  
  25. // version
  26. const static char Version[]="\0$VER: Bin2Asm 0.5 (15.12.1995) by LeMUr/Fire & blabla\0";
  27.  
  28. // memory and long
  29. UBYTE *Bufor=NULL;
  30. ULONG Size=NULL;
  31.  
  32.  
  33. void out(int kod)
  34. // procedure frees memory and quits
  35. {
  36.     if(Bufor && Size)
  37.         FreeMem(Bufor, Size);
  38.  
  39.     exit(kod);
  40. } /* out() */
  41.  
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     UBYTE znak[10];
  46.     ULONG i=0;
  47.  
  48.     BPTR file, lock;
  49.     struct FileInfoBlock __aligned fib;
  50.  
  51.     /* let's check arguments... */
  52.     if(argc!=3)
  53.     {
  54.         Printf("I need two file-names as argument!\n");
  55.         out(5);
  56.     }
  57.  
  58.     /* read file into memory */
  59.     if(!(lock=Lock(argv[1], ACCESS_READ)))
  60.     {
  61.         Printf("Unable to lock \"%s\"\n", argv[1]);
  62.         out(25);
  63.     }
  64.  
  65.     if(!Examine(lock, &fib))
  66.     {
  67.         Printf("Unable to examine \"%s\"\n", argv[1]);
  68.         UnLock(lock);
  69.         out(30);
  70.     }
  71.  
  72.     if(!(Size=fib.fib_Size))
  73.     {
  74.         Printf("File couldn't be 0 bytes long!\n");
  75.         UnLock(lock);
  76.         out(32);
  77.     }
  78.  
  79.     if(!(Bufor=AllocMem(Size, MEMF_CHIP)))
  80.     {
  81.         Printf("Unable to allocate memory!\n");
  82.         UnLock(lock);
  83.         out(35);
  84.     }
  85.  
  86.     if(!(file=Open(argv[1], MODE_OLDFILE)))
  87.     {
  88.         Printf("Unable to open file \"%s\"\n", argv[1]);
  89.         UnLock(lock);
  90.         out(40);
  91.     }
  92.  
  93.     Read(file, Bufor, Size);
  94.     Close(file);
  95.     UnLock(lock);
  96.  
  97.     /* saving source code */
  98.     if(!(file=Open(argv[2], MODE_NEWFILE)))
  99.     {
  100.         Printf("Unable to open file \"%s\"\n", argv[2]);
  101.         UnLock(lock);
  102.         out(40);
  103.     }
  104.  
  105.     Write(file, "Data:\n", 6);
  106.     /* "conversion" */
  107.     for(i=0; i<Size; i++)
  108.     {
  109.         sprintf(znak, "\t$%X\n", Bufor[i]);
  110.         if(strlen(znak)==4)
  111.             sprintf(znak, "\t$0%X\n", Bufor[i]);
  112.         Write(file, "\tDC.B", 5);
  113.         Write(file, znak, strlen(znak));
  114.     }
  115.  
  116.     Close(file);
  117.  
  118.     out(0);
  119. } /* main() */
  120.