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

  1. /*                             -*- Mode: Elec-C -*- 
  2.  * toglclr.c -- utility to toggle the program load flags in the .prg header.
  3.  *
  4.  *              usage: toglclr [options] file file ...
  5.  * 
  6.  * Author          : J.R. Bammi, modified by F. Ridderbusch
  7.  * Created On      : Some time ago.
  8.  * Last Modified By: J.R. Bammi
  9.  * Last Modified On: Fri Sep 25 12:00:28 1992
  10.  * Update Count    : 6
  11.  * Status          : Unknown, Use with caution!
  12.  */
  13.  
  14. /* HISTORY 
  15.  * 25-Sep-1992          ++jrb
  16.  *    Added support for cross-environment with support for 
  17.  *    WORD_ALIGNED hosts.
  18.  *
  19.  * 13-Sep-1992        Frank Ridderbusch    
  20.  *    This program was originally written by J.R. Bammi
  21.  *    (bammi@cadence.com) to toggle the clear TPA above BSS flag
  22.  *    introduced with TOS 1.4. I extended it to also handle the additional
  23.  *    program flags introduced with the TOS versions for the STE and TT.
  24.  *
  25.  * 31-Oct-1992        Thomas Schulze
  26.  *    Added code for MiNT 0.96 (and up) shared text feature. -fshare
  27.  *    will switch this flag.
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <unixlib.h>
  33. #include <string.h>
  34. #include <st-out.h>
  35.  
  36. #ifndef FILENAME_MAX
  37. #define FILENAME_MAX 1024
  38. #endif
  39.  
  40. int flags_to_toggle = 0;
  41.  
  42. #ifdef WORD_ALIGNED
  43.  
  44. #define ckread(addr, bytes, fd) if(read(fd, addr, bytes) != bytes) return 1;
  45. #define ckwrite(addr, bytes, fd) if(write(fd, addr, bytes) != bytes) return 1;
  46.  
  47. int readhead(h, fd)
  48. struct aexec *h;
  49. int fd;
  50. {
  51.     short i;
  52.     long j;
  53.     int k;
  54.     
  55.     ckread(&i, 2, fd);
  56.     h->a_magic = i;
  57.     ckread(&j, 4, fd);
  58.     h->a_text = j;
  59.     ckread(&j, 4, fd);
  60.     h->a_data = j;
  61.     ckread(&j, 4, fd);
  62.     h->a_bss = j;
  63.     ckread(&j, 4, fd);
  64.     h->a_syms = j;
  65.     ckread(&j, 4, fd);
  66.     h->a_AZero1 = j;
  67.     ckread(&j, 4, fd);
  68.     h->a_ldflgs = j;
  69.     ckread(&i, 2, fd);
  70.     h->a_isreloc = i;
  71.  
  72.     return 0;
  73. }
  74.  
  75. int writehead(h, fd)
  76. struct aexec *h;
  77. int fd;
  78. {
  79.     short i;
  80.     long j;
  81.     int k;
  82.     
  83.     i = h->a_magic;
  84.     ckwrite(&i, 2, fd);
  85.     j = h->a_text;
  86.     ckwrite(&j, 4, fd);
  87.     j = h->a_data;
  88.     ckwrite(&j, 4, fd);
  89.     j = h->a_bss;
  90.     ckwrite(&j, 4, fd);
  91.     j = h->a_syms;
  92.     ckwrite(&j, 4, fd);
  93.     j = h->a_AZero1;
  94.     ckwrite(&j, 4, fd);
  95.     j = h->a_ldflgs;
  96.     ckwrite(&j, 4, fd);
  97.     i = h->a_isreloc;
  98.     ckwrite(&i, 2, fd);
  99.  
  100.     return 0;
  101. }
  102.  
  103. #else
  104.  
  105. #define readhead(addr, fd) \
  106.  (read(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  107.  
  108. #define writehead(addr, fd) \
  109.  (write(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  110.  
  111. #endif /* WORD_ALIGNED */
  112.  
  113. int toggle (fd, fn)
  114. int fd;
  115. char *fn;
  116. {
  117.     struct aexec head;
  118.     unsigned long t;
  119.     char *ptr;
  120.     
  121.     if(readhead(&head, fd))
  122.     {
  123.     perror(fn);
  124.     return 4;
  125.     }
  126.     if(head.a_magic != CMAGIC)
  127.     {
  128.     fprintf(stderr,"%s: Invalid magic number %x\n", fn, head.a_magic);
  129.     return 8;
  130.     }
  131.  
  132.     t = head.a_AZero2;
  133.     if (flags_to_toggle)
  134.     {
  135.         head.a_AZero2 ^= flags_to_toggle;
  136.         lseek(fd, 0L, SEEK_SET);
  137.     if(writehead(&head, fd))
  138.         {
  139.         perror(fn);
  140.         return 16;
  141.         }
  142.     }
  143.  
  144.     printf("%s:            `fast load' bit was %d is now %d\n", fn, 
  145.        (int)((t & F_FASTLOAD) == F_FASTLOAD),
  146.        (int)((head.a_AZero2 & F_FASTLOAD) == F_FASTLOAD));
  147.  
  148.     printf("%s:      `run in fast ram' bit was %d is now %d\n", fn, 
  149.        (int)((t & F_ALTLOAD) == F_ALTLOAD),
  150.        (int)((head.a_AZero2 & F_ALTLOAD) == F_ALTLOAD));
  151.  
  152.     printf("%s: `malloc from fast ram' bit was %d is now %d\n", fn, 
  153.        (int)((t & F_ALTALLOC) == F_ALTALLOC),
  154.        (int)((head.a_AZero2 & F_ALTALLOC) == F_ALTALLOC));
  155.  
  156.     printf("%s: `shared text' bit was %d is now %d\n", fn,
  157.         (int)((t & F_SHTEXT) == F_SHTEXT),
  158.         (int)((head.a_AZero2 & F_SHTEXT) == F_SHTEXT));
  159.  
  160.     return 0;
  161. }
  162.  
  163. int main(argc, argv)
  164. int argc;
  165. char **argv;
  166. {
  167.     int fd;
  168.     int status = 0;
  169.     char fn[FILENAME_MAX];
  170.     
  171.     if(argc < 2)
  172.     {
  173.     fprintf(stderr, "usage: toglclr [options] file file .....\n\n");
  174.     fprintf(stderr, "options: -fload = toggle `fast load' bit\n");
  175.     fprintf(stderr, "         -frun  = toggle `load program into fast ram' bit\n");
  176.     fprintf(stderr, "         -fram  = toggle `malloc from fast ram' bit\n");
  177.     fprintf(stderr, "         -fshare= toggle `shared text' bit\n\n");
  178.     fprintf(stderr, "without options the current state is reported.\n");
  179.     exit(1);
  180.     }
  181.  
  182.     while (**++argv == '-')
  183.     {
  184.         if (!strcmp(*argv, "-fload"))
  185.         flags_to_toggle |= F_FASTLOAD;
  186.         if (!strcmp(*argv, "-frun"))
  187.         flags_to_toggle |= F_ALTLOAD;
  188.         if (!strcmp(*argv, "-fram"))
  189.         flags_to_toggle |= F_ALTALLOC;
  190.     if (!strcmp(*argv, "-fshare"))
  191.         flags_to_toggle |= F_SHTEXT;
  192.     --argc;
  193.     }
  194.  
  195.     while(--argc > 0)
  196.     {
  197.     (void) strcpy(fn, *argv++);
  198.         if((fd = open(fn, 2)) < 0)
  199.         {
  200.         perror(fn);
  201.         continue;
  202.         }
  203.     status |= toggle(fd, fn);
  204.     if(close(fd))
  205.     {
  206.         perror(fn);
  207.         exit(2);
  208.     }
  209.     }
  210.     
  211.     return status;
  212. }
  213.