home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / tstdev.zip / TESTDEV.C < prev    next >
Text File  |  1993-12-06  |  11KB  |  294 lines

  1. #define INCL_DOS
  2. #define INCL_DOSDEVIOCTL
  3. #define INCL_DOSERRORS
  4. #define INCL_KBD
  5. #define INCL_VIO
  6. #include <os2.h>                /* include dos function declarations    */
  7. #include <stdlib.h>             /* include C memory mgmt defines        */
  8. #include <stdio.h>              /* include C memory mgmt defines        */
  9. #include <malloc.h>             /* include C memory mgmt defines        */
  10. #include <string.h>             /* include C memory mgmt defines        */
  11. #pragma pack(1)
  12.  
  13. #define CDType         0x8000
  14. #define FloppyType     0x4000
  15. #define LanType        0x2000
  16. #define LogicalType    0x1000
  17. #define VDISKType      0x0800
  18. #define OpticalType    0x0400
  19. #define NonRemovable   0x01
  20.  
  21. #define DisketteCylinders 80
  22.  
  23. #define FirstDrive             0
  24. #define B_Drive_0_Based        1
  25. #define Base_1_offset          1
  26. #define Binary_to_Printable 0x41
  27. #define LastDrive             26
  28. #define OpticalSectorsPerCluster 4
  29.  
  30. #define FSAttachSpace 100
  31.  
  32. // used for input to logical disk Get device parms Ioctl
  33. struct {
  34.         UCHAR Infotype;
  35.         UCHAR DriveUnit;
  36.         } DriveRequest;
  37.  
  38. // used for CD number of units Ioctl
  39. struct {
  40.         USHORT count;
  41.         USHORT first;
  42.         } cdinfo;
  43.  
  44. // array of device info to be collected
  45. BIOSPARAMETERBLOCK devices[LastDrive];
  46.  
  47. //
  48. //      Check if the B: drive is a logical drive by asking for its letter map
  49. //      if there is ANY map, then it MUST be logical, otherwise default is physical
  50. //
  51. void CheckifBisLogical(UCHAR FixedDriveLetter)
  52. {
  53.   ULONG parmsize,datasize;
  54.   ULONG Action;
  55.   UCHAR DriveIndex=1,Parm,LogicalDrive;
  56.   HFILE handle;
  57.   PSZ devname="C:";
  58.  
  59.   devname[0]=FixedDriveLetter;
  60.   if(!DosOpen(devname,&handle,&Action,0,FILE_NORMAL,OPEN_ACTION_OPEN_IF_EXISTS,OPEN_FLAGS_DASD|OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY,NULL))
  61.     {
  62.     parmsize=sizeof(Parm);
  63.     datasize=sizeof(LogicalDrive);
  64.     Parm=0;
  65.     LogicalDrive=DriveIndex+Base_1_offset;      // drive to check for (1 based)
  66.     if(!DosDevIOCtl(handle,IOCTL_DISK,DSK_GETLOGICALMAP,
  67.       (PVOID)&Parm, sizeof(Parm), &parmsize,
  68.       (PVOID)&LogicalDrive,    sizeof(LogicalDrive)   , &datasize))
  69.       {
  70.       // if the response is non-zero, this drive letter may be
  71.       // a logical drive mapped onto a physical drive
  72.       if(LogicalDrive)
  73.         {
  74.         // mark it logical
  75.         devices[DriveIndex].fsDeviceAttr |= LogicalType;  // say its a diskette drive
  76.         } /* end if */
  77.       } /* end if */
  78.     DosClose(handle);
  79.     } /* end if */
  80. }
  81.  
  82. int main(int argc, char *argv[], char *envp)
  83. {
  84.   APIRET rc;
  85.   ULONG parmsize,datasize;
  86.   ULONG drivemap,currentdisk,mask,Action;
  87.   UCHAR DriveIndex,NumDiskettesInstalled,NumDiskettesFound=0;
  88.   HFILE handle;
  89.   UCHAR devname[5]="c:";
  90.   PFSQBUFFER2 fsinfo;
  91.   ULONG len;
  92.   PSZ name;
  93.   UCHAR FirstHardfile=0;
  94.  
  95.   // clear the device workarea
  96.   memset(&devices,0,sizeof(devices));
  97.  
  98.   // get number of physical diskette drives
  99.   DosDevConfig(&NumDiskettesInstalled,DEVINFO_FLOPPY);
  100.  
  101.   // get current drive map
  102.   rc=DosQueryCurrentDisk(¤tdisk,&drivemap);
  103.  
  104.   // loop thru the drive map
  105.   for(DriveIndex=FirstDrive, mask=1L; DriveIndex<LastDrive; DriveIndex++, mask<<=1)
  106.     {
  107.     if(mask & drivemap)  // is this drive present
  108.       {
  109.       parmsize=sizeof(DriveRequest);
  110.       datasize=sizeof(devices[DriveIndex]);
  111.       DriveRequest.Infotype=0;
  112.       DriveRequest.DriveUnit=DriveIndex;
  113.  
  114.       // ask for the device attributes, type & default BPB cylinder size
  115.  
  116.       if(!DosDevIOCtl(-1,IOCTL_DISK,DSK_GETDEVICEPARAMS,
  117.         (PVOID)&DriveRequest, sizeof(DriveRequest), &parmsize,
  118.         (PVOID)&devices[DriveIndex],    sizeof(devices[DriveIndex])   , &datasize))
  119.         {
  120.         if(FirstHardfile==0 && devices[DriveIndex].fsDeviceAttr==DEVTYPE_FIXED)
  121.           {
  122.           FirstHardfile=DriveIndex+Binary_to_Printable;
  123.           } /* end if */
  124.         } /* end if */
  125.       } /* end if */
  126.     } /* end for */
  127.  
  128.   // try to check for CD drives just in case
  129.   if(!DosOpen("\\DEV\\CD-ROM2$",&handle,&Action,0,FILE_NORMAL,OPEN_ACTION_OPEN_IF_EXISTS,OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY,NULL))
  130.     {
  131.     datasize=sizeof(cdinfo);
  132.     if(!(rc=DosDevIOCtl(handle,0x82,0x60,
  133.       NULL, 0, NULL,
  134.       (PVOID)&cdinfo,    sizeof(cdinfo)   , &datasize)))
  135.       {
  136.       // mark those devices as CDs
  137.       while(cdinfo.count--)
  138.         {
  139.         devices[cdinfo.first+cdinfo.count].fsDeviceAttr|=CDType;     // say its a CD device
  140.         } /* end while */
  141.       } /* end if */
  142.     DosClose(handle);
  143.     } /* end if */
  144.  
  145.   // turn off error popups for removables with no media
  146.   DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR);
  147.  
  148.   // allocate space for DosQueryFSAttach response
  149.   fsinfo=( PFSQBUFFER2 )malloc(FSAttachSpace);
  150.  
  151.   // loop thru the found devices
  152.   for(DriveIndex=FirstDrive; DriveIndex<LastDrive; DriveIndex++)
  153.     {
  154.     // if the device is removable and NOT a CD
  155.     if(devices[DriveIndex].bDeviceType)
  156.       {
  157.       if((devices[DriveIndex].fsDeviceAttr & (CDType | NonRemovable))==0)
  158.         {
  159.         devname[0]=DriveIndex+Binary_to_Printable;
  160.         len=FSAttachSpace;
  161.         rc=DosQueryFSAttach(devname,0L,
  162.                            FSAIL_QUERYNAME,fsinfo,&len);
  163.         if (rc==NO_ERROR)
  164.           {
  165.           if(fsinfo->iType==FSAT_REMOTEDRV)     // if remote
  166.             devices[DriveIndex].fsDeviceAttr |= LanType;      // mark it so
  167.           else if(fsinfo->iType==FSAT_LOCALDRV) // if local
  168.             {
  169.             name=fsinfo->szName;
  170.             name+=strlen(name)+1;
  171.             if(!strcmp(name,"FAT"))
  172.               {
  173.               // device is a removable FAT drive, so it MUST be diskette
  174.               // as Optical has another name as does LAN and SRVIFS
  175.               if(devices[DriveIndex].bSectorsPerCluster!=OpticalSectorsPerCluster)
  176.                 {
  177.                 devices[DriveIndex].fsDeviceAttr |= FloppyType;   // say its a diskette drive
  178.                 NumDiskettesFound++;                  // count it, assume physical drive
  179.                 if(DriveIndex==B_Drive_0_Based)       // is this for the B drive?
  180.                   {
  181.                   CheckifBisLogical(FirstHardfile);   // yes, see if logical
  182.                   } /* end if */
  183.                 } /* end if */
  184.               else
  185.                 {
  186.                 devices[DriveIndex].fsDeviceAttr |= OpticalType;  // say its a diskette drive
  187.                 } /* end else */
  188.               } /* end if */
  189.             } /* end else */
  190.           } /* end if */
  191.         else // must be no media or audio only (for CDs at least)
  192.           {
  193.           if(devices[DriveIndex].cCylinders<=DisketteCylinders)// floppies will always be 80
  194.             {                                      // or less cylinders
  195.             if(devices[DriveIndex].bSectorsPerCluster!=OpticalSectorsPerCluster)
  196.               {
  197.               devices[DriveIndex].fsDeviceAttr |= FloppyType;    // say its a diskette drive
  198.               NumDiskettesFound++;                   // count it, assume physical drive
  199.               if(DriveIndex==B_Drive_0_Based)        // is this for the B drive?
  200.                 {
  201.                 CheckifBisLogical(FirstHardfile);    // yes, see if logical
  202.                 } /* end if */
  203.               } /* end if */
  204.             else
  205.               {
  206.               devices[DriveIndex].fsDeviceAttr |= OpticalType;  // say its a diskette drive
  207.               } /* end else */
  208.             } /* end if */
  209.           } /* end else */
  210.         } /* end if */
  211.       else      // non removable or CD type. maybe VDISK
  212.         {
  213.         if(!(devices[DriveIndex].fsDeviceAttr & CDType))    // if NOT CD
  214.           {
  215.           if(devices[DriveIndex].cFATs==1)                  // is there only one FAT?
  216.             {
  217.             devices[DriveIndex].fsDeviceAttr |= VDISKType;  // is vdisk
  218.             } /* end if */
  219.           } /* end if */
  220.         } /* end else */
  221.       } /* end if */
  222.     } /* end for */
  223.   free(fsinfo);
  224.  
  225.   // check to see if we accounted for all physical diskette drives install
  226.   if(NumDiskettesInstalled!=NumDiskettesFound)
  227.     {
  228.     // found more diskettes than physically installed, some are logical
  229.     if(NumDiskettesInstalled<NumDiskettesFound)
  230.       {
  231.       printf("%d more diskette drive(s) were found than are reported physically installed\n",NumDiskettesFound-NumDiskettesInstalled);
  232.       } /* end if */
  233.     else
  234.       {
  235.       //
  236.       // This is a code bug, we didn't find all the physical diskette drives
  237.       // we should NEVER EVER get here
  238.       //
  239.       printf("Some diskette drive was not detected properly\n");
  240.       printf("Diskettes installed =%d Diskettes detected = %d\n", NumDiskettesInstalled, NumDiskettesFound);
  241.       } /* end else */
  242.     } /* end if */
  243.  
  244.   // display the device types we found
  245.   for(DriveIndex=FirstDrive; DriveIndex<LastDrive; DriveIndex++)
  246.     {
  247.     if(devices[DriveIndex].bDeviceType)
  248.       {
  249.       printf("Device %c is ", DriveIndex+Binary_to_Printable);
  250.       if(devices[DriveIndex].fsDeviceAttr & FloppyType)
  251.         {
  252.         printf("a  %s diskette drive",devices[DriveIndex].fsDeviceAttr & LogicalType?"logical ":"physical");
  253.         } /* end if */
  254.       else if(devices[DriveIndex].fsDeviceAttr & CDType)
  255.         {
  256.         printf("a  CDROM drive");
  257.         } /* end else */
  258.       else if(devices[DriveIndex].fsDeviceAttr & LanType)
  259.         {
  260.         printf("a  redirected drive");
  261.         } /* end else */
  262.       else if(devices[DriveIndex].fsDeviceAttr & VDISKType)
  263.         {
  264.         printf("a  virtual disk");
  265.         } /* end else */
  266.       else if(devices[DriveIndex].fsDeviceAttr & OpticalType)
  267.         {
  268.         printf("an Optical disk");
  269.         } /* end else */
  270.       else if(devices[DriveIndex].bDeviceType == DEVTYPE_FIXED)
  271.         {
  272.         printf("a  hardfile partition");
  273.         } /* end else */
  274.       else if(devices[DriveIndex].bDeviceType == DEVTYPE_UNKNOWN)
  275.         {
  276.         // we should really really never see these if the code above
  277.         // is right (no bernouli or optical drives with no media to test)
  278.         if(devices[DriveIndex].fsDeviceAttr & NonRemovable)
  279.           {
  280.           printf("an unknown non-removable media device Attrib=%02X",
  281.              devices[DriveIndex].fsDeviceAttr );
  282.           } /* end if */
  283.         else
  284.           {
  285.           printf("an unknown removable media device Attrib=%02X",
  286.              devices[DriveIndex].fsDeviceAttr );
  287.           } /* end else */
  288.         } /* end else */
  289.       printf("\n");
  290.       } /* end if */
  291.     } /* end for */
  292.   return rc;
  293. }
  294.