home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / tools / bgi_tool / bh.c < prev    next >
Text File  |  1989-05-30  |  5KB  |  130 lines

  1.  
  2. /*
  3.     BH - writes header portion of BGI driver
  4.  
  5.     Copyright (c) 1988,89 Borland International
  6. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define VERSION     2        /* Version Number of header    */
  14. #define REVISION    0        /* Revision number of header    */
  15. #define MIN_VERSION    1        /* Minimum Version Number    */
  16. #define MIN_REVISION    1        /* Minimum Revision Number    */
  17. #define HEADER_SIZE    160        /* Size (in bytes) of header    */
  18.  
  19. FILE    *Ifile, *Ofile = stdout;
  20.  
  21. char mask[] = "pk\010\010BGI Device Driver (%s) %d.%02d - " __DATE__ "\r\n"
  22.           "Copyright (c) 1987,1989 Borland International\r\n";
  23.  
  24. char help[] = "BGI Driver Builder Copyright (c) 1987,1989 Borland International, Inc.\n\n"
  25.               "Usage is:  BH [input.BIN] [output.BGI] [drv_name]\n\n"
  26.           "    input.BIN is the DRIVER.BIN from EXE2BIN.\n"
  27.           "    output.BGI is the DRIVER.BGI file name.\n"
  28.           "    drv_name is the device name in ASCII (i.e. EGA or CGA)\n";
  29.  
  30. int    drv_num = 0;
  31.  
  32. void main( argc, argv )
  33. int argc;
  34. char *argv[];
  35. {
  36.   long int size, offset;
  37.   int i, j;
  38.   char name[80], *cptr;
  39.  
  40.   argv++;    argc--;         /* Skip over program name    */
  41.  
  42.   if( argc != 3 && argc != 4 ){     /* Must have input and output    */
  43.     fprintf( stderr, help );        /* Give user a help message    */
  44.     exit( 1 );                /* Leave the program        */
  45.     }
  46.  
  47.   strcpy( name, *argv++ );        /* Get input file name        */
  48.   cptr = strchr( name, '.' );           /* Is ther an extention?        */
  49.   if( cptr ) *cptr = '\0';              /* Cut extent if give           */
  50.   strcat( name, ".BIN" );               /* Add input file extention     */
  51.  
  52.   Ifile = fopen( name, "rb" );          /* Open input file              */
  53.   if( NULL == Ifile ){            /* Did the open suceed?     */
  54.     fprintf( stderr, "ERROR: Could not open input file %s.\n", *(argv-1) );
  55.     exit( 2 );                /* Leave the program        */
  56.     }
  57.  
  58.   strcpy( name, *argv++ );        /* Get input file name        */
  59.   cptr = strchr( name, '.' );           /* Is ther an extention?        */
  60.   if( cptr ) *cptr = '\0';              /* Cut extent if give           */
  61.   strcat( name, ".BGI" );               /* Add input file extention     */
  62.  
  63.   Ofile = fopen( name, "wb" );          /* Open output file             */
  64.   if( NULL == Ofile ){            /* Did the open suceed?     */
  65.     fprintf( stderr, "ERROR: Could not open output file %s.\n", *(argv-1) );
  66.     exit( 3 );                /* Leave the program        */
  67.     }
  68.  
  69.   strcpy( name, *argv++ );        /* Get driver name from line    */
  70.   strupr( name );            /* Convert name to uppercase    */
  71.  
  72. /*    The driver number is not needed for version 2 drivers, but is    */
  73. /*    allowed for version 1 compatability.                */
  74.  
  75.   if( argc == 4 )            /* Is driver number is present? */
  76.     drv_num = atoi( *argv++ );        /* convert driver number to bin */
  77.  
  78.   fseek( Ifile, 0L, SEEK_END );     /* Goto the end of the file    */
  79.   size = ftell( Ifile );        /* Read the length of the file    */
  80.   fseek( Ifile, 0L, SEEK_SET );     /* Goto the beginning of file    */
  81.  
  82.   fprintf( Ofile, mask, name, VERSION, REVISION );
  83.   putc( 0x00, Ofile );            /* Null terminate string in file*/
  84.   putc( 0x1a, Ofile );            /* Control Z terminate file    */
  85.  
  86.   putw( HEADER_SIZE, Ofile );        /* Write out the size of header */
  87.   putw( drv_num, Ofile );        /* Write out the driver number    */
  88.   putw( (int) size, Ofile );        /* Size (in bytes) of driver    */
  89.  
  90.   putc( VERSION, Ofile );        /* Write the version number    */
  91.   putc( REVISION, Ofile );        /* Write the revision number    */
  92.  
  93.   putc( MIN_VERSION, Ofile );        /* Write the version number    */
  94.   putc( MIN_REVISION, Ofile );        /* Write the revision number    */
  95.  
  96.   offset = ftell( Ofile );        /* Find location in output file */
  97.   for( i=(int)offset ; i<0x80 ; ++i ) putc( 0x00, Ofile );
  98.  
  99.   putw( HEADER_SIZE, Ofile );        /* Write out the size of header */
  100.   putw( drv_num, Ofile );        /* Write out the driver number    */
  101.   putw( (int) size, Ofile );        /* Size (in bytes) of driver    */
  102.  
  103.   putc( VERSION, Ofile );        /* Write the version number    */
  104.   putc( REVISION, Ofile );        /* Write the revision number    */
  105.  
  106.   putc( MIN_VERSION, Ofile );        /* Write the version number    */
  107.   putc( MIN_REVISION, Ofile );        /* Write the revision number    */
  108.  
  109.   name[8] = '\0';                       /* Cut name to 8 characters     */
  110.   j = strlen( name );            /* Get device driver int name    */
  111.   putc( j, Ofile );            /* Make string pascal format    */
  112.   for( i=0 ; i<j ; ++i ) putc( name[i], Ofile );
  113.  
  114.   size = ftell( Ofile );        /* How big is header so far    */
  115.   i = HEADER_SIZE - (int) size;     /* Determine # of pad bytes    */
  116.  
  117.   for( j=0 ; j<i ; ++j )        /* Pad header with zeros    */
  118.         putc( 0, Ofile );
  119.  
  120.   i = getc( Ifile );            /* Read source byte        */
  121.   while( !feof(Ifile) ){        /* Copy the input to output    */
  122.     putc( i, Ofile );            /* Write destination byte    */
  123.     i = getc( Ifile );            /* Read source byte        */
  124.     }
  125.  
  126.   fclose( Ifile );            /* Close file streams        */
  127.   fclose( Ofile );
  128.  
  129. }
  130.