home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 4 Drivers / 04-Drivers.zip / DEVHLP.ZIP / DRVRLIST.C < prev    next >
C/C++ Source or Header  |  1990-09-25  |  5KB  |  135 lines

  1. /* DRVRLIST.C
  2.    list the device drivers in OS/2
  3.    Art Rothstein, 1990
  4.  
  5.    we assume the first driver in the chain is NUL and is in the global data
  6.    segment, and that the second driver (CON) is the same segment.
  7.  
  8.    cl -AL drvrlist.c (four-byte data pointers required for memchr)
  9. */
  10. #define INCL_DOSDEVICES
  11. #include <os2.h>
  12. #include <process.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "devhlp.h"
  16.  
  17. USHORT devhlp ;
  18.  
  19. SEL MakeSel( SEL selValue)
  20. {
  21.   extern USHORT devhlp ;
  22.   REGS regs ;
  23.   USHORT ret ;
  24.  
  25.   regs.dx = DevHlp_VirtToPhys ;         // function requested
  26.   regs.ds = selValue ;                  // selector
  27.   regs.es = 0 ;                         // avoid trap
  28.   regs.si = 0 ;                         // offset
  29.   ret = DosDevIOCtl( ®s, ®s, 0x60, 128, devhlp) ;
  30.   if ( ret != 0  ||  regs.flags.carry != 0)
  31.     return 0 ;
  32.   // physical address in ax:bx
  33.   regs.cx = 0 ;                                  // limit 65,535
  34.   regs.dx = MAKEUSHORT( DevHlp_PhysToUVirt, UVirt_ReadWrite);
  35.   regs.es = 0 ;                                  // avoid trap
  36.   ret = DosDevIOCtl( ®s, ®s, 0x60, 128, devhlp) ;
  37.   if ( ret != 0  ||  regs.flags.carry != 0)      // if error
  38.     return 0 ;
  39.   return regs.es ;                               // return the selector
  40. }
  41.  
  42. BOOL ReleaseSel( SEL selValue)
  43. {
  44.   extern USHORT devhlp ;
  45.   REGS regs ;
  46.   USHORT ret ;
  47.  
  48.   regs.ax = selValue ;                           // selector to free
  49.   regs.dx = MAKEUSHORT( DevHlp_PhysToUVirt, UVirt_Release);
  50.   regs.ds = 0 ;                                  // safety
  51.   regs.es = 0 ;
  52.   ret = DosDevIOCtl( ®s, ®s, 0x60, 128, devhlp) ;
  53.   if ( ret != 0  ||  regs.flags.carry != 0)     // if error
  54.     return FALSE ;
  55.   return TRUE ;                                 // successful return
  56. }
  57.  
  58. void main( void)
  59. {
  60.   USHORT usOffsetDriver
  61.        , usBytesLeft;                       // in search for NUL device
  62.   PCH pchGlobal ;                           // pointer to system global data
  63.   static CHAR szDriverName[] = "DEVHLPXX"   // device helper driver
  64.         , szNullDriver[] = "NUL     ";      // first driver in system
  65.  
  66.   typedef struct _DDHEADER  {               // device driver header
  67.     struct _DDHEADER * pddNext ;            // chain to next driver
  68.     USHORT fsAttribute ;                    // driver attributes
  69.     USHORT usStrategyEntryOffset ;
  70.     USHORT usIDCEntryOffset ;               // inter device communication
  71.     CHAR chName[ 8] ;                       // name for character devices
  72.     USHORT usIDCEntrySegmentProt ;
  73.     USHORT usIDCDataSegmentProt ;
  74.     USHORT usIDCEntrySegmentReal ;
  75.     USHORT usIDCDataSegmentReal ;
  76.   }  DDHEADER ;
  77.   typedef DDHEADER * PDDHEADER ;
  78.   PDDHEADER pddCurrent                      // current DCB
  79.       , pddNext;                            // next DCB
  80.   SEL selDriver ;                           // selector of DCB
  81.  
  82.   // open the DEVHLP device
  83.   if ((devhlp = open(szDriverName, 0)) == -1) {
  84.     puts( "Can't find DEVHLP.SYS") ;
  85.     exit( 1) ;
  86.   }
  87.  
  88.   // locate the first driver
  89.   selDriver = 0x50 ;                        // global data segment
  90.   usOffsetDriver = 0 ;
  91.   usBytesLeft = 32000 ;                     // should be large enough
  92.   pchGlobal = MAKEP( MakeSel( selDriver), usOffsetDriver) ;
  93.   do  {
  94.     PCH pchMatch ;
  95.  
  96.     pchMatch = memchr( pchGlobal + 1, 'N', usBytesLeft); //look for first char
  97.     if ( pchMatch == NULL)  {               // if no match
  98.       ReleaseSel( SELECTOROF( pchGlobal)) ; // release the selector
  99.       puts( "NUL driver not found") ;       // and give up
  100.       exit( 1) ;
  101.     }  // if no match
  102.                         // partial match
  103.     usBytesLeft -= pchMatch - pchGlobal ;   // reduce residual count
  104.     pchGlobal = pchMatch ;                  // point to start of match
  105.   }  while ( memcmp( pchGlobal              // break out if name matches
  106.            , szNullDriver                   //  exactly
  107.            , sizeof szNullDriver - 1) != 0);
  108.  
  109.   // run the chain
  110.   printf( "  Address Name\n") ;                      // column headings
  111.   for ( usOffsetDriver = OFFSETOF( pchGlobal) - 0x0a // back up to DCB start
  112.     , pddCurrent = ( PDDHEADER) ( pchGlobal - 0x0a)
  113.     , selDriver = SELECTOROF( pddCurrent->pddNext)   // selector of next DCB
  114.       ; ; ) {
  115.     printf( "%4X:%04X ", selDriver, usOffsetDriver);
  116.     if ( ( pddCurrent->fsAttribute & 0x8000) == 0)  // if block driver
  117.       printf( "Block device, %d logical units\n"
  118.           , pddCurrent->chName[ 0]);                // number of units
  119.     else                                            // if character driver
  120.       printf( "%-8.8s\n", pddCurrent->chName);
  121.     selDriver = SELECTOROF( pddCurrent->pddNext) ;  // point to next DCB
  122.     usOffsetDriver = OFFSETOF( pddCurrent->pddNext) ;
  123.     if ( usOffsetDriver == 0xffff)                  // if end of chain
  124.       break ;                                       // we are done
  125.     pddNext = MAKEP( MakeSel( selDriver), usOffsetDriver) ;
  126.     ReleaseSel( SELECTOROF( pddCurrent)) ;          // free previous DCB
  127.     pddCurrent = pddNext ;                          // age the pointer
  128.   }  // loop once for each device driver
  129.  
  130.   // release the last selector
  131.   ReleaseSel( SELECTOROF( pddCurrent)) ;
  132.  
  133.   exit( 0) ;
  134. }
  135.