home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / bootunix.lzh / bf22 / BootFlag.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-19  |  8.3 KB  |  318 lines

  1. /*
  2. ** FreeWare! by John Edward Mosley, jem3@engr.uark.edu
  3. ** BootFlag.c - Allows manipulation of an 32-bit flag register in a file on a
  4. ** given disk (usually a Workbench disk) so that bit pattern comparisons of
  5. ** flags can set the Shell script condition to Warn of the patterns match.
  6. */
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <stdlib.h>
  10.  
  11. #define VERSION_INFO "BootFlag version 2.1 for Unix (FreeWare) by John Mosley 8-31-1992\n"
  12.  
  13. typedef char *CHARPTR;
  14.  
  15. void parse_flags(char *, unsigned *, unsigned *);
  16. int return_status(unsigned, unsigned);
  17. void print_flags(char *);
  18. void set_status(char *, unsigned);
  19. unsigned atoui(CHARPTR *, int);
  20. char *strrev(char *);
  21. void Exit(int);
  22. void print_usage(void);
  23.  
  24. char *DEF_STAT_FILE, STAT_FILE[41];
  25.  
  26. void main(int argc, char *argv[]) {
  27.     unsigned ONflags, OFFflags;
  28.  
  29.     DEF_STAT_FILE = getenv("BF_FILE");
  30.     if(*argv[argc - 1] == '-' && *(argv[argc - 1] + 1) == 'f') {
  31.         strcpy(STAT_FILE, argv[argc - 1] + 2);
  32.         argc--;
  33.     } /* if */
  34.     else strcpy(STAT_FILE, DEF_STAT_FILE);
  35.     switch(argc) {
  36.         case 3:
  37.             if(strcmp(argv[1], "check") == 0) {
  38.                 parse_flags(argv[2], &ONflags, &OFFflags);
  39.                 if(return_status(ONflags, OFFflags))
  40.                     Exit(1);
  41.                 else
  42.                     Exit(0);
  43.             } /* if */
  44.             else if(*argv[1] == 'p' || *argv[1] == 'P')
  45.                 print_flags(argv[2]);
  46.             else {
  47.                 parse_flags(argv[2], &ONflags, &OFFflags);
  48.                 set_status(argv[1], ONflags);
  49.             } /* else */
  50.             break;
  51.         case 2:
  52.             if(*argv[1] == 'p' || *argv[1] == 'P')
  53.                 print_flags(NULL);
  54.             break;
  55.         default:
  56.             printf("BootFlag: Wrong number of arguments\n");
  57.             print_usage();
  58.     } /* switch */
  59. } /* main() */
  60.  
  61. void parse_flags(char *strptr, unsigned *ONflags, unsigned *OFFflags) {
  62.     int sign;
  63.     unsigned temp;
  64.     char num[41], *numptr;
  65.  
  66.     *ONflags = *OFFflags = 0;
  67.     while(*strptr) {
  68.         sign = 1;
  69.         /* check for a sign on the flags */
  70.         if(*strptr == '+')
  71.             strptr++;
  72.         else if(*strptr == '-') {
  73.             sign = 0;
  74.             strptr++;
  75.         } /* else if */
  76.         /* check to see if it's just a decimal value */
  77.         if(isdigit(*strptr)) {
  78.             numptr = num;
  79.             while(isdigit(*strptr)) *(numptr++) = *(strptr++);
  80.             *numptr = NULL;
  81.             temp = (unsigned)atol(num);
  82.         } /* if */
  83.         /* check for a xhex, &oct, or %binary number & convert */    
  84.         else switch(*strptr) {
  85.             case 'x':    /* hexadecimal number */
  86.                 strptr++;
  87.                 temp = atoui(&strptr, 4);
  88.                 break;
  89.             case '&':    /* octal number */
  90.                 strptr++;
  91.                 temp = atoui(&strptr, 3);
  92.                 break;
  93.             case '%':    /* binary number */
  94.                 strptr++;
  95.                 temp = atoui(&strptr, 1);
  96.                 break;
  97.             default:
  98.                 printf("BootFlag: Unknown number system \"%c\"\n",
  99.                     *strptr);
  100.                 print_usage();
  101.         } /* else switch */
  102.         if(sign) *ONflags = temp;
  103.         else *OFFflags = temp;
  104.     } /* while */
  105. } /* parse_flags() */
  106.  
  107. int return_status(unsigned ONflags, unsigned OFFflags) {
  108.     unsigned status;
  109.     FILE *fptr;
  110.     int check_result;
  111.  
  112.     if((fptr = fopen(STAT_FILE, "r")) == NULL) {
  113.         printf("BootFlag: Cannot access status file\n");
  114.         Exit(-1);
  115.     } /* if */
  116.     if(fread((char *)&status, sizeof(unsigned), 1, fptr) == EOF) {
  117.         fclose(fptr);
  118.         printf("BootFlag: Status file corrupt?\n");
  119.         Exit(-1);
  120.     } /* if */
  121.     fclose(fptr);
  122.     check_result = 1;
  123.     if(ONflags) check_result = ((status & ONflags) == ONflags);
  124.     if(OFFflags) check_result &= ((~status & OFFflags) == OFFflags);
  125.     return(check_result);
  126. } /* return_status() */
  127.  
  128. void print_flags(char *format) {
  129.     unsigned status, bit;
  130.     FILE *fptr;
  131.     char printing_zeros = 0;
  132.  
  133.     if((fptr = fopen(STAT_FILE, "r")) == NULL) {
  134.         printf("BootFlag: Cannot access status file\n");
  135.         Exit(-1);
  136.     } /* if */
  137.     if(fread((char *)&status, sizeof(unsigned), 1, fptr) == EOF) {
  138.         fclose(fptr);
  139.         printf("BootFlag: Status file corrupt?\n");
  140.         Exit(-1);
  141.     } /* if */
  142.     fclose(fptr);
  143.     if(format) {    /* determine format specification */
  144.         switch(*format) {
  145.             case 'x':
  146.                 printf("Boot flags are set at x%x.\n", status);
  147.                 break;
  148.             case '&':
  149.                 printf("Boot flags are set at &%o.\n", status);
  150.                 break;
  151.             case '%':
  152.                 printf("Boot flags are set at %%");
  153.                 for(bit = 0x80000000; bit; bit >>= 1)
  154.                     if(status & bit) {
  155.                         printf("1");
  156.                         printing_zeros = 1;
  157.                     } /* if */
  158.                     else if(printing_zeros || bit == 1)
  159.                         printf("0");
  160.                 printf(".\n");
  161.                 break;
  162.             default:
  163.                 printf("pmod: Unknown display format \"%c\"\n", *format);
  164.         } /* switch */
  165.     } /* if */
  166.     else
  167.         printf("Boot flags are set at %u.\n", status);
  168. } /* print_flags() */
  169.  
  170. void set_status(char *operation, unsigned flags) {
  171.     unsigned status;
  172.     FILE *fptr;
  173.  
  174.     if(flags < 0 || flags > 0xffffffff) {
  175.           printf("BootFlag: Given flag out of range\n");
  176.         print_usage();
  177.     } /* if */
  178.     if(*operation == '=') {
  179.         if((fptr = fopen(STAT_FILE, "w")) == NULL) {
  180.             printf("BootFlag: Cannot access status file\n");
  181.             Exit(-1);
  182.         } /* if */
  183.     } /* if */
  184.     else {
  185.         if((fptr = fopen(STAT_FILE, "r+")) == NULL) {
  186.             printf("BootFlag: Cannot access status file\n");
  187.             Exit(-1);
  188.         } /* if */
  189.         if(fread((char *)&status, sizeof(unsigned), 1, fptr) == EOF) {
  190.             fclose(fptr);
  191.             printf("BootFlag: Status file corrupt?\n");
  192.             Exit(-1);
  193.         } /* if */
  194.     } /* else */
  195.     switch(*operation) {
  196.         case 's':
  197.         case 'S':
  198.             status |= flags;
  199.             break;
  200.         case 'c':
  201.         case 'C':
  202.             status &= ~flags;
  203.             break;
  204.         case 't':
  205.         case 'T':
  206.             status ^= flags;
  207.             break;
  208.         case '=':
  209.             status = flags;
  210.             break;
  211.         default:
  212.             printf("BootFlag: Unknown command \"%s\"\n", operation);
  213.             fclose(fptr);
  214.             print_usage();
  215.     } /* switch */
  216.     if(fseek(fptr, 0, 0) != NULL) {
  217.         fclose(fptr);
  218.         printf("BootFlag: Status file corrupt?\n");
  219.         Exit(-1);
  220.     } /* if */
  221.     if(fwrite((char *)&status, sizeof(unsigned), 1, fptr) == NULL) {
  222.         fclose(fptr);
  223.         printf("BootFlag: Unable to update flags in status file\n");
  224.         Exit(-1);
  225.     } /* if */
  226.     fclose(fptr);
  227. } /* set_status() */
  228.  
  229. unsigned atoui(CHARPTR *lineptr, int shift_base) {
  230.     int digit, place = 1;
  231.     unsigned value, sum = 0;
  232.     char num[41], *ptr;
  233.  
  234.     /* place the current number from the command-line string into num[] */
  235.     ptr = num;
  236.     while(**lineptr && **lineptr != '+' && **lineptr != '-')
  237.         *(ptr++) = *((*lineptr)++);
  238.     *ptr = NULL;
  239.     ptr = num;
  240.     /* reverse string to start conversion from Least Sig digit */
  241.     strrev(ptr);
  242.     /* loop through each digit and convert to integer */
  243.     while(*ptr) {
  244.         *ptr = toupper(*ptr);
  245.         switch(shift_base) {
  246.             case 4:
  247.                 if(!isxdigit(*ptr)) {
  248.                     printf("BootFlag: Bad hexadecimal digit \"%c\"\n", *ptr);
  249.                     print_usage();
  250.                 } /* if */
  251.                 digit = (isdigit(*ptr) != 0)*(*ptr - '0') + (isalpha(*ptr))*(*ptr - 'A' + 10);
  252.                 break;
  253.             case 3:
  254.                 if(!isdigit(*ptr) || *ptr < '0' || *ptr > '7') {
  255.                     printf("BootFlag: Bad octal digit \"%c\"\n", *ptr);
  256.                     print_usage();
  257.                 } /* if */
  258.                 digit = (*ptr - '0');
  259.                 break;
  260.             case 1:
  261.                 if(*ptr != '0' && *ptr != '1') {
  262.                     printf("BootFlag: Bad binary digit \"%c\"\n", *ptr);
  263.                     print_usage();
  264.                 } /* if */
  265.                 digit = (*ptr - '0');
  266.                 break;
  267.             default:
  268.                 printf("BootFlag: atoui: Unknown base %d\n", shift_base);
  269.                 print_usage();
  270.         } /* switch */
  271.         value = digit*place;
  272.         place <<= shift_base;
  273.         sum += value;
  274.         ptr++;
  275.     } /* while */
  276.     return(sum);
  277. } /* atoui() */
  278.  
  279. char *strrev(char *string) {
  280.     char *front = string, *end = string, temp;
  281.  
  282.     if(string == NULL || *string == NULL) return(string);
  283.     while(*end) end++;
  284.     end--;
  285.     while(front < end) {
  286.         temp = *front;
  287.         *front++ = *end;
  288.         *end-- = temp;
  289.     } /* while */
  290.     return(string);
  291. } /* strrev() */
  292.  
  293. void Exit(int return_code) {
  294.     printf("%d", return_code);
  295.     exit();
  296. } /* Exit() */
  297.  
  298. void print_usage(void) {
  299.     printf(VERSION_INFO);
  300.     printf("USAGE: BootFlag <check|print|set|clear|tog|=> <flags> [-f<file>]\n");
  301.     printf("\tflags : specifier representing 32 binary flags to manipulate\n");
  302.     printf("\t\tSyntax: [+|-][x|&|%]<digits>[<+|->[x|&|%]<digits>]\n");
  303.     printf("\t\t+: check if flags are ON, -: check if flags are OFF\n");
  304.     printf("\t\txhexadecimal#, &octal#, %binary#, or (none)decimal#\n");
  305.     printf("\tcheck : compare flags, return Warn if the given pattern matches\n");
  306.     printf("\tprint : print out current flag settings\n");
  307.     printf("\t\tUse [x|&|%] in place of <flags> for output format.\n");
  308.     printf("\tThe commands below only use the + flag value:\n");
  309.     printf("\tset   : set given flags to ON state\n");
  310.     printf("\tclear : set given flags to OFF state\n");
  311.     printf("\ttog   : toggle given flags\n");
  312.     printf("\t=     : set entire 32-bit register equal to given flags\n");
  313.     printf("\tFile option(s):\n");
  314.     printf("\t-f    : use a different register file (<file> required)\n");
  315.     printf("\tfile  : path and filename of the register file to use\n");
  316.     exit();
  317. } /* print_usage() */
  318.