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