home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / POWERC.ZIP / DELTREE.C next >
C/C++ Source or Header  |  1995-02-08  |  4KB  |  130 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dos.h>
  4. #include <direct.h>
  5.  
  6. #define STDSTRLN 256
  7.  
  8. #define BOOLEAN char
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. #define TESTMODE FALSE    /* Test-Mode vs. Honest-to-God-Delete-Mode */
  13.  
  14. int wholedrive = FALSE;   /* whether or not to delete entire drive */
  15. /*
  16. ----------------------------------------------
  17. */
  18. void zaploop(char *filepath)  /* recursive erase / rmdir loop */
  19. {
  20.   char filename[STDSTRLN + 1], searchpathname[STDSTRLN + 1], *chrptr;
  21.   struct ffblk flrec;
  22.   union REGS intregs;
  23.   struct SREGS intsregs;
  24.   BOOLEAN fndresult;
  25.   strcpy(searchpathname, filepath);   /* compose file template to search on */
  26.   strcat(searchpathname, "\\*.*");
  27.   fndresult = findfirst(searchpathname, &flrec,
  28.               FA_DIREC + FA_RDONLY + FA_HIDDEN + FA_SYSTEM + FA_ARCH);
  29.  
  30.   while (!fndresult)    /* keep searching for files until there are no more */
  31.   {
  32.     if (flrec.ff_name[0] != '.')        /* ignore "." and ".." directories */
  33.     {
  34.       strcpy(filename, filepath);
  35.       strcat(filename, "\\");
  36.       strcat(filename, flrec.ff_name);
  37.       if (flrec.ff_attrib & FA_DIREC)   /* found directory: call recursive */
  38.       {
  39.         zaploop(filename);
  40.         if (TESTMODE) printf("%s\n", filename);
  41.                  else rmdir(filename);
  42.       }
  43.       else                              /* found file: erase it */
  44.       {
  45.         chrptr = &filename;
  46.         intsregs.ds = FP_SEG(chrptr);
  47.         intregs.x.dx = FP_OFF(chrptr);
  48.         if (TESTMODE)
  49.         {
  50.           printf("   %s\n", filename);
  51.         }
  52.         else
  53.         {
  54.           if (flrec.ff_attrib & (FA_HIDDEN + FA_SYSTEM + FA_RDONLY))
  55.           {
  56.             intregs.h.ah = 0x43;
  57.             intregs.h.al = 0x01;
  58.             intregs.h.ch = 0x00;
  59.             intregs.h.cl = 0x00;
  60.             intdosx(&intregs, &intregs, &intsregs);   /* set attr to erase */
  61.           }
  62.           remove(filename);
  63.         }
  64.       }
  65.     }
  66.     fndresult = findnext(&flrec);   /* looking for next file */
  67.   }
  68. }
  69. /*
  70. ----------------------------------------------
  71. */
  72. void setupzap(char *temp2zap)   /* prepare for recursive erase / rmdir */
  73. {
  74.   int ind;
  75.   char filepath[STDSTRLN + 1];
  76.   strcpy(filepath, temp2zap);
  77.   ind = strlen(filepath) - 1;
  78.   
  79.   /* kill backslashes at end of path */
  80.   
  81.   while ((ind >= 0) && (filepath[ind] == '\\')) filepath[ind--] = '\0';
  82.   
  83.   /* verify that we're not trying to DELTREE a root directory without
  84.      the WHOLEDRIVE paramter */
  85.   
  86.   if (((filepath[0] == '\0') || (filepath[ind] == ':')) && (!wholedrive))
  87.      printf("I won't try to eliminate an entire drive.\n");
  88.   else
  89.   {
  90.     zaploop(filepath);              /* start recursive erase / rmdir */
  91.     if (!wholedrive)                /* don't try to delete root itself! */
  92.     {
  93.       if (TESTMODE) printf("%s\n", filepath);
  94.                else rmdir(filepath);
  95.     }
  96.   }
  97. }
  98. /*
  99. ----------------------------------------------
  100. */
  101. void main(int argc, char *argv[])
  102. {
  103.   char cnfrm;
  104.   if (TESTMODE) printf("(Note: this program running in TEST MODE.)\n");
  105.   if (argc < 2)
  106.   {
  107.     printf("\nDELTREE\n\n");
  108.     printf("Purpose: to remove all files / subdirectories from the \n");
  109.     printf("         specified path\n\n");
  110.     printf("Usage:   DELTREE path [WHOLEDRIVE]\n\n");
  111.     printf("Example: \"DELTREE C:\\UTIL\" will eliminate all files in\n");
  112.     printf("         C:\\UTIL, as well as clearing out/removing all\n");
  113.     printf("         subdirectories under and including C:\\UTIL.  The\n");
  114.     printf("         WHOLEDRIVE option is needed to delete all files on\n");
  115.     printf("         a drive, and the path must consist of only the\n");
  116.     printf("         drive name (example: \"DELTREE C: WHOLEDRIVE\").\n\n");
  117.   }
  118.   else
  119.   {
  120.     strupr(argv[1]);
  121.     if (!strcmpi(argv[2], "WHOLEDRIVE")) wholedrive = TRUE;
  122.     printf("\nConfirm: eliminate %s? (y/n) ", argv[1]);
  123.     scanf("%c", &cnfrm);
  124.     printf("\n");
  125.     strupr(&cnfrm);
  126.     if (cnfrm == 'Y') setupzap(argv[1]);
  127.   }
  128. }
  129.  
  130.