home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / RM_ALL.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  4KB  |  158 lines

  1. /*
  2. **  Remove all files and (optionally) subdirectories
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <io.h>
  11. #include <dos.h>
  12. #include <ctype.h>
  13.  
  14. #define LAST_CHAR(str) (str[strlen(str) - 1])
  15. #define MAX_PATH 80
  16.  
  17. #ifdef __TURBOC__
  18.  #include <dir.h>
  19.  #define find_1st(n,a,b) (findfirst((n),(b),(a)))
  20.  #define find_nxt(b) (findnext(b))
  21.  #define find_t ffblk
  22.  #define name ff_name
  23.  #define attrib ff_attrib
  24.  #define _A_SUBDIR FA_DIREC
  25. #else
  26.  #include <direct.h>
  27.  #define find_1st(n,a,b) (_dos_findfirst((n),(a),(b)))
  28.  #define find_nxt(b) (_dos_findnext(b))
  29. #endif
  30.  
  31. /* Select one of the following - remove() is ANSI       */
  32.  
  33. #define rmfunc remove
  34. /* #define rmfunc unlink */
  35.  
  36. #define show(s) fputs((s), stderr)
  37.  
  38. typedef enum {ERROR = -1, SUCCESS, FALSE = 0, TRUE} LOGICAL;
  39. LOGICAL recurse = FALSE, gobble = FALSE;
  40.  
  41. char *mask = "*.*";
  42.  
  43. /*
  44. **  Clean all files from a directory
  45. */
  46.  
  47. void clean_dir(char *path)
  48. {
  49.       char rmpath[MAX_PATH], *rmfile;
  50.       struct find_t fbuf;
  51.  
  52.       strcpy(rmpath, path);
  53.       if ('\\' != LAST_CHAR(rmpath))
  54.             strcat(rmpath, "\\");
  55.       rmfile = &rmpath[strlen(rmpath)];
  56.       strcpy(rmfile, mask);
  57.       if (0 == find_1st(rmpath, 0, &fbuf)) do
  58.       {
  59.             strcpy(rmfile, fbuf.name);
  60.             rmfunc(rmpath);
  61.             printf("deleting %s\n", rmpath);
  62.       } while (0 == find_nxt(&fbuf));
  63. }
  64.  
  65. /*
  66. **  Process directories
  67. */
  68.  
  69. void do_dir(char *path)
  70. {
  71.       char search[MAX_PATH], new[MAX_PATH];
  72.       struct find_t ff;
  73.  
  74.       strcpy(search, path);
  75.       if ('\\' != LAST_CHAR(search))
  76.             strcat(search, "\\");
  77.       strcat(search, "*.*");
  78.       if (SUCCESS == find_1st(search, 0xff, &ff)) do
  79.       {
  80.             if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
  81.             {
  82.                   strcat(strcat(strcpy(new, path), "\\"), ff.name);
  83.                   do_dir(new);
  84.             }
  85.       } while (SUCCESS == find_nxt(&ff));
  86.       clean_dir(path);
  87.       if (gobble)
  88.             rmdir(path);
  89. }
  90.  
  91. /*
  92. **  Tell 'em they messed up
  93. */
  94.  
  95. void usage(LOGICAL errstat)
  96. {
  97.       if (errstat)
  98.             fputc('\a', stderr);
  99.       show("Usage: RM_ALL directory [...directory] [-eFNAME.EXT] [-rg?]\n");
  100.       show("switches: -eFNAME.EXT  Remove only files matching mask "
  101.             "(default is \"-e*.*\")\n");
  102.       show("          -r           Recurse subdirectories\n");
  103.       show("          -g           Gobble (delete) empty subdirectories\n");
  104.       show("          -?           Display help (this message)\n");
  105.       exit(errstat);
  106. }
  107.  
  108. /*
  109. **  RM_ALL - Deletes all files and (optionally) subdirectories
  110. */
  111.  
  112. int main(int argc, char *argv[])
  113. {
  114.       char rmpath[MAX_PATH], *rmfile;
  115.       int i;
  116.       LOGICAL found_dir = FALSE;
  117.       void (*clean_func)(char *) = clean_dir;
  118.  
  119.       for (i = 1; i < argc; ++i)          /* Check for switches         */
  120.       {
  121.             if (NULL == strchr("-/", *argv[i]))
  122.                   continue;               /* Assume it's a filename     */
  123.             switch (toupper(argv[i][1]))
  124.             {
  125.             case 'R':
  126.                   clean_func = do_dir;
  127.                   break;
  128.  
  129.             case 'E':
  130.                   if (2 < strlen(argv[i]))
  131.                         mask = strupr(&argv[i][2]);
  132.                   else  usage(ERROR);
  133.                   break;
  134.  
  135.             case 'G':
  136.                   gobble = TRUE;
  137.                   break;
  138.  
  139.             case '?':
  140.                   usage(FALSE);
  141.                   break;
  142.  
  143.             default:
  144.                   usage(ERROR);
  145.             }
  146.       }
  147.       for (i = 1; i < argc; ++i)          /* Scan filenames             */
  148.       {
  149.             if (strchr("/-", *argv[i]))
  150.                   continue;
  151.             found_dir = TRUE;
  152.             clean_func(argv[i]);
  153.       }
  154.       if (!found_dir)
  155.             usage(TRUE);
  156.       else  return 0;
  157. }
  158.