home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_03 / 1003058a < prev    next >
Text File  |  1991-10-07  |  4KB  |  173 lines

  1. /* ----------------------------------------------------
  2.  *  LISTING 6
  3.  *
  4.  *  Filename:           ctl.c
  5.  *  Summary:            Test IOCTL program for tape
  6.  *                      device driver
  7.  *  Author:             T.W. Nelson
  8.  *  Compile options:    
  9.  *  Version:            1.00
  10.  *  Date:               29-Sep-1991
  11.  *  Notes:
  12.  *
  13.  * ------------------------------------------------- */
  14.  
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <sys\stat.h>
  19. #include <dos.h>
  20. #include <bios.h>
  21. #include "tape.h"
  22.  
  23. #define IOCTL_OK    0x4000      //ioctl check bit
  24. #define IM_A_DEVICE 0x0080      //device check bit
  25. #define IO_BUF_SIZE sizeof(CMDARG)
  26.  
  27. union REGS regs;
  28. struct SREGS sregs;
  29. char Dname[] = "TAPEXXXX";
  30. int ioHandle;
  31. CMDARG ioArg;
  32.  
  33. char *Menu[] = {
  34.     "",
  35.     "Power ON .............0",
  36.     "Power OFF ............1",
  37.     "Insert Cassette ......2",
  38.     "Eject Cassette .......3",
  39.     "Record ...............4",
  40.     "Play .................5",
  41.     "Fast Forward .........6",
  42.     "Rewind ...............7",
  43.     "Check Status .........8",
  44.     "Stop .................9",
  45.     "Quit .................<Esc>",
  46.     "",
  47.     0
  48.     };
  49.  
  50. extern int _doserrno;
  51.  
  52. int  _dos_dev_info( int handle,
  53.                     size_t *info )
  54. {
  55.    /* Get information about specified handle assoc
  56.     * with a file or device and return it in 'info'.
  57.     * Returns 0 if successful, !0 if not.  Error code
  58.     * contained in global '_doserrno'.
  59.     */
  60.  
  61.     regs.x.ax = 0x4400;     //DOS IOCTL get device info
  62.     regs.x.bx = handle;
  63.     intdos( ®s, ®s );
  64.     *info = regs.x.dx;      //assign device info word
  65.  
  66.     return regs.x.cflag;    //CY signals error
  67. }
  68.  
  69. int _dos_ioctl_read( int handle,
  70.                      void *iobuf,
  71.                      size_t nbytes  )
  72. {
  73.    /* Read ioctl data from a character device driver
  74.     * into 'iobuf'. Returns 0 if successful, !0 if not.
  75.     * Error code contained in global '_doserrno'.  This
  76.     * call ends up as the device driver's cmd code 3,
  77.     * ioctl_read.
  78.     */
  79.  
  80.     regs.x.ax = 0x4402;     //DOS read ioctl data
  81.     regs.x.bx = handle;
  82.     regs.x.cx = nbytes;
  83.     sregs.ds = FP_SEG( iobuf );
  84.     regs.x.dx = FP_OFF( iobuf );
  85.     intdosx( ®s, ®s, &sregs );
  86.  
  87.     return regs.x.cflag;    //CY signals error
  88. }
  89.  
  90. int _dos_ioctl_write( int handle,
  91.                       void *iobuf,
  92.                       size_t nbytes  )
  93. {
  94.    /* Write ioctl data to a character device driver
  95.     * from 'iobuf'. Returns 0 if successful, !0 if not.
  96.     * Error code contained in global '_doserrno'. This
  97.     * call ends up as the device driver's cmd code 12,
  98.     * ioctl_write.
  99.     */
  100.  
  101.     regs.x.ax = 0x4403;     //DOS write ioctl data
  102.     regs.x.bx = handle;
  103.     regs.x.cx = nbytes;
  104.     sregs.ds = FP_SEG( iobuf );
  105.     regs.x.dx = FP_OFF( iobuf );
  106.     intdosx( ®s, ®s, &sregs );
  107.  
  108.     return regs.x.cflag;    //CY signals error
  109. }
  110.  
  111. void print_menu(void )
  112. {
  113.     char **p;
  114.  
  115.     for( p = Menu; *p; p++ )
  116.         printf( "%s\n", *p );
  117. }
  118.  
  119. main( int argc, char **argv )
  120. {
  121.     int cmd;
  122.     size_t d_info;
  123.  
  124.     ioHandle = open( Dname, O_RDWR );
  125.  
  126.     if( ioHandle == -1 )  {
  127.         printf( "Unable to open TAPE device\n" );
  128.         return 1;
  129.     }
  130.  
  131.     //Verify that TAPE is a device and that
  132.     //it supports ioctl calls .....
  133.  
  134.     _dos_dev_info( ioHandle, &d_info );
  135.  
  136.     if( (d_info & IM_A_DEVICE) == 0 )  {
  137.         printf("Sorry, '%s' is a file\n", Dname );
  138.         return 1;
  139.     }
  140.  
  141.     if( (d_info & IOCTL_OK) == 0 )  {
  142.         printf("Sorry, TAPE doesn't support IOCTL\n");
  143.         return 1;
  144.     }
  145.  
  146.     //Initialize the device at 'boot-up' ....
  147.     ioArg.c_state = OFF;
  148.     ioArg.cmd = CMD_POWER_ON;
  149.     _dos_ioctl_write( ioHandle, (void *) &ioArg,
  150.                         IO_BUF_SIZE );
  151.  
  152.     //Execute user control loop .....
  153.     while( 1 )  {
  154.             print_menu();
  155.             printf( "Enter selection: " );
  156.             cmd = bioskey(0);
  157.             cmd &= 0x00ff;     //zap scan code
  158.  
  159.             if( cmd == 27 )
  160.                     break;
  161.  
  162.             printf( "%c\n\n", cmd );    //echo cmd
  163.             ioArg.cmd = cmd - '0';      //to integer
  164.             _dos_ioctl_read( ioHandle,
  165.                         (void *) &ioArg,
  166.                             IO_BUF_SIZE );
  167.     }
  168.  
  169.     return 0;
  170. }
  171.  
  172. /* ----- End of File ------------------------------- */
  173.