home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / commands / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  7.2 KB  |  331 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     df.c
  4.         
  5.     Copyright (c) 1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  16. #include <A4Stuff.h>
  17. #endif
  18.  
  19. #include "nshc.h"
  20.  
  21. #include "arg_utl.proto.h"
  22. #include "str_utl.proto.h"
  23. #include "nshc_utl.proto.h"
  24.  
  25. /* ======================================== */
  26.  
  27. // data definition - this struct is the root of all data
  28.  
  29. typedef struct {
  30.  
  31.     int        arg;            // position in arg list (used when args are given)
  32.     short    volumeIndex;    // position in volume list (used when no args are given)
  33.     
  34. } t_df_data;
  35.  
  36. typedef    t_df_data    **df_hndl;
  37.  
  38. /* ======================================== */
  39.  
  40. // prototypes
  41.  
  42. // prototypes - utility
  43.  
  44. void df_bad( t_nshc_parms *nshc_parms, int code );
  45. void df_good( t_nshc_parms *nshc_parms );
  46. int  df_match( StringPtr from_os, StringPtr from_user );
  47.  
  48. // prototypes - file routines
  49.  
  50. void df_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 name, HParamBlockRec    *hfsParams);
  51. int  df_scan( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, short volumeIndex  );
  52. int  df_find( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 match  );
  53.  
  54. // state machine - custom routines
  55.  
  56. void df_by_arg( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, df_hndl hData );
  57. void df_by_vol( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, df_hndl hData );
  58.  
  59. // prototypes - state machine
  60.  
  61. void df_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  );
  62. void df_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  63. void df_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  64.  
  65. /* ======================================== */
  66.  
  67. // utility routines
  68.  
  69. /* ======================================== */
  70.  
  71. void df_bad(  t_nshc_parms *nshc_parms, int code )
  72. {
  73.     nshc_parms->action = nsh_stop;
  74.     nshc_parms->result = code;
  75. }
  76.  
  77. void df_good(  t_nshc_parms *nshc_parms )
  78. {
  79.     nshc_parms->action = nsh_stop;
  80.     nshc_parms->result = 0;
  81. }
  82.  
  83. int df_match( StringPtr from_os, StringPtr from_user )
  84. {
  85.     int len;
  86.     
  87.     len = from_user[0];
  88.     
  89.     if ( from_user[len] == ':' )
  90.         len--;
  91.     
  92.     if (len != from_os[0])
  93.         return( 0 );
  94.         
  95.     while (len) {
  96.         if ( from_os[len] != from_user[len] )
  97.             return(0);
  98.         len--;
  99.         }
  100.         
  101.     return(1);
  102. }
  103.  
  104. /* ======================================== */
  105.  
  106. // file access routines
  107.  
  108. /* ======================================== */
  109.  
  110. void df_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 name, HParamBlockRec *hfsParams)
  111. {
  112.     int        i;
  113.     long    kbytes, used, avail;
  114.     int        capacity;
  115.     
  116.     i = name[0];
  117.     
  118.     while (i < 27)
  119.         name[++i] = ' ';
  120.         
  121.     name[0] = i;
  122.     
  123.     kbytes = hfsParams->volumeParam.ioVNmAlBlks;
  124.     kbytes *= hfsParams->volumeParam.ioVAlBlkSiz;
  125.     kbytes /= 1024;
  126.     
  127.     avail = hfsParams->volumeParam.ioVFrBlk;
  128.     avail *= hfsParams->volumeParam.ioVAlBlkSiz;
  129.     avail /= 1024;
  130.     
  131.     used = kbytes - avail;
  132.     
  133.     capacity = ( used * 100 ) / kbytes;
  134.     
  135.     nshc_calls->NSH_putStr( name );
  136.     nshc_calls->NSH_printf( " %8ld %8ld %8ld", kbytes, used, avail );
  137.     nshc_calls->NSH_printf( " %3d%%\r", capacity );
  138. }
  139.  
  140. /* ======================================== */
  141.  
  142. int df_scan( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, short volumeIndex  )
  143. {            
  144.     HParamBlockRec    hfsParams;
  145.     OSErr            myErr;
  146.     Str255            name;
  147.  
  148.     hfsParams.volumeParam.ioNamePtr = name;
  149.     hfsParams.volumeParam.ioVRefNum = 0;
  150.     hfsParams.volumeParam.ioVolIndex = volumeIndex;
  151.     
  152.     myErr = PBHGetVInfoSync(&hfsParams);
  153.     
  154.     // a nsvErr indicates that the current pass is over
  155.     
  156.     if (myErr)
  157.         return( 0 );
  158.     
  159.     // process data
  160.     
  161.     df_display( nshc_parms, nshc_calls, name, &hfsParams );
  162.  
  163.     return( 1 );
  164. }
  165.  
  166. /* ======================================== */
  167.  
  168. int df_find( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 match  )
  169. {
  170.     HParamBlockRec    hfsParams;
  171.     short            volumeIndex;
  172.     OSErr            myErr;
  173.     Str255            name;
  174.     
  175.     volumeIndex = 0;
  176.     
  177.     for (;;) {
  178.     
  179.         volumeIndex++;
  180.             
  181.         // convert the volumeIndex into a vRefNum
  182.         
  183.         hfsParams.volumeParam.ioNamePtr = name;
  184.         hfsParams.volumeParam.ioVRefNum = 0;
  185.         hfsParams.volumeParam.ioVolIndex = volumeIndex;
  186.         
  187.         myErr = PBHGetVInfoSync(&hfsParams);
  188.         
  189.         // a nsvErr indicates that the current pass is over
  190.         
  191.         if (myErr)
  192.             return( 0 );
  193.         
  194.         // process data
  195.         
  196.         if ( df_match( name, match) ) {
  197.             df_display( nshc_parms, nshc_calls, name, &hfsParams );
  198.             return( 1 );
  199.             }
  200.             
  201.         }
  202. }
  203.  
  204. /* ======================================== */
  205.  
  206. // state machine - custom routines
  207.  
  208. /* ======================================== */
  209.  
  210. void df_by_arg( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, df_hndl hData )
  211. {
  212.     Str255    vName;
  213.  
  214.     if ((**hData).arg >= nshc_parms->argc)
  215.     
  216.         df_good( nshc_parms );
  217.         
  218.     else {
  219.     
  220.         if (!arg_to_str( nshc_parms, nshc_calls, (**hData).arg, vName ))
  221.             if (!df_find( nshc_parms, nshc_calls, vName )) {
  222.                 nshc_calls->NSH_putStr_err( "\pdf: Volume not found = ");
  223.                 nshc_calls->NSH_putStr_err( vName );
  224.                 nshc_calls->NSH_putchar_err( '\r' );
  225.                 }
  226.                 
  227.         (**hData).arg++;
  228.                 
  229.         }
  230. }
  231.  
  232. /* ======================================== */
  233.  
  234. void df_by_vol( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, df_hndl hData )
  235. {
  236.  
  237.     if (!df_scan( nshc_parms, nshc_calls, (**hData).volumeIndex ))
  238.         df_good(nshc_parms);
  239.             
  240.     (**hData).volumeIndex++;
  241. }
  242.  
  243. /* ======================================== */
  244.  
  245. // state machine - core routines
  246.  
  247. /* ======================================== */
  248.  
  249. void df_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  )
  250. {
  251.     df_hndl    hData;    // handle to hold our data
  252.     
  253.     nshc_parms->action = nsh_continue;
  254.  
  255.     hData = (df_hndl)NewHandleClear(sizeof(t_df_data));
  256.     
  257.     if (hData) {
  258.         (**hData).arg = 1;                    // start at the arg = 1 position
  259.         (**hData).volumeIndex = 1;            // start at the volume = 1 position
  260.         nshc_parms->data = (Handle)hData;
  261.         nshc_calls->NSH_putStr( "\pfilesystem                    kbytes     used    avail  capacity\r");
  262.         }
  263.     else {
  264.         nshc_calls->NSH_putStr_err( "\pdf: Could not allocate storage.\r" );
  265.         df_bad( nshc_parms, NSHC_ERR_MEMORY );
  266.         }
  267. }
  268.  
  269. /* ======================================== */
  270.  
  271. void df_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  272. {
  273.     df_hndl    hData;
  274.     
  275.     if (hData = (df_hndl)nshc_parms->data) {
  276.     
  277.         if ( nshc_parms->argc > 1 )        
  278.             df_by_arg( nshc_parms, nshc_calls, hData );    // we are searching by name
  279.         else                                 
  280.             df_by_vol( nshc_parms, nshc_calls, hData ); // we are scanning all volumes
  281.             
  282.         }
  283.     else
  284.         df_bad( nshc_parms, NSHC_ERR_MEMORY );
  285.  
  286. }
  287.  
  288. /* ======================================== */
  289.  
  290. void df_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  291. {
  292.     df_hndl    hData;
  293.     
  294.     if (hData = (df_hndl)nshc_parms->data)
  295.         DisposeHandle(nshc_parms->data);
  296.         
  297.     nshc_parms->action = nsh_idle;
  298. }
  299.  
  300. /* ======================================== */
  301.  
  302. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  303. {
  304. #ifdef __MWERKS__
  305.     long oldA4  = SetCurrentA4();
  306. #endif
  307.     
  308.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  309.     
  310.         switch (nshc_parms->action) {
  311.             case nsh_start:
  312.                 df_start(nshc_parms, nshc_calls);
  313.                 break;
  314.             case nsh_continue:
  315.                 df_continue(nshc_parms, nshc_calls);
  316.                 break;
  317.             case nsh_stop:
  318.                 df_stop(nshc_parms, nshc_calls);
  319.                 break;
  320.             }
  321.             
  322.         }
  323.     
  324. #ifdef __MWERKS__
  325.     SetA4(oldA4);
  326. #endif
  327. }
  328.  
  329. /* ======================================== */
  330.  
  331.