home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / DISKFREE.C < prev    next >
C/C++ Source or Header  |  1992-08-27  |  3KB  |  138 lines

  1. /*
  2. * DISKFREE.C - Output the amount of free storage on a disk.
  3. *
  4. * PROGRAMMER:        Martti Ylikoski
  5. * CREATED:        7.12.1990
  6. */
  7. static char *VERSION = "Version 1.1, Copyright (c) Martti Ylikoski, 1990,1991" ;
  8. /*
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <param.h>
  15. #include <paramstd.h>
  16. #define INCL_DOS
  17. #define INCL_DOSFILEMGR
  18. #include <os2.h>
  19. static char *progname ;
  20. extern unsigned long pflags ;
  21. static int diskfree(USHORT usDisk) ;
  22. ParamEntry pentry[12] = {
  23.      "P", &ParamSetPause, 0,
  24.      "F", &ParamSetFold, 0,
  25.      "V", &ParamSetVerbose, 0,
  26.      "R", &ParamSetReport, 1,
  27.      "S", &ParamSetSubDirs, 0,
  28.      "?", &ParamSetHelp, 1,
  29.      "H", &ParamSetHelp, 1,
  30.      "NOD", &ParamSetNoDefault, 0,
  31.      "TEST", &ParamSetTest, 0,
  32.      "Y", &ParamSetYes, 0,
  33.      "N", &ParamSetTest, 0,
  34.      "\0", NULL, 0
  35. } ;
  36.  
  37. ParamBlock params = {
  38.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  39.     pentry
  40. } ;
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44. USHORT ret, usDisk ;
  45. ULONG ulDrives ;
  46.  
  47. int i, j ;
  48.  
  49.    progname = argv[0] ;
  50.  
  51.    ParamHandle(¶ms, &argc, argv) ;
  52.  
  53.    if (pflags & PA_HELP)
  54.    {
  55.       printf("%s- display the amount of free space on a disk.\n", progname) ;
  56.       puts(VERSION) ;
  57.       puts("Usage: diskfree [/H | /?] [/R] [drive(s)]") ;
  58.       puts("Where,") ;
  59.       puts("     /R    = Report    /H,/? = Help");
  60.       return( 1 ) ;
  61.    }
  62.  
  63.    if ( argc == 1 )
  64.    {
  65.       if (( ret = DosQCurDisk(&usDisk, &ulDrives)) != 0)
  66.       {
  67.      fprintf(stderr,"%s: error in DosQCurDir ...\nExiting ...\n", progname) ;
  68.      return( 1 ) ;
  69.       }
  70.    }
  71.  
  72.    if (argc == 1)
  73.       diskfree(usDisk) ;
  74.    else
  75.       for (i = 1 ; i <argc ; i++)
  76.       {
  77.      usDisk = toupper( (int) argv[i][0] ) - 'A' + 1 ;
  78.      diskfree(usDisk) ;
  79.       }
  80.    return( 0 ) ;
  81. }
  82.  
  83. static int diskfree(USHORT usDisk)
  84. {
  85. FSALLOCATE fsallocate ;
  86. char p1[24], p2[24], p3[24] ;
  87. int i, j ;
  88. USHORT ret;
  89. ULONG freedisk ;
  90. ULONG totaldisk ;
  91.  
  92.    if (( ret = DosQFSInfo(usDisk, 1, (PBYTE) &fsallocate, sizeof(fsallocate))) != 0)
  93.    {
  94.       fprintf(stderr,"%s: error in DosQFSInfo ...\nExiting ...\n", progname) ;
  95.       return( 1 ) ;
  96.    }
  97.  
  98.    freedisk = fsallocate.cUnitAvail * fsallocate.cSectorUnit * fsallocate.cbSector ;
  99.    totaldisk = fsallocate.cUnit * fsallocate.cSectorUnit * fsallocate.cbSector ;   
  100.    sprintf(p1, "%lu", freedisk) ;
  101.    sprintf(p3, "%lu", totaldisk) ;   
  102.    strrev(p1) ;
  103.    strrev(p3) ;   
  104.  
  105.    for (i=0, j = 0; i < strlen(p1) ; i++, j++)
  106.    {
  107.       if (i%3 == 0 && i != 0)
  108.       {
  109.     p2[j] = ',' ;
  110.     j++;
  111.       }
  112.  
  113.       p2[j] = p1[i] ;
  114.    }
  115.    p2[j] = '\0' ;
  116.    strrev(p2) ;
  117.  
  118.    for (i=0, j = 0; i < strlen(p3) ; i++, j++)
  119.    {
  120.       if (i%3 == 0 && i != 0)
  121.       {
  122.     p1[j] = ',' ;
  123.     j++;
  124.       }
  125.  
  126.       p1[j] = p3[i] ;
  127.    }
  128.    p1[j] = '\0' ;
  129.    strrev(p1) ;
  130.    strcpy(p3,p1) ;
  131.  
  132.    
  133.    if (pflags & PA_REPORT)
  134.       printf("[DISKFREE]\nFreedisk=%ld\nTotaldisk=%ld\ndrive=%c:\n", freedisk, totaldisk, usDisk+'A'-1) ;
  135.    else
  136.       printf("\n%c: %s bytes free of %s bytes available.\n", usDisk+'A'-1, p2, p3 ) ;
  137. }
  138.