home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rwp15.zip / DRIVES.C next >
C/C++ Source or Header  |  1993-03-24  |  9KB  |  287 lines

  1. /* --------------------------------------------------------------------
  2.                                 Drives 
  3.                               Chapter 15
  4.  
  5.                     Real World Programming for OS/2
  6.              Copyright (c) 1993 Blain, Delimon, and English
  7. -------------------------------------------------------------------- */
  8.  
  9. #define INCL_DOS
  10. #define INCL_DOSDEVIOCTL
  11. #define INCL_WIN
  12. #define INCL_ERRORS
  13.  
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "drives.h"
  18.  
  19. /* Local Functions    */
  20. VOID DisplayDriveInfo (HWND,ULONG);
  21. VOID FormatNumberField (HWND,USHORT,ULONG);
  22. VOID QueryDiskSpace (ULONG,PULONG,PULONG,PULONG);
  23. VOID QueryVolumeLabel (ULONG,PSZ);
  24.  
  25. /* External variables */
  26. extern HAB      hab;                    /* Handle to anchor block       */
  27.  
  28. /* Global variables   */
  29. DRIVEINFO DriveInfo[26];                /* Array of drive information   */
  30. ULONG     ulNumDrives = 0L;             /* Number of valid drives       */
  31.  
  32. char szDriveDescription[11][16] =
  33.     {
  34.         "LD Floppy",
  35.         "HD Floppy",
  36.         "3.5"" Floppy",
  37.         "8"" SD Floppy",
  38.         "8"" HD Floppy",
  39.         "Fixed Disk",
  40.         "Tape Drive",
  41.         "Unknown",
  42.         "R/W Optical",
  43.         "3.5"" 4MB Floppy",
  44.         "Not Ready",
  45.     };
  46.  
  47. /* ----------------------- Local Functions ----------------------- */
  48.  
  49. VOID DisplayDriveInfo (HWND hWnd, ULONG ulInx)
  50. {
  51.     CHAR  szVolumeLabel[12];
  52.     ULONG ulTotalSpace,
  53.           ulAllocated,
  54.           ulAvailable;
  55.  
  56.     WinSetDlgItemText (hWnd, IDC_LOCATION, DriveInfo[ulInx].szLocation);
  57.     WinSetDlgItemText (hWnd, IDC_REMOVABLE, DriveInfo[ulInx].szRemovable);
  58.     WinSetDlgItemText (hWnd, IDC_FILESYSTEM, DriveInfo[ulInx].szFileSystem);
  59.     WinSetDlgItemText (hWnd, IDC_DESCRIPTION, 
  60.         szDriveDescription[DriveInfo[ulInx].ulDescriptionIndex]);
  61.  
  62.     WinSetPointer (HWND_DESKTOP, 
  63.         WinQuerySysPointer (HWND_DESKTOP,SPTR_WAIT,FALSE));
  64.  
  65.     QueryVolumeLabel (DriveInfo[ulInx].szDrive[0] - 'A' + 1, szVolumeLabel);
  66.     WinSetDlgItemText (hWnd, IDC_VOLUMELABEL, szVolumeLabel);
  67.  
  68.     QueryDiskSpace (DriveInfo[ulInx].szDrive[0] - 'A' + 1, &ulTotalSpace,
  69.         &ulAllocated, &ulAvailable);
  70.  
  71.     FormatNumberField (hWnd, IDC_TOTALSPACE, ulTotalSpace);
  72.     FormatNumberField (hWnd, IDC_ALLOCATED,  ulAllocated);
  73.     FormatNumberField (hWnd, IDC_AVAILABLE,  ulAvailable);
  74.  
  75.     WinSetPointer (HWND_DESKTOP, 
  76.         WinQuerySysPointer (HWND_DESKTOP,SPTR_ARROW,FALSE));
  77.  
  78.     return;
  79. }
  80.  
  81. VOID FormatNumberField (HWND hWnd, USHORT usCtlID, ULONG ulNumber)
  82. {
  83.     CHAR  szNumber[13];
  84.  
  85.     sprintf (szNumber, "%12lu", ulNumber);
  86.  
  87.     WinSetDlgItemText (hWnd, usCtlID, szNumber);
  88.  
  89.     return;
  90. }
  91.  
  92. VOID QueryDiskSpace (ULONG ulDriveNumber, PULONG pulTotalSpace,
  93.                      PULONG pulAllocated, PULONG pulAvailable)
  94. {
  95.     FSALLOCATE fsAllocate;
  96.  
  97.     DosError (FERR_DISABLEHARDERR);
  98.  
  99.     if (!DosQueryFSInfo (ulDriveNumber, FSIL_ALLOC, &fsAllocate, sizeof(FSALLOCATE)))
  100.     {
  101.         *pulTotalSpace =
  102.             fsAllocate.cSectorUnit * fsAllocate.cUnit * fsAllocate.cbSector;
  103.         *pulAvailable  =
  104.             fsAllocate.cSectorUnit * fsAllocate.cUnitAvail * fsAllocate.cbSector;
  105.         *pulAllocated  = *pulTotalSpace - *pulAvailable;
  106.     }
  107.     else
  108.         *pulTotalSpace = *pulAllocated = *pulAvailable  = 0L;
  109.  
  110.     DosError (FERR_ENABLEHARDERR);
  111.  
  112.     return;
  113. }
  114.  
  115. VOID QueryVolumeLabel (ULONG ulDriveNumber, PSZ pszVolumeLabel)
  116. {
  117.     FSINFO fsInfo;
  118.  
  119.     DosError (FERR_DISABLEHARDERR);
  120.  
  121.     if (!DosQueryFSInfo (ulDriveNumber, FSIL_VOLSER, &fsInfo, sizeof(FSINFO)))
  122.         strcpy (pszVolumeLabel, fsInfo.vol.szVolLabel);
  123.     else
  124.         pszVolumeLabel[0] = '\0';
  125.  
  126.     DosError (FERR_ENABLEHARDERR);
  127.  
  128.     return;
  129. }
  130.  
  131. VOID QueryDrives (HWND hWnd)
  132. {
  133.     PFSQBUFFER2        pfsq2;
  134.     ULONG              ulLen,
  135.                        ulInx,
  136.                        ulAction,
  137.                        ulParmLen,
  138.                        ulDataLen;
  139.     APIRET             RetCode;
  140.     HFILE              hFile;
  141.     BIOSPARAMETERBLOCK bpb;
  142.     CHAR               szDrive[3] = " :";
  143.     BYTE               cBlock     = 0;
  144.  
  145.     WinSetPointer (HWND_DESKTOP, 
  146.         WinQuerySysPointer (HWND_DESKTOP,SPTR_WAIT,FALSE));
  147.  
  148.     /* Allocate buffer */
  149.     DosAllocMem ((PPVOID)&pfsq2, 1024L, fALLOC);
  150.  
  151.     DosError (FERR_DISABLEHARDERR);
  152.  
  153.     ulNumDrives = 0L;
  154.  
  155.     for (ulInx = 0; ulInx < 26; ulInx++)
  156.     {
  157.         szDrive[0] = (CHAR)('A' + ulInx);
  158.  
  159.         ulLen = 1024L;
  160.         RetCode = DosQueryFSAttach (szDrive, 0L, FSAIL_QUERYNAME, pfsq2, &ulLen);
  161.  
  162.         DriveInfo[ulNumDrives].szDrive[0]         = szDrive[0];
  163.         DriveInfo[ulNumDrives].szDrive[1]         = '\0';
  164.  
  165.         if (RetCode == ERROR_NOT_READY)
  166.         {
  167.             /* Assume local, removable, and FAT file system */
  168.             strcpy (DriveInfo[ulNumDrives].szLocation,   "Local");
  169.             strcpy (DriveInfo[ulNumDrives].szRemovable,  "Yes");
  170.             strcpy (DriveInfo[ulNumDrives].szFileSystem, "FAT");
  171.             DriveInfo[ulNumDrives].ulDescriptionIndex = 10L;
  172.             ulNumDrives++;
  173.         }
  174.         else if (RetCode != ERROR_INVALID_DRIVE)
  175.         {
  176.             bpb.fsDeviceAttr = 0;
  177.  
  178.             /* Attempt to open the device */
  179.             if (!DosOpen (szDrive, &hFile, &ulAction, 0L,
  180.                 FILE_NORMAL, FILE_OPEN, OPEN_FLAGS_DASD | OPEN_SHARE_DENYNONE, 0L))
  181.             {
  182.                 ulParmLen = sizeof(BYTE);
  183.                 ulDataLen = sizeof(BIOSPARAMETERBLOCK);
  184.                 DosDevIOCtl (hFile, IOCTL_DISK, DSK_GETDEVICEPARAMS,
  185.                     (PVOID)&cBlock, ulParmLen, &ulParmLen,
  186.                     (PVOID)&bpb, ulDataLen, &ulDataLen);
  187.                 DosClose (hFile);
  188.             }
  189.             else
  190.             {
  191.                 /* Remote drives may not allow themselves to be opened
  192.                    with the OPEN_FLAGS_DASD access flag.  Default to
  193.                    not removable and description of unknown. */
  194.                 bpb.fsDeviceAttr = 0x0001;
  195.                 bpb.bDeviceType  = 7;
  196.             }
  197.  
  198.             /* Is the drive remote?      */
  199.             if (pfsq2->iType == FSAT_REMOTEDRV)
  200.                 strcpy (DriveInfo[ulNumDrives].szLocation, "Remote");
  201.             else
  202.                 strcpy (DriveInfo[ulNumDrives].szLocation, "Local");
  203.  
  204.             /* Is the drive removable?   */
  205.             if (bpb.fsDeviceAttr & 0x0001)
  206.                 strcpy (DriveInfo[ulNumDrives].szRemovable, "No");
  207.             else
  208.                 strcpy (DriveInfo[ulNumDrives].szRemovable, "Yes");
  209.                                   
  210.             /* Set the description index */
  211.             if (bpb.bDeviceType < 10)
  212.                 DriveInfo[ulNumDrives].ulDescriptionIndex = (LONG)bpb.bDeviceType;
  213.             else
  214.                 DriveInfo[ulNumDrives].ulDescriptionIndex = 7L;
  215.  
  216.             /* Set the file system name  */
  217.             strncpy (DriveInfo[ulNumDrives].szFileSystem,
  218.                        (PSZ)(pfsq2->szName + 1 + pfsq2->cbName), 15);
  219.  
  220.             ulNumDrives++;
  221.         }
  222.     }
  223.  
  224.     DosError (FERR_ENABLEHARDERR);
  225.  
  226.     DosFreeMem (pfsq2);
  227.  
  228.     /* Add items to the drive listbox */
  229.     for (ulInx = 0; ulInx < ulNumDrives; ulInx++)
  230.         WinSendDlgItemMsg (hWnd, IDC_DRIVELIST, LM_INSERTITEM, (MPARAM)LIT_END, 
  231.             DriveInfo[ulInx].szDrive);
  232.     WinSendDlgItemMsg (hWnd, IDC_DRIVELIST, LM_SELECTITEM, 0L, (MPARAM)TRUE);
  233.  
  234.     WinSetPointer (HWND_DESKTOP, 
  235.         WinQuerySysPointer (HWND_DESKTOP,SPTR_ARROW,FALSE));
  236.     return;
  237. }
  238.  
  239. /* ----------------------  Dialog Function ----------------------- */
  240.  
  241. MRESULT EXPENTRY DrivesDlgProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  242. {
  243.     BOOL    bHandled = TRUE;
  244.     MRESULT mReturn  = 0;
  245.  
  246.     switch (msg)
  247.     {           
  248.         case WM_INITDLG:
  249.             QueryDrives (hWnd);
  250.             break;
  251.  
  252.         case WM_CONTROL:
  253.             switch (LOUSHORT(mp1))
  254.             {
  255.                 case IDC_DRIVELIST:
  256.                     if (HIUSHORT(mp1) == CBN_EFCHANGE)
  257.                     {
  258.                         DisplayDriveInfo (hWnd,
  259.                             (ULONG)WinSendDlgItemMsg (hWnd, IDC_DRIVELIST, 
  260.                                 LM_QUERYSELECTION, (MPARAM)LIT_FIRST, 0L));
  261.                     }
  262.                     break;
  263.             }
  264.             break;
  265.  
  266.         case WM_SYSCOMMAND:
  267.               switch (SHORT1FROMMP(mp1))
  268.             {
  269.                     case SC_CLOSE:
  270.                     WinDismissDlg (hWnd, FALSE);
  271.                     bHandled = TRUE;
  272.                     break;
  273.             }
  274.             break;
  275.  
  276.         default:
  277.             bHandled = FALSE;
  278.             break;
  279.     }
  280.  
  281.     if (!bHandled)
  282.         mReturn = WinDefDlgProc (hWnd, msg, mp1, mp2);
  283.  
  284.     return (mReturn);
  285. }
  286.  
  287.