home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / RM_ALL.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  199 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  Remove all files and (optionally) subdirectories
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13. #include <dos.h>
  14. #include <ctype.h>
  15. #include "sniptype.h"
  16. #include "dirport.h"
  17. #if defined(MSDOS) || defined(__MSDOS__)
  18.  #include "unistd.h"
  19. #else
  20.  #include <unistd.h>
  21. #endif
  22.  
  23. #define MAX_PATH 80
  24.  
  25. #if (defined(_MSC_VER) && (_MSC_VER >= 700)) || (defined(__SC__))
  26.  /* Make FP_xxx macros lvalues as in older versions */
  27.  #undef FP_SEG
  28.  #undef FP_OFF
  29.  #define FP_SEG(fp)    ((unsigned)((unsigned long)(fp) >> 16))
  30.  #define FP_OFF(fp)    ((unsigned)(fp && 0xffff))
  31. #endif
  32.  
  33. /* Select one of the following - remove() is ANSI       */
  34.  
  35. #define rmfunc remove
  36. /* #define rmfunc unlink */
  37.  
  38. #define show(s) fputs((s), stderr)
  39.  
  40. Boolean_T recurse = False_, gobble = False_, ignore = False_;
  41.  
  42. char *mask = "*.*";
  43.  
  44. /*
  45. **  Clean all files from a directory
  46. */
  47.  
  48. void clean_dir(char *path)
  49. {
  50.       char rmpath[MAX_PATH], *rmfile;
  51.       DOSFileData fbuf;
  52.       unsigned attrib = (ignore) ? _A_ANY : 0;
  53.  
  54.       strcpy(rmpath, path);
  55.       if ('\\' != LAST_CHAR(rmpath))
  56.             strcat(rmpath, "\\");
  57.       rmfile = &rmpath[strlen(rmpath)];
  58.       strcpy(rmfile, mask);
  59.       if (0 == FIND_FIRST(rmpath, attrib, &fbuf)) do
  60.       {
  61.             strcpy(rmfile, ff_name(&fbuf));
  62.             if (ignore)
  63.             {
  64.                   union REGS regs;
  65.                   struct SREGS sregs;
  66.  
  67.                   regs.x.ax = 0x4300;
  68.                   regs.x.dx = FP_OFF((char FAR *)rmpath);
  69.                   segread(&sregs);
  70.                   sregs.ds  = FP_SEG((char FAR *)rmpath);
  71.                   intdosx(®s, ®s, &sregs);
  72.                   if (!regs.x.cflag)
  73.                   {
  74.                         regs.x.ax  = 0x4301;
  75.                         regs.x.cx &= ~(_A_RDONLY | _A_HIDDEN | _A_SYSTEM);
  76.                         intdosx(®s, ®s, &sregs);
  77.                         if (regs.x.cflag)
  78.                               printf("unable to delete %s\n", rmpath);
  79.                   }
  80.             }
  81.             rmfunc(rmpath);
  82.             printf("deleting %s\n", rmpath);
  83.       } while (0 == FIND_NEXT(&fbuf));
  84. }
  85.  
  86. /*
  87. **  Process directories
  88. */
  89.  
  90. void do_dir(char *path)
  91. {
  92.       char search[MAX_PATH], new[MAX_PATH];
  93.       DOSFileData ff;
  94.  
  95.       strcpy(search, path);
  96.       if ('\\' != LAST_CHAR(search))
  97.             strcat(search, "\\");
  98.       strcat(search, "*.*");
  99.       if (Success_ == FIND_FIRST(search, _A_ANY, &ff)) do
  100.       {
  101.             if (ff_attr(&ff) & _A_SUBDIR && '.' != *ff_name(&ff))
  102.             {
  103.                   strcpy(new, path);
  104.                   if ('\\' != LAST_CHAR(new))
  105.                         strcat(new, "\\");
  106.                   strcat(new, ff_name(&ff));
  107.                   do_dir(new);
  108.             }
  109.       } while (Success_ == FIND_NEXT(&ff));
  110.       clean_dir(path);
  111.       if (gobble)
  112.             rmdir(path);
  113. }
  114.  
  115. /*
  116. **  Tell 'em they messed up
  117. */
  118.  
  119. void usage(Boolean_T errstat)
  120. {
  121.       if (errstat)
  122.             fputc('\a', stderr);
  123.       show("Usage: RM_ALL directory [...directory] [-eFNAME.EXT] [-rgi?]\n");
  124.       show("switches: -eFNAME.EXT  Remove only files matching mask "
  125.             "(default is \"-e*.*\")\n");
  126.       show("          -r           Recurse subdirectories\n");
  127.       show("          -g           Gobble (delete) empty subdirectories\n");
  128.       show("          -i           Ignore special file attributes "
  129.             "(CAUTION!)\n");
  130.       show("          -?           Display help (this message)\n");
  131.       exit(errstat);
  132. }
  133.  
  134. /*
  135. **  RM_ALL - Deletes all files and (optionally) subdirectories
  136. */
  137.  
  138. int main(int argc, char *argv[])
  139. {
  140.       int i, j;
  141.       Boolean_T found_dir = False_;
  142.       void (*clean_func)(char *) = clean_dir;
  143.  
  144.       for (i = 1; i < argc; ++i)          /* Check for switches         */
  145.       {
  146.             if (NULL == strchr("-/", *argv[i]))
  147.                   continue;               /* Assume it's a filename     */
  148.             for (j = 1; argv[i][j] ; ++j) /* Traverse nested switches   */
  149.             {
  150.                   switch (toupper(argv[i][j]))
  151.                   {
  152.                   case 'R':
  153.                         clean_func = do_dir;
  154.                         break;
  155.  
  156.                   case 'G':
  157.                         gobble = True_;
  158.                         break;
  159.  
  160.                   case 'I':
  161.                         ignore = True_;
  162.                         break;
  163.  
  164.                   case '?':
  165.                         puts("***help***");
  166.                         usage(False_);
  167.                         break;
  168.  
  169.                   case 'E':
  170.                         if (0 == strlen(&argv[i][++j]))
  171.                         {
  172.                               puts("***no file***");
  173.                               usage(Error_);                /* Oops     */
  174.                         }
  175.                         mask = strupr(&argv[i][j]);
  176.                         j += strlen(&argv[i][j]) - 1; /* End of switch  */
  177.                         break;
  178.  
  179.                   default:
  180.                         puts("***default***");
  181.                         usage(Error_);
  182.                   }
  183.             }
  184.       }
  185.       for (i = 1; i < argc; ++i)          /* Scan filenames             */
  186.       {
  187.             if (strchr("/-", *argv[i]))
  188.                   continue;
  189.             found_dir = True_;
  190.             clean_func(argv[i]);
  191.       }
  192.       if (!found_dir)
  193.       {
  194.             puts("***not found***");
  195.             usage(True_);
  196.       }
  197.       return 0;
  198. }
  199.