home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / format / P_FORMAT.C < prev    next >
C/C++ Source or Header  |  1993-01-27  |  3KB  |  125 lines

  1. /*
  2.  * Format the device given on the command line.
  3.  * Copyright(C)1993 Advidea Ventures
  4.  * Portions Copyright(C) Psion Plc.
  5.  *
  6.  * Author  :  Russ Beinder
  7.  * Language:  C/PLIB
  8.  * Created :  93.01.27
  9.  *
  10.  * Designed to be run from MCLINK. Tested with MCLINK v3.0.
  11.  * Must be located in the root directory on the default device.
  12.  *
  13.  * Use the following command format from MCLINK:
  14.  *    RUN P_FORMAT LOC::<device>\<name><ext>
  15.  *
  16.  * Example:
  17.  *    RUN P_FORMAT LOC::A:\MYDISK
  18.  */
  19.  
  20. #include <plib.h>
  21.  
  22. GLREF_C TEXT *DatCommandPtr;     /* command line */
  23.  
  24. /*
  25.  * ============================================================================
  26.  * Routine:  formatDevice
  27.  * Purpose:  Format the given device and assign the given volume name.
  28.  *           Taken from pg 126, Psion 'C' SDK, v1.10
  29.  *   Parms:  name - LOC::<device>\<name><ext>
  30.  * Returns:  none
  31.  * ============================================================================
  32.  */
  33. LOCAL_C VOID formatDevice( TEXT *name ) {
  34.  
  35.    INT err, i, dummy;
  36.    UWORD count;
  37.    VOID *chan;
  38.    TEXT bb[E_MAX_ERROR_TEXT_SIZE];
  39.    TEXT buf[128] = "Format device ";
  40.  
  41.    chan = NULL;
  42.  
  43.    p_scat( buf, name );
  44.    err = p_notify( buf, "Are you sure?", "Abort", "Continue", NULL );
  45.    if ( err == 0 )
  46.       return;
  47.  
  48.    p_printf( "Formatting %s ", name );
  49.  
  50.    err = p_open( &chan, name, P_FFORMAT );
  51.    if ( err >= 0 ) {
  52.  
  53.       err = p_read( chan, &count, 0 );
  54.  
  55.       if ( err >= 0 ) {
  56.  
  57.          i = 1;
  58.          err = p_read( chan, &dummy, 0 );
  59.          while ( err >= 0 ) {
  60.             p_print( "\r%u%%", (UWORD)( i++ * 100 / count ));
  61.             err = p_read( chan, &count, 0 );
  62.          }
  63.  
  64.          if ( err == E_FILE_EOF )
  65.             err = 0;
  66.  
  67.       }
  68.    }
  69.  
  70.    p_close( chan );
  71.  
  72.    if ( err < 0 ) {
  73.       p_errs( &bb[0], err );
  74.       p_printf( "\r\nFormat failed: %s", &bb[0] );
  75.       p_scpy( buf, "Format failed " );
  76.       p_scat( buf, name );
  77.       p_notifyerr( err, buf, "Continue", NULL, NULL );
  78.    } else {
  79.       p_printf( "\r\nFormat complete." );
  80.    }
  81.  
  82.    p_getch();
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /*
  91.  * ============================================================================
  92.  * Routine:  main
  93.  * ============================================================================
  94.  */
  95. GLDEF_C INT main(VOID) {
  96.  
  97.    TEXT *pb;
  98.    TEXT cmdline[129];
  99.    TEXT device[129];
  100.  
  101. /* Get command line */
  102. /* DatCommandPtr points to a zero terminated string containing the
  103.  * name of the file that is being executed, followed by a length byte
  104.  * preceeded string containing the actual command line.
  105.  */
  106.    pb = DatCommandPtr + p_slen(DatCommandPtr) + 1;
  107.    p_bcpy( cmdline, pb + 1, *pb );
  108.    cmdline[*pb] = 0;
  109.  
  110.    if ( p_slen( cmdline ) == 0 ) {
  111.  
  112.       p_printf( "Usage:\r\n P_FORMAT [LOC::]<device>[\<name><ext>]" );
  113.       p_getch();
  114.       return 0;
  115.  
  116.    } else {
  117.  
  118.       p_fparse( cmdline, "LOC::", device, NULL );
  119.  
  120.       formatDevice( device );
  121.  
  122.       return(0);
  123.    }
  124. }
  125.