home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / util / cdro / 003 / cleanup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  1.5 KB  |  76 lines

  1. #include <stdio.h>
  2. #include <dir.h>
  3. #include <dos.h>
  4. #include <io.h>
  5. #include <sys\stat.h>
  6.  
  7. /* cleanup wipes everything in the given directory including subdirectories */
  8. /* It doesn't remove the directory itself                                   */
  9. /* The cleanup procedure calls itself again for removing subdirectories     */
  10.  
  11. void cleanup(char *dir)
  12. {
  13.     char    fname[BUFSIZ];
  14.     char    finame[BUFSIZ];
  15.     static    int    level=0;
  16.  
  17.     struct  ffblk    fcblk;
  18.  
  19.     sprintf (fname,"%s\\*.*",dir);
  20.  
  21.     if (findfirst(fname,&fcblk,FA_DIREC|FA_HIDDEN|FA_SYSTEM|FA_RDONLY) == 0)
  22.     {
  23.         if (fcblk.ff_attrib == FA_DIREC)
  24.         {
  25.             if (strcmp(fcblk.ff_name,"."))
  26.             {
  27.                 if (strcmp(fcblk.ff_name,".."))
  28.                 {
  29.                     sprintf (fname,"%s\\%s",dir,fcblk.ff_name);
  30.                     level++;
  31.                     cleanup(fname);
  32.                 }
  33.             }
  34.         }
  35.         else
  36.         {
  37.             printf ("Removing %s\\%s\n",dir,fcblk.ff_name);
  38.             sprintf (finame,"%s\\%s",dir,fcblk.ff_name);
  39.             _chmod (finame,1,0);
  40.             unlink (finame);
  41.         }
  42.  
  43.         while (findnext(&fcblk) == 0)
  44.         {
  45.             if (fcblk.ff_attrib == FA_DIREC)
  46.             {
  47.                 if (strcmp(fcblk.ff_name,"."))
  48.                 {
  49.                     if (strcmp(fcblk.ff_name,".."))
  50.                     {
  51.                         sprintf (fname,"%s\\%s",dir,fcblk.ff_name);
  52.                         level++;
  53.                         cleanup(fname);
  54.                     }
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 printf ("Removing %s\\%s\n",dir,fcblk.ff_name);
  60.                 sprintf (finame,"%s\\%s",dir,fcblk.ff_name);
  61.                 _chmod (finame,1,0);
  62.                 unlink (finame);
  63.             }
  64.         }
  65.     }
  66.  
  67.     if (level > 0)
  68.     {
  69.         printf ("Removing dir %s\n",dir);
  70.         rmdir (dir);
  71.     }
  72.  
  73.     level--;
  74. }
  75.  
  76.