home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / util / utlsrc37.zoo / toglclr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-30  |  6.3 KB  |  271 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: Frank Ridderbusch
  9.  * Last Modified On: Wed Apr 14 21:26:12 1993
  10.  * Update Count    : 18
  11.  * Status          : Unknown, Use with caution!
  12.  */
  13.  
  14. /* HISTORY 
  15.  * 14-Apr-1993        Frank Ridderbusch    
  16.  *    Added code to handle the memory protection bits under MultiTOS.
  17.  *    Changed output format slightly.
  18.  * 31-Oct-1992        Thomas Schulze
  19.  *    Added code for MiNT 0.96 (and up) shared text feature. -fshared
  20.  *    will switch this flag.
  21.  * 25-Sep-1992          ++jrb
  22.  *    Added support for cross-environment with support for 
  23.  *    WORD_ALIGNED hosts.
  24.  * 13-Sep-1992        Frank Ridderbusch    
  25.  *    This program was originally written by J.R. Bammi
  26.  *    (bammi@cadence.com) to toggle the clear TPA above BSS flag
  27.  *    introduced with TOS 1.4. I extended it to also handle the additional
  28.  *    program flags introduced with the TOS versions for the STE and TT.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <st-out.h>
  36.  
  37. #ifndef FILENAME_MAX
  38. #define FILENAME_MAX 1024
  39. #endif
  40.  
  41. /*
  42.  * ldflgs - stolen from MiNT mem.h
  43.  */
  44.  
  45. #define    F_PROTMODE    0xf0        /* protection mode bits */
  46. #define F_PROT_P    0x00        /* no read or write */
  47. #define F_PROT_G    0x10        /* any access OK */
  48. #define F_PROT_S    0x20        /* any super access OK */
  49. #define F_PROT_PR    0x30        /* any read OK, no write */
  50.  
  51. int flags_to_toggle = 0;
  52. int set_mp_flags = 0;
  53.  
  54. #ifdef WORD_ALIGNED
  55.  
  56. #define ckread(addr, bytes, fd) if(read(fd, addr, bytes) != bytes) return 1;
  57. #define ckwrite(addr, bytes, fd) if(write(fd, addr, bytes) != bytes) return 1;
  58.  
  59. int readhead(h, fd)
  60. struct aexec *h;
  61. int fd;
  62. {
  63.     short i;
  64.     long j;
  65.     int k;
  66.     
  67.     ckread(&i, 2, fd);
  68.     h->a_magic = i;
  69.     ckread(&j, 4, fd);
  70.     h->a_text = j;
  71.     ckread(&j, 4, fd);
  72.     h->a_data = j;
  73.     ckread(&j, 4, fd);
  74.     h->a_bss = j;
  75.     ckread(&j, 4, fd);
  76.     h->a_syms = j;
  77.     ckread(&j, 4, fd);
  78.     h->a_AZero1 = j;
  79.     ckread(&j, 4, fd);
  80.     h->a_ldflgs = j;
  81.     ckread(&i, 2, fd);
  82.     h->a_isreloc = i;
  83.  
  84.     return 0;
  85. }
  86.  
  87. int writehead(h, fd)
  88. struct aexec *h;
  89. int fd;
  90. {
  91.     short i;
  92.     long j;
  93.     int k;
  94.     
  95.     i = h->a_magic;
  96.     ckwrite(&i, 2, fd);
  97.     j = h->a_text;
  98.     ckwrite(&j, 4, fd);
  99.     j = h->a_data;
  100.     ckwrite(&j, 4, fd);
  101.     j = h->a_bss;
  102.     ckwrite(&j, 4, fd);
  103.     j = h->a_syms;
  104.     ckwrite(&j, 4, fd);
  105.     j = h->a_AZero1;
  106.     ckwrite(&j, 4, fd);
  107.     j = h->a_ldflgs;
  108.     ckwrite(&j, 4, fd);
  109.     i = h->a_isreloc;
  110.     ckwrite(&i, 2, fd);
  111.  
  112.     return 0;
  113. }
  114.  
  115. #else
  116.  
  117. #define readhead(addr, fd) \
  118.  (read(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  119.  
  120. #define writehead(addr, fd) \
  121.  (write(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  122.  
  123. #endif /* WORD_ALIGNED */
  124.  
  125. int toggle (fd, fn)
  126. int fd;
  127. char *fn;
  128. {
  129.     struct aexec head;
  130.     unsigned long t;
  131.     char *ptr;
  132.     
  133.     if(readhead(&head, fd))
  134.     {
  135.     perror(fn);
  136.     return 4;
  137.     }
  138.     if(head.a_magic != CMAGIC)
  139.     {
  140.     fprintf(stderr,"%s: Invalid magic number %x\n", fn, head.a_magic);
  141.     return 8;
  142.     }
  143.  
  144.     t = head.a_AZero2;
  145.     if (flags_to_toggle || set_mp_flags)
  146.     {
  147.         head.a_AZero2 &= ~F_PROTMODE;
  148.         head.a_AZero2 ^= flags_to_toggle;
  149.         lseek(fd, 0L, SEEK_SET);
  150.     if(writehead(&head, fd))
  151.         {
  152.         perror(fn);
  153.         return 16;
  154.         }
  155.     }
  156.  
  157.     printf("%s:\n\t           `fast load' bit was %d, is now %d.\n", fn, 
  158.        (int)((t & F_FASTLOAD) == F_FASTLOAD),
  159.        (int)((head.a_AZero2 & F_FASTLOAD) == F_FASTLOAD));
  160.  
  161.     printf("\t     `run in fast ram' bit was %d, is now %d.\n",
  162.        (int)((t & F_ALTLOAD) == F_ALTLOAD),
  163.        (int)((head.a_AZero2 & F_ALTLOAD) == F_ALTLOAD));
  164.  
  165.     printf("\t`malloc from fast ram' bit was %d, is now %d.\n",
  166.        (int)((t & F_ALTALLOC) == F_ALTALLOC),
  167.        (int)((head.a_AZero2 & F_ALTALLOC) == F_ALTALLOC));
  168.  
  169.     printf("\t         `shared text' bit was %d, is now %d.\n",
  170.         (int)((t & F_SHTEXT) == F_SHTEXT),
  171.         (int)((head.a_AZero2 & F_SHTEXT) == F_SHTEXT));
  172.  
  173.     switch (head.a_AZero2 & F_PROTMODE)
  174.     {
  175.         case F_PROT_P:
  176.             ptr = "private";
  177.         break;
  178.         case F_PROT_G:
  179.             ptr = "global";
  180.         break;
  181.         case F_PROT_S:
  182.             ptr = "super";
  183.         break;
  184.         case F_PROT_PR:
  185.             ptr = "private/readable";
  186.         break;
  187.     }
  188.     printf("\truns now with `%s' memory protection.\n", ptr);
  189.     return 0;
  190. }
  191.  
  192. int main(argc, argv)
  193. int argc;
  194. char **argv;
  195. {
  196.     int fd;
  197.     int tmp = 0;
  198.     int status = 0;
  199.     char fn[FILENAME_MAX];
  200.     
  201.     if(argc < 2)
  202.     {
  203.     fprintf(stderr, "usage: toglclr [options] file file .....\n\n");
  204.     fprintf(stderr, "options: -fload    = toggle `fast load' bit\n");
  205.     fprintf(stderr, "         -frun     = toggle `load program into fast ram' bit\n");
  206.     fprintf(stderr, "         -fram     = toggle `malloc from fast ram' bit\n");
  207.     fprintf(stderr, "         -fshare   = toggle `shared text' bit\n\n");
  208.     fprintf(stderr, "options for memory protection (only one of these allowed and\n");
  209.         fprintf(stderr, "        only sensible under MultiTOS):\n");
  210.     fprintf(stderr, "         -private  = \n");
  211.     fprintf(stderr, "         -global   = \n");
  212.     fprintf(stderr, "         -super    = \n");
  213.     fprintf(stderr, "         -readable = \n\n");
  214.     fprintf(stderr, "without options the current state is reported.\n");
  215.     exit(1);
  216.     }
  217.  
  218.     while (**++argv == '-')
  219.     {
  220.         if (!strcmp(*argv, "-fload"))
  221.         flags_to_toggle |= F_FASTLOAD;
  222.         if (!strcmp(*argv, "-frun"))
  223.         flags_to_toggle |= F_ALTLOAD;
  224.         if (!strcmp(*argv, "-fram"))
  225.         flags_to_toggle |= F_ALTALLOC;
  226.     if (!strcmp(*argv, "-fshare"))
  227.         flags_to_toggle |= F_SHTEXT;
  228.     if (!strcmp(*argv, "-private"))
  229.     {
  230.         tmp = F_PROT_P;
  231.         set_mp_flags = 1;
  232.     }
  233.     if (!strcmp(*argv, "-global"))
  234.     {
  235.         tmp = F_PROT_G;
  236.         set_mp_flags = 1;
  237.     }
  238.     if (!strcmp(*argv, "-super"))
  239.     {
  240.         tmp = F_PROT_S;
  241.         set_mp_flags = 1;
  242.     }
  243.     if (!strcmp(*argv, "-readable"))
  244.     {
  245.         tmp = F_PROT_PR;
  246.         set_mp_flags = 1;
  247.     }
  248.     --argc;
  249.     }
  250.     
  251.     flags_to_toggle |= tmp;
  252.  
  253.     while(--argc > 0)
  254.     {
  255.     (void) strcpy(fn, *argv++);
  256.         if((fd = open(fn, 2)) < 0)
  257.         {
  258.         perror(fn);
  259.         continue;
  260.         }
  261.     status |= toggle(fd, fn);
  262.     if(close(fd))
  263.     {
  264.         perror(fn);
  265.         exit(2);
  266.     }
  267.     }
  268.     
  269.     return status;
  270. }
  271.