home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NUKE21.ZIP / NUKE.C < prev    next >
C/C++ Source or Header  |  1992-02-26  |  5KB  |  144 lines

  1. #define INCL_BASE
  2. #define INCL_SUB
  3. #define INCL_DOSFILEMGR
  4.  
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #define F_ALL_TYPES  FILE_NORMAL | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED
  10.  
  11. /* I like to rename the string comparison functions */
  12. #define STREQUAL(  x, y    )    ( !strcmp(  (x), (y)      ) )
  13. #define STRNEQUAL( x, y, z )    ( !strncmp( (x), (y), (z) ) )
  14.  
  15.  
  16. /* a little bit of global data. */
  17. ULONG       NumOfSubs;
  18. ULONG       NumOfFiles;
  19. USHORT      SearchCount = 1;
  20.  
  21.  
  22. /* function prototypes */
  23. BOOL  RemoveBranches( PCHAR root, PCHAR DirName );
  24. PCHAR MakeFullFileName( PCHAR path, PCHAR filename );
  25.  
  26.  
  27.  
  28.  
  29. VOID main( int argc, PCHAR argv[] )
  30. {
  31.    HDIR        hdir = HDIR_SYSTEM;
  32.    FILEFINDBUF findbuf;
  33.    USHORT      RetCode;
  34.    KBDKEYINFO  kbci;
  35.  
  36.    if ( argc == 1 ) {
  37.       printf( "NUKE ver. 2.1, by Gene Williams, Jr., CPA\n" );
  38.       printf( "NUKE removes entire subdirectory trees, and the files in them.\n\n" );
  39.       printf( "usage is:  NUKE subdirectory\n" );
  40.       printf( "where 'subdirectory' is directly under the current subdirectory.\n\n" );
  41.       printf( "This program is bound; it works under both DOS and OS/2.\n" );
  42.       DosExit( EXIT_PROCESS, 0 );
  43.    }
  44.  
  45.    RetCode = DosFindFirst( argv[1], &hdir, FILE_DIRECTORY, &findbuf, sizeof(findbuf), &SearchCount, 0L );
  46.    if ( RetCode || ( !RetCode && (findbuf.attrFile != FILE_DIRECTORY) ) ) {
  47.       printf( "\tCould not find subdirectory %s\n\a", argv[1] );
  48.       DosExit( EXIT_PROCESS, 0 );
  49.    }
  50.  
  51.    NumOfSubs = NumOfFiles = 0;
  52.  
  53.    printf( "Pay close attention.  You are about to destroy %s.\n", argv[1] );
  54.    printf( "If you are intent upon doing this, press '!'\n\a" );
  55.    KbdCharIn( &kbci, IO_WAIT, 0 );
  56.    printf( "\n" );
  57.  
  58.    if ( kbci.chChar != '!' ) {
  59.       printf( "Wise decision.  Aborting...\n" );
  60.    } else {
  61.       if ( RemoveBranches( argv[1], (PCHAR) NULL ) && !DosRmDir( argv[1], 0L ) ) {
  62.          printf( "\tComplete!   " );
  63.          NumOfSubs++;
  64.       } else {
  65.          printf( "Could not delete entire subdirectory tree.\n" );
  66.          printf( "Look for hidden files, or (in OS/2) another process that is using this tree.\n\a" );
  67.       }
  68.  
  69.       printf( "Removed %ld director%s, %ld file%s.\n", NumOfSubs, NumOfSubs == 1 ? "y" : "ies", NumOfFiles, NumOfFiles == 1 ? "" : "s" );
  70.       DosFindClose( hdir );
  71.    }
  72.  
  73.    DosExit( EXIT_PROCESS, 0 );
  74. }
  75.  
  76.  
  77. BOOL RemoveBranches( PCHAR root, PCHAR DirName )
  78. {
  79.    char        path[CCHMAXPATH];
  80.    char        fullFilename[CCHMAXPATH];
  81.    HDIR        hdir = HDIR_SYSTEM;
  82.    FILEFINDBUF findbuf;
  83.    BOOL        result;
  84.    BOOL        done = FALSE;
  85.  
  86.    strcpy( path, MakeFullFileName( root, DirName ) );
  87.    strcpy( fullFilename, MakeFullFileName( path, "*.*" ) );
  88.  
  89.    SearchCount = 1;
  90.    if ( !(DosFindFirst( fullFilename, &hdir, F_ALL_TYPES, &findbuf, sizeof(findbuf), &SearchCount, 0L ) ) ) {
  91.       do {
  92.          if ( findbuf.attrFile == FILE_DIRECTORY ) {
  93.             if ( !STREQUAL( findbuf.achName, "." ) && !STREQUAL( findbuf.achName, ".." ) ) {
  94.                RemoveBranches( path, findbuf.achName );
  95.                if ( DosRmDir( MakeFullFileName( path, findbuf.achName ), 0L ) ) {
  96.                   DosFindClose( hdir );
  97.                   return FALSE;
  98.                } else
  99.                   NumOfSubs++;
  100.                /* DosFindFirst doesn't accept recursion, so we */
  101.                /* have to call it again after a recursive call.*/
  102.                strcpy( fullFilename, MakeFullFileName( path, "*.*" ) );
  103.                SearchCount = 1;
  104.                hdir = HDIR_SYSTEM;
  105.                if ( DosFindFirst( fullFilename, &hdir, F_ALL_TYPES, &findbuf, sizeof(findbuf), &SearchCount, 0L ) ) {
  106.                   done = TRUE;
  107.                }
  108.             }
  109.          } else {
  110.             strcpy( fullFilename, MakeFullFileName( path, findbuf.achName ) );
  111.             if ( (findbuf.attrFile != FILE_NORMAL) && (findbuf.attrFile != (FILE_NORMAL | FILE_ARCHIVED )) )
  112.                DosSetFileMode( fullFilename, FILE_NORMAL, 0L );
  113.             if ( DosDelete( fullFilename, 0L ) ) {
  114.                DosFindClose( hdir );
  115.                return FALSE;
  116.             } else
  117.                NumOfFiles++;
  118.          }
  119.       }
  120.       while ( !done && !(DosFindNext( hdir, &findbuf, sizeof(findbuf), &SearchCount) ) );
  121.  
  122.       result = TRUE;
  123.    } else
  124.       result = FALSE;
  125.  
  126.    DosFindClose( hdir );
  127.    return result;
  128. }
  129.  
  130.  
  131. PCHAR MakeFullFileName( PCHAR path, PCHAR filename )
  132. {
  133.    static char far fullname[CCHMAXPATH];
  134.  
  135.    strcpy( fullname, path );
  136.    if ( filename != NULL ) {
  137.       if ( fullname[strlen( fullname )-1] != '\\' ) {
  138.          strcat( fullname, "\\" );
  139.       }
  140.       strcat( fullname, filename );
  141.    }
  142.    return fullname;
  143. }
  144.