home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / dfemx.zip / df.cc next >
C/C++ Source or Header  |  1993-10-02  |  4KB  |  155 lines

  1. // Hi Emacs, this may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  * df, yet another clone of this unix utility program.
  4.  *
  5.  * Approach: All drive letters from C to Z are scanned with
  6.  * DosQueryFSInfo. This would lead to error messages at all
  7.  * drive letters which are unused. So we turn them off with
  8.  * the OS-function DosError and reenable them later on.
  9.  * A call to DosQueryFSInfo in mode FSIL_ALLOC gives information
  10.  * about the total and available size of the drive. An additional
  11.  * call of DosQueryFSAttach returns information about the file
  12.  * system driver attached to the drive (FAT,HPFS,NFS,LAN,CDROM)
  13.  * and for LAN and NFS drives information about the service name.
  14.  *
  15.  * Compiler: emx-0.8g
  16.  *
  17.  * Author: Bernd Esser
  18.  *         Physikalisches Institut der Universit"at Bonn, Germany
  19.  *         esser@pib1.physik.uni-bonn.de
  20.  *
  21.  * $Log: df.cc,v $
  22.  * Revision 1.2  1993/10/25  18:42:23  Bernd_Esser
  23.  * style brushup
  24.  *
  25.  * Revision 1.1  1993/09/25  17:12:16  Bernd_Esser
  26.  * first version
  27.  */
  28.  
  29. /*-----------------------------------------------------------------*/
  30.  
  31. #define INCL_DOSMISC
  32. #define INCL_DOSERRORS
  33. #define INCL_DOSFILEMGR
  34.  
  35. #include <os2.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <iostream.h>
  39.  
  40. /*-----------------------------------------------------------------*/
  41.  
  42. struct drvInfo {
  43.   ULONG totalSize;
  44.   ULONG usedSize;
  45.   ULONG availSize;
  46.   ULONG percentUsed;
  47.   char  driveLetter;
  48.   char  remoteName[256];
  49.   char  driverName[256];
  50. };
  51.  
  52. /*-----------------------------------------------------------------*/
  53.  
  54. int getDriveInfo(const int driveNum, struct drvInfo * info)
  55. {
  56.   struct buffer1 {
  57.     ULONG  unitID;
  58.     ULONG  sectorsPerUnit;
  59.     ULONG  totalUnits;
  60.     ULONG  availUnits;
  61.     USHORT bytesPerUnit;
  62.   } buf1;
  63.   
  64.   /* turn off all error messages */
  65.   int rc = DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  66.   
  67.   if (DosQueryFSInfo(driveNum, FSIL_ALLOC, &buf1, sizeof(buf1)) != NO_ERROR)
  68.     return 0;
  69.   else {
  70.     info->totalSize   = ULONG(double(buf1.totalUnits) * 
  71.                   double(buf1.bytesPerUnit) / 1024.0);
  72.     info->availSize   = ULONG(double(buf1.availUnits) * 
  73.                   double(buf1.bytesPerUnit) / 1024.0);
  74.     info->usedSize    = info->totalSize - info->availSize;
  75.     info->percentUsed = ULONG(100.0 * 
  76.                   double(info->usedSize) / 
  77.                   double(info->totalSize));
  78.   }
  79.   
  80.   /* reenable error messages */
  81.   rc = DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
  82.   
  83.   info->driveLetter = (driveNum - 1) + 'A';
  84.   UCHAR drv[3];
  85.   drv[0] = info->driveLetter;
  86.   drv[1] = ':';
  87.   drv[2] = 0;
  88.   
  89.   struct buffer2 {
  90.     USHORT iType;
  91.     USHORT cbName;
  92.     USHORT cbFSDName;
  93.     USHORT cbFSAData;
  94.     char   buf[1024];
  95.   } buf2;
  96.   
  97.   ULONG len = sizeof(buf2);
  98.   
  99.   if (DosQueryFSAttach(drv, 0, FSAIL_QUERYNAME, 
  100.                (struct FSQBUFFER2 *)&buf2, &len) == NO_ERROR) {
  101.     strcpy(info->driverName, &buf2.buf[buf2.cbName + 1]);
  102.     
  103.     switch (buf2.iType) {
  104.     case 1 :     /* should not happen at all */
  105.       strcpy(info->remoteName, "local char device");
  106.       break;
  107.     case 2 :     /* ditto */
  108.       strcpy(info->remoteName, "pseudo char device");
  109.       break;
  110.     case 3 :
  111.       strcpy(info->remoteName, "local");
  112.       break;
  113.     case 4 :
  114.       strcpy(info->remoteName, 
  115.          (char *)&buf2.buf[buf2.cbName + 1 + buf2.cbFSDName + 1]);
  116.       break;
  117.     default :
  118.       strcpy(info->remoteName, "unknown device type");
  119.       break;
  120.     }
  121.   }
  122.   else {
  123.     strcpy(info->driverName, "???");
  124.     strcpy(info->remoteName, "???");
  125.   }
  126.   
  127.   return 1;
  128. }
  129.  
  130. /*-----------------------------------------------------------------*/
  131.  
  132. int main(int argc, char * argv[])
  133. {
  134.   struct drvInfo driveInfo;
  135.   
  136.   cout.form("Filesystem          1024-blocks       Used  Available  "
  137.         "Capacity  Mounted on\n");
  138.   
  139.   for(int i = 3; i < 27; i++)
  140.     if (getDriveInfo(i, &driveInfo))
  141.       cout.form("%-20s %10lu %10lu %10lu %8lu%%  %c:(%s)\n",
  142.         driveInfo.remoteName, driveInfo.totalSize,
  143.         driveInfo.usedSize, driveInfo.availSize,
  144.         driveInfo.percentUsed, driveInfo.driveLetter,
  145.         driveInfo.driverName);
  146.   
  147.   return 0;
  148. }
  149.  
  150. /*
  151.  * Local Variables:
  152.  * compile-command: "dmake df.exe"
  153.  * end:
  154.  */
  155.