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

  1.  
  2. #define __USE_SYSBASE 1
  3.  
  4. #include <exec/types.h>
  5. #include <exec/execbase.h>
  6. #include <exec/memory.h>
  7. #include <dos/dosextens.h>
  8. #include <dos/rdargs.h>
  9.  
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <stdarg.h>
  13.  
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include "load.h"
  17.  
  18. #define TEMPLATE "EXE/A,OUT/A,ADDR=ADDRESS"
  19. #define OPT_EXE  0
  20. #define OPT_OUT  1
  21. #define OPT_ADDR 2
  22.  
  23. #define OPT_COUNT 3
  24.  
  25. #define MSG(x) Write(Output(),x,strlen(x))
  26.  
  27. MODULE m;
  28. int myprintf(char *ctl, ...);
  29.  
  30. unsigned long parsenum(char *n)
  31. {
  32.    long l;
  33.  
  34.    if(n[0] == '0' && toupper(n[1]) == 'X')
  35.       stch_l(n+2, &l);
  36.    else if(n[0] == '$')
  37.       stch_l(n+1, &l);
  38.    else
  39.       stcd_l(n, &l);
  40.  
  41.    return (unsigned long)l;
  42. }
  43.  
  44. int main(void)
  45. {
  46.    int i;
  47.    long opts[OPT_COUNT];
  48.    struct RDArgs *rdargs;
  49.    unsigned long addr = 0L;
  50.    BPTR fp;
  51.  
  52.    if(SysBase->LibNode.lib_Version < 37)
  53.    {
  54.       MSG("Requires AmigaDOS Version 2.0 or later\n");
  55.       return 20;
  56.    }
  57.  
  58.    memset(opts, 0, sizeof(opts));
  59.  
  60.    if(!(rdargs = ReadArgs(TEMPLATE, opts, NULL)))
  61.    {
  62.       PrintFault(IoErr(), NULL);
  63.       return(99);
  64.    }
  65.  
  66.    if(opts[OPT_ADDR]) addr = parsenum((char *)opts[OPT_ADDR]);
  67.  
  68.    if(i=Load(&m, (char *)opts[OPT_EXE], addr)) return 20;
  69.  
  70.    if(!(fp=Open((STRPTR)opts[OPT_OUT], MODE_NEWFILE)))
  71.    {
  72.       MSG("Can't open \"");
  73.       MSG((APTR)opts[OPT_OUT]);
  74.       MSG("\" for output\n");
  75.       return(20);
  76.    }
  77.  
  78.    for(i=0; i<m.hnum; i++)
  79.       Write(fp, m.hunks[i].data, m.hunks[i].size*4);
  80.  
  81.    UnLoad(&m);
  82.  
  83.    Close(fp);
  84.  
  85.    return 0;
  86. }
  87.  
  88. int myprintf(char *ctl, ...)
  89. {
  90.    char buffer[256];
  91.    va_list args;
  92.  
  93.    va_start(args, ctl);
  94.  
  95.    /*********************************************************/
  96.    /* NOTE: The string below is actually CODE that copies a */
  97.    /*       value from d0 to A3 and increments A3:          */
  98.    /*                                                       */
  99.    /*          move.b d0,(a3)+                              */
  100.    /*          rts                                          */
  101.    /*                                                       */
  102.    /*       It is essentially the callback routine needed   */
  103.    /*       by RawDoFmt.                                    */
  104.    /*********************************************************/
  105.  
  106.    RawDoFmt(ctl, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  107.  
  108.    va_end(args);
  109.  
  110.    return((int)strlen(buffer));
  111. }
  112.