home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / dismem / dismem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  2.7 KB  |  133 lines

  1.  
  2. #define __USESYSBASE
  3. #include <exec/types.h>
  4. #include <exec/execbase.h>
  5. #include <exec/memory.h>
  6. #include <dos/dosextens.h>
  7. #include <dos/rdargs.h>
  8. #include <dos/dostags.h>
  9. #include <stdarg.h>
  10.  
  11. #include <string.h>
  12.  
  13. #include <proto/dos.h>
  14. #include <proto/exec.h>
  15.  
  16. #define TEMPLATE "FILE/A"
  17. #define OPT_FILE 0
  18.  
  19. #define OPT_COUNT 1
  20.  
  21. #define MSG(x) Write(Output(),x,strlen(x))
  22.  
  23. #define HEADER  "      section text,code\n"
  24. #define TRAILER "      END\n"
  25.  
  26. int mysprintf(char *buffer, char *ctl, ...);
  27.  
  28. int __saveds dismem(void)
  29. {
  30.    int i, j;
  31.    BPTR lock = 0L;
  32.    long opts[OPT_COUNT];
  33.    struct RDArgs *rdargs;
  34.    BPTR fp = 0L, ofp = 0L;
  35.    long buf[256];
  36.    char obuf[80];
  37.    struct Library *DOSBase = OpenLibrary("dos.library", 36L);
  38.  
  39.    if(!DOSBase) return 20;
  40.  
  41.    if(!(rdargs = ReadArgs(TEMPLATE, opts, NULL)))
  42.    {
  43.       PrintFault(IoErr(), NULL);
  44.       return(20);
  45.    }
  46.  
  47.    if(!(fp=Open((STRPTR)opts[OPT_FILE], MODE_OLDFILE)))
  48.    {
  49.       PrintFault(IoErr(), NULL);
  50.       return(20);
  51.    }
  52.  
  53.    if(!(ofp=Open("t:dismem.a", MODE_NEWFILE)))
  54.    {
  55.       PrintFault(IoErr(),NULL);
  56.       goto cleanup;
  57.    }
  58.  
  59.    Write(ofp, HEADER, strlen(HEADER));
  60.  
  61.    while((i=Read(fp,buf,sizeof(buf))) > 0)
  62.    {
  63.       if(i<0)
  64.       {
  65.          PrintFault(IoErr(), NULL);
  66.          goto cleanup;
  67.       }
  68.       for(j=0, i/=4; j<i; j++)
  69.       {
  70.          mysprintf(obuf, "      dc.l $%08lx\n", buf[j]);
  71.          Write(ofp, obuf, strlen(obuf));
  72.       }
  73.    }
  74.  
  75.    Write(ofp, TRAILER, strlen(TRAILER));
  76.  
  77.    Close(ofp);
  78.    ofp = NULL;
  79.  
  80.    lock = CurrentDir(Lock("T:", SHARED_LOCK));
  81.  
  82.    i = SystemTags("asm dismem.a", SYS_UserShell, -1, TAG_DONE);
  83.  
  84.    UnLock(CurrentDir(lock));
  85.  
  86.    if(i)
  87.    {
  88.       MSG("Assembler failed!\n");
  89.       goto cleanup;
  90.    }
  91.  
  92.    if(SystemTags("omd t:dismem.o >dismem.omd", SYS_UserShell, -1, TAG_DONE))
  93.    {
  94.       MSG("OMD failed!\n");
  95.       goto cleanup;
  96.    }
  97.  
  98.    MSG("Output is in \"dismem.omd\"\n");
  99.  
  100. cleanup:
  101.  
  102.    if(ofp) Close(ofp);
  103.  
  104.    if(fp) Close(fp);
  105.  
  106.    return 0;
  107. }
  108.  
  109. int mysprintf(char *buffer, char *ctl, ...)
  110. {
  111.    va_list args;
  112.  
  113.    va_start(args, ctl);
  114.  
  115.    /*********************************************************/
  116.    /* NOTE: The string below is actually CODE that copies a */
  117.    /*       value from d0 to A3 and increments A3:          */
  118.    /*                                                       */
  119.    /*          move.b d0,(a3)+                              */
  120.    /*          rts                                          */
  121.    /*                                                       */
  122.    /*       It is essentially the callback routine needed   */
  123.    /*       by RawDoFmt.                                    */
  124.    /*********************************************************/
  125.  
  126.    RawDoFmt(ctl, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  127.  
  128.    va_end(args);
  129.  
  130.    return((int)strlen(buffer));
  131. }
  132.  
  133.