home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / WIPEDISK.ZIP / WIPEDISK.C < prev    next >
C/C++ Source or Header  |  1992-03-13  |  5KB  |  176 lines

  1. /*----------------------------------------------------------------------------
  2.  -----  (C) Copyright    Baxter Diagnostics 1991                        ------
  3.  -----------------------------------------------------------------------------
  4.  -----  Project       : Status IIntellect (tm)
  5.  -----
  6.  -----  Function      : Query Directory file size
  7.  -----
  8.  -----  AUTHOR        : Nick Bethman - Wed  02-26-1992
  9.  -----
  10.  ---------------------------------------------------------------------------*/
  11. #define INCL_DOSFILEMGR
  12. #define INCL_DOSMISC
  13. #define INCL_DOSDEVICES
  14. #include <os2.h>
  15. #ifndef BSEDEV_INCLUDED
  16. #include <bsedev.h>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22.  
  23. #define MAXDEPTH 15
  24. #define NOTREMOVE 0xfffe
  25. #define FATTRFIND (FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY)
  26. #define FATTRRESET (FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM)
  27.  
  28.      /* this will work with 2.0 or 1.2+ */
  29. #ifdef INCL_32
  30.   #define DosDevIO( hDev, cat, fun, pParm, cbParmSize, pcbParmLen, \
  31.                     pData, cbDataSize, pcvDataLen ) \
  32.      DosDevIOCtl( hDev, cat, fun, pParm, cbParmSize, pcbParmLen, \
  33.                   pData, cbDataSize, pcvDataLen )
  34. #else
  35.   #define DosDevIO( hDev, cat, fun, pParm, cbParmSize, pcbParmLen, \
  36.                     pData, cbDataSize, pcvDataLen ) \
  37.      DosDevIOCtl2( pData, cbDataSize, pParm, cbParmSize, fun, cat, hDev )
  38. #endif
  39.  
  40.  
  41. /*----------------------------------------------------------------------------
  42. * Is a drive removeable.
  43. *---------------------------------------------------------------------------*/
  44. unsigned NEAR isRemoveableDrive( char drive,
  45.                                  BOOL *isRemoveable )
  46. {
  47. char     dname[3];
  48. HFILE    handle;
  49. unsigned result;
  50. int      data = 0;
  51. int      parms = 0;
  52. unsigned dataIO = sizeof(data);
  53. unsigned parmIO = sizeof(parms);
  54. unsigned action;
  55.  
  56.   dname[0] = drive;
  57.   dname[1] = ':';
  58.   dname[2] = 0;
  59.  
  60.  
  61.   result = DosOpen( dname, &handle, &action, 0,
  62.                     FILE_NORMAL, FILE_OPEN,
  63.                     OPEN_ACCESS_READWRITE | OPEN_FLAGS_DASD | OPEN_SHARE_DENYWRITE, 0 );
  64.   if ( result == 0 ) {
  65.     result = DosDevIO( handle, IOCTL_DISK, DSK_BLOCKREMOVABLE, 
  66.                        &parms, sizeof(parms), &parmIO,
  67.                        &data, sizeof(data), &dataIO );
  68.     *isRemoveable = (data == 0);
  69.     DosClose( handle );
  70.   }
  71.  
  72.   return( result );
  73.  
  74. } /* isRemoveableDrive */
  75.  
  76.  
  77. unsigned NEAR wipeDir( char    *path,
  78.                        unsigned depth )
  79. {
  80. HDIR         hdir = HDIR_CREATE;
  81. FILEFINDBUF  buffer;
  82. unsigned     count = 1;
  83. unsigned     err;
  84. unsigned     pathLen;
  85. char         fullName[CCHMAXPATH];
  86. static char  EADATA[] = "EA DATA. SF";
  87.  
  88.   if ( depth == MAXDEPTH )
  89.     return( 1 );
  90.  
  91.   puts( path );
  92.  
  93.   pathLen = strlen( path ) - 1;  /* ingnore the '*' */
  94.   strcpy( fullName, path );
  95.   err = DosFindFirst( path, &hdir, FATTRFIND,
  96.                       &buffer, sizeof(buffer), &count, 0L );
  97.   if ( err != 0 )
  98.     printf( "file err %u\n", err );
  99.   else {
  100.     do {
  101.       strcpy( &fullName[pathLen], buffer.achName );
  102.       if ( (buffer.attrFile & FATTRRESET) &&
  103.            strcmp( buffer.achName, EADATA ) != 0 )
  104.         DosSetFileMode( fullName, FILE_NORMAL, 0 );
  105.       if ( buffer.attrFile & FILE_DIRECTORY ) {
  106.         if ( strcmp( buffer.achName, "." ) != 0 &&
  107.              strcmp( buffer.achName, ".." ) != 0 ) {
  108.           strcat( fullName, "\\*" );
  109.           wipeDir( fullName, depth+1 );
  110.              /* rip \* off and remove it */
  111.           strcpy( &fullName[pathLen], buffer.achName );
  112.           err = DosRmDir( fullName, 0 );
  113.           if ( err != 0 )
  114.             printf( "%s remove err %u\n", path, err );
  115.         }
  116.       }else if ( strcmp( buffer.achName, EADATA ) != 0 ) {
  117.         printf( fullName );
  118.         err = DosDelete( fullName, 0 );
  119.         if ( err != 0 )
  120.           printf( "err %u", err );
  121.         printf( "\n" );
  122.       }
  123.     }while ( DosFindNext( hdir, &buffer, sizeof(buffer), &count ) == 0 );
  124.     DosFindClose( hdir );
  125.   }
  126.  
  127.   return( err );
  128.  
  129. }  /* wipeDir */
  130.  
  131.  
  132. unsigned NEAR WipeDisk( unsigned drive )
  133. {
  134. static char  path[] = "x:\\*";
  135. unsigned err;
  136. BOOL     removeable;
  137.  
  138.      /* don't let them wap any hard drives */
  139.   err = isRemoveableDrive( (char)drive, &removeable );
  140.   if ( err != 0 )
  141.     return( err );
  142.   else if ( !removeable )
  143.     return( NOTREMOVE );
  144.  
  145.   path[0] = (char)drive;
  146.   wipeDir( path, 1 );
  147.   return( 0 );
  148.  
  149. }  /* WipeDisk */
  150.  
  151.  
  152. unsigned _cdecl main( int   argc,
  153.                       char *argv[] )
  154. {
  155. unsigned err;
  156.  
  157.   DosError( FERR_DISABLEHARDERR );
  158.  
  159.   puts( "Wipe Disk v1.0, Copyright Ngb Technologies, 1992, All rights reserved\n" );
  160.   if ( argc >= 2 ) {
  161.     err = WipeDisk( (char)toupper( argv[1][0] ) );
  162.     if ( err == NOTREMOVE )
  163.       printf( "Drive %c is not removeable, will not wipe!\n", argv[1][0] );
  164.     else if ( err != 0 )
  165.       printf( "Drive %c is not accessable, err %u, will not wipe!\n",
  166.                argv[1][0], err );
  167.   }else {
  168.     err = 0;
  169.     puts( "No drive specified. try: wipedisk a" );
  170.   }
  171.  
  172.   return( err );
  173.  
  174. }  /* main */
  175.  
  176.