home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / abslink / runtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.7 KB  |  171 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. int mysprintf(char *buffer, char *ctl, ...);
  24.  
  25. static char obuf[80];
  26.  
  27. int getfilesize(STRPTR);
  28.  
  29. struct DosLibrary *DOSBase;
  30.  
  31. /* Note: Since we link with no startup, this is where execution */
  32. /* begins.  Don't try to run this program from the WorkBench!   */
  33. int __saveds dotest(void)
  34. {
  35.    int i, memsize;
  36.    long opts[OPT_COUNT];
  37.    struct RDArgs *rdargs;
  38.    BPTR fp = 0L;
  39.    void *mem = NULL;
  40.    int (* __asm func)(register __a0 char *arg);
  41.    
  42.    DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L);
  43.  
  44.    if(!DOSBase)
  45.    {
  46.       if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 0L))
  47.       {
  48.          MSG("Requires AmigaDOS 2.0 or higher\n");
  49.          CloseLibrary(DOSBase);
  50.       }
  51.       return 20;
  52.    }
  53.  
  54.    if(!(rdargs = ReadArgs(TEMPLATE, opts, NULL)))
  55.    {
  56.       PrintFault(IoErr(), NULL);
  57.       return(20);
  58.    }
  59.  
  60.    if((memsize=getfilesize((STRPTR)opts[OPT_FILE])) < 0)
  61.       goto cleanup;
  62.  
  63. retry:
  64.    /* Note that this is only a GUESS as to the final size. */
  65.    mem = AllocMem(memsize, 0);
  66.    if(mem == NULL)
  67.    {
  68.       MSG("No memory!\n");
  69.       goto cleanup;
  70.    }
  71.  
  72.    mysprintf(obuf, "abslink %s t:abslink addr=%ld", opts[OPT_FILE], mem);
  73.    MSG(obuf);
  74.    MSG("\n");
  75.    if(SystemTags(obuf, SYS_UserShell, -1, TAG_DONE))
  76.    {
  77.       MSG("abslink failed!\n");
  78.       goto cleanup;
  79.    }
  80.  
  81.    if((i=getfilesize("t:abslink")) > memsize)
  82.    {
  83.       /* Oops, our guess was wrong */
  84.       MSG("RUNTEST guessed wrong mem size, trying again\n");
  85.       FreeMem(mem, memsize);
  86.       memsize = i;
  87.       goto retry;
  88.    }
  89.  
  90.    if(!(fp=Open("t:abslink", MODE_OLDFILE)))
  91.    {
  92.       MSG("Can't open abslink output file!\n");
  93.       goto cleanup;
  94.    }
  95.  
  96.    if(Read(fp, mem, i) <= 0)
  97.    {
  98.       PrintFault(IoErr(), "Can't read abslink file: ");
  99.       goto cleanup;
  100.    }
  101.  
  102.    Close(fp);
  103.    fp = NULL;
  104.  
  105. #pragma msg 147 ignore
  106.    func = mem;
  107.  
  108.    i = (*func)("test");
  109.    mysprintf(obuf, "Return code is %d\n", i);
  110.    MSG(obuf);
  111.  
  112. cleanup:
  113.  
  114.    if(mem) FreeMem(mem, memsize);
  115.  
  116.    if(fp) Close(fp);
  117.    
  118.    if(DOSBase) CloseLibrary(DOSBase);
  119.  
  120.    return 0;
  121. }
  122.  
  123. int __aligned getfilesize(STRPTR file)
  124. {
  125.    BPTR lock;
  126.    struct FileInfoBlock __aligned fib;
  127.  
  128.    if(!(lock = Lock(file, SHARED_LOCK)))
  129.    {
  130.       mysprintf(obuf, "Can't find file \"%s\"\n", file);
  131.       PrintFault(IoErr(),obuf);
  132.       return(-1);
  133.    }
  134.  
  135.    if(!Examine(lock, &fib))
  136.    {
  137.       PrintFault(IoErr(),NULL);
  138.       UnLock(lock);
  139.       return(-1);
  140.    }
  141.  
  142.    UnLock(lock);
  143.    return fib.fib_Size;
  144. }
  145.  
  146. int mysprintf(char *buffer, char *ctl, ...)
  147. {
  148.    va_list args;
  149.  
  150.    va_start(args, ctl);
  151.  
  152.    /*********************************************************/
  153.    /* NOTE: The string below is actually CODE that copies a */
  154.    /*       value from d0 to A3 and increments A3:          */
  155.    /*                                                       */
  156.    /*          move.b d0,(a3)+                              */
  157.    /*          rts                                          */
  158.    /*                                                       */
  159.    /*       It is essentially the callback routine needed   */
  160.    /*       by RawDoFmt.  THIS FILE MUST BE COMPILED WITH   */
  161.    /*       THE -cs OPTION OR THIS CODE WILL NOT WORK ON    */
  162.    /*       THE 68040 DUE TO INSTRUCTION CACHE!!!!!         */
  163.    /*********************************************************/
  164.  
  165.    RawDoFmt(ctl, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  166.  
  167.    va_end(args);
  168.  
  169.    return((int)strlen(buffer));
  170. }
  171.