home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / PRG2OUT.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  3KB  |  162 lines

  1. /*
  2.  * prg2out - convert gemdos .prg to minix a.out
  3.  *    info derived from
  4.  *        minixcvt.c - thanks frans (meulenbr@cst.prl.philips.nl)
  5.  *    and    (re-hacked) relmix.c provided with minix St 
  6.  *        ++jrb (bammi@sargsun.ces.cwru.edu)
  7.  */
  8. #include "st-out.h"
  9.  
  10. extern long    lseek();
  11.  
  12. struct aexec    head;
  13.  
  14. char        buff[16384];
  15. int        fi, fo;
  16. char        *progname;
  17. char        *chmemstr;
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char *argv[];
  22. {
  23.     register int    part, n;
  24.     register long    left;
  25.  
  26.     progname = argv[0];
  27.     if (argc > 1)
  28.         switch (argv[1][0]) { 
  29.         case '-':
  30.         case '+':
  31.         case '=':
  32.             chmemstr = argv[1];
  33.             argc--;
  34.             argv++;
  35.         }
  36.     if (argc != 3)
  37.         error('f', "Usage: %s [+-= amount] infile outfile", progname);
  38.     if ((fi = open(argv[1], 0)) < 0)
  39.         error('f',"Couldn't open %s for input",argv[1]);
  40.     if ((fo = creat(argv[2], 0666)) < 0)
  41.         error('f',"Couldn't open %s for output",argv[2]);
  42.  
  43.     /*
  44.      * read header and copy to output
  45.      */
  46.     if (read(fi, &head, sizeof(struct aexec)) != sizeof(struct aexec))
  47.         error('f', "read error (magic)");
  48.     if (head.a_magic != CMAGIC)
  49.         error('f', "bad magic %04x, must be %04x",
  50.             head.a_magic, CMAGIC);
  51.     if (head.a_isreloc != 0)
  52.         error('f', "no relocation info present");
  53.  
  54.     /* write out minix head */
  55.     minixhead();
  56.  
  57.     /*
  58.      * copy text+data
  59.      */
  60.     left = head.a_text + head.a_data;
  61.     part = sizeof(buff);
  62.     while (left) {
  63.         n = left > part ? part : left;
  64.         if (read(fi, buff, n) != n)
  65.             error('f', "read error (text+data)");
  66.         if (write(fo, buff, n) != n)
  67.             error('f', "write error(text+data)");
  68.         left -= n;
  69.     }
  70.  
  71.     /*
  72.      * skip symbols
  73.      */
  74.     if (head.a_syms > 0)
  75.         if (lseek(fi, head.a_syms, 1) != 
  76.                  (sizeof(struct aexec) + head.a_text + head.a_data + head.a_syms))
  77.             error('f', "seek error");
  78.  
  79.     /*
  80.      * copy relocation info
  81.      */
  82.     while((n = read(fi, buff, sizeof(buff))) > 0)
  83.         if(write(fo, buff, n) != n)
  84.             error('f',"write error(reloc)");
  85.     if(n != 0)
  86.         error('f', "read error(reloc)");
  87.     close(fi); close(fo);
  88. }
  89.  
  90. minixhead()
  91. {
  92.     long        mh[8];
  93.     char        mb[32];
  94.     long        stack;
  95.     long        chmem();
  96.  
  97.     mh[0] = 0x04100301L;
  98.     mh[1] = 0x00000020L;
  99.     mh[2] = head.a_text;
  100.     mh[3] = head.a_data;
  101.     mh[4] = head.a_bss;
  102.     mh[5] = 0;
  103.     stack = 0x00010000L - (mh[3] + mh[4]);
  104.     if ((mh[0] & 0x00200000L) == 0)        /* not SEPARATE */
  105.         stack -= mh[2];
  106.     while (stack < 0)
  107.         stack += 0x00010000L;
  108.     if (chmemstr)
  109.         stack = chmem(chmemstr, stack);
  110.     printf("%ld bytes assigned to stack+malloc area\n", stack);
  111.     mh[6] = stack + (mh[3] + mh[4]);
  112.     if ((mh[0] & 0x00200000L) == 0)        /* not SEPARATE */
  113.         mh[6] += mh[2];
  114.     mh[7] = 0;
  115.     putstruc((char *)mh, "M44444444", mb);
  116.     if (write(fo, mb, sizeof(mb)) != sizeof(mb))
  117.         error('f', "write error(head)");
  118. }
  119.  
  120. long chmem(str, old)
  121. char *str;
  122. long old;
  123. {
  124.     register long num, new;
  125.     long atol();
  126.  
  127.     num = atol(str+1);
  128.     if (num == 0)
  129.         error('f', "bad chmem amount %s", str+1);
  130.     switch (str[0]) {
  131.     case '-':
  132.         new = old - num; break;
  133.     case '+':
  134.         new = old + num; break;
  135.     case '=':
  136.         new = num; break;
  137.     }
  138.     return(new);
  139. }
  140.  
  141. /* VARARGS2 */
  142. error(t,s,a,b,c,d,e,f,g,h,i,j)
  143. char t;
  144. char *s;
  145. {
  146.     printf("%s: ",progname);
  147.     printf(s,a,b,c,d,e,f,g,h,i,j);
  148.     printf("\n");
  149.     switch (t) {
  150.     case 'w':
  151.         return;
  152.     case 'f':
  153.         exit(1);
  154.     case 'a':
  155.         abort();
  156.     default:
  157.         error('w',"Illegal error type: '%c'",t);
  158.     }
  159. }
  160.  
  161. #include "putstruc.c"
  162.