home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / clndsk10.zip / CLEANDSK.C next >
Text File  |  1996-04-02  |  5KB  |  153 lines

  1. /* cleandsk.c (emx+gcc) (c) 1996 William D. Ezell */
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <ftw.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include <fnmatch.h>
  9.  
  10. #define DEBUG              /* when defined compilation will produce
  11.                               non-destructive version of program */
  12.  
  13. #undef DEBUG                /* enable production version compile */
  14.  
  15. #define WORKBUFSIZE 256    /* size for work buffers */
  16.  
  17. /* prototypes */
  18. static int walker (const char *name, const struct stat *st, int flag);
  19.  
  20. /* pattern masks designating files to search for -- TERMINATE LIST WITH \0 */
  21. char *refmasks[] = { "*.old",
  22.                      "*.tmp",
  23.                      "*.syd",
  24.                      "*.fix",
  25.                      "*.chk",
  26.                      "*.??_",
  27.                      "*.~??",
  28.                      "~*.*",
  29.                      "\0"
  30.                    };
  31.  
  32. /* cleandsk: main function */
  33. int main (int argc, char *argv[])
  34. {
  35.    int rc, i;                 /* ftw() return value, misc index var */
  36.    char ans[WORKBUFSIZE];     /* user response to continue prompt */
  37.    char path[WORKBUFSIZE]; /* buffer for formation of drive/path to scan */
  38.  
  39.    if (argc > 1) {
  40.       /* use command-line argument if present for drive/path specification */
  41.       strcpy (path, argv[1]);
  42.    } else {
  43.       /* otherwise default to root of drive C: (using UNIX-style path sep) */
  44.       strcpy (path, "c:/");
  45.    } 
  46.  
  47.    /* perform a crude screen clear and display warning */
  48.    for (i=0; i < 15; i++) {
  49.       printf ("\n");
  50.    }
  51.    
  52.    printf ("CLEANDSK 1.0  (c) 1996 William D. Ezell\n");
  53.    printf ("Address comments or suggestions to  wdezell@ibm.net\n\n");
  54.    printf ("WARNING:    This program will delete ALL files which match the following\n");
  55.    printf ("            pattern masks from the drive and path specified, including files\n");
  56.    printf ("            marked as READ-ONLY and all files in subordinate directories!\n\n");
  57.    printf ("Drive/Path: %s\n", path );
  58.    printf ("Files:      *.chk\t*.fix\t*.syd\t*.tmp\t*.??_\t*.~??\t~*.*\n\n");
  59.    printf ("Do you wish to proceed?  Type 'yes' or 'no' and press ENTER:\a ");
  60.  
  61.    /* pause until user enters 'yes' or 'no' */
  62.    scanf ("%s", &ans);
  63.    while ((strcmp(strlwr(ans), "yes") != 0) && (strcmp(strlwr(ans), "no") != 0)) {
  64.       scanf ("%s", &ans);
  65.    } /* endwhile */
  66.  
  67.    if (strcmp(strlwr(ans), "yes") == 0) {
  68.       printf("\n\n");
  69.       /* invoke 'ftw()' to apply a function to every file in the directory tree
  70.          of the specified drive beginning at specified directory */
  71.       rc = ftw (path, walker, 10);
  72.       if (rc < 0) {
  73.          /* ftw encountered some kind of error -- display & return non-zero  */
  74.          perror (path);
  75.          return (1);
  76.       }
  77.    } else {
  78.       printf ("Program aborted\n");
  79.       return(1);
  80.    } /* endif */
  81.  
  82.    return (0);
  83. }
  84.  
  85. static int walker (const char *name, const struct stat *st, int flag)
  86. {
  87.    int rc, i;               /* return code from fnmatch(), loop index var */
  88.    char fname[WORKBUFSIZE]; /* 'name' portion of current directory entry */
  89.    char *lastsep;           /* pointers to last path separator & 'dot' char */
  90.  
  91.    /* only process directory entries of type 'file' */
  92.    if (flag == FTW_F) {
  93.  
  94.       /* extract file name and extension from fully-qualified filename */
  95.       /* find final path separator */
  96.       lastsep = strrchr (name, '/');
  97.  
  98.       if (lastsep != NULL) {
  99.          /* found separator -- now copy remainder beyond separator to buffer */
  100.          lastsep++;
  101.          strcpy (fname, lastsep);
  102.       } else {
  103.          /* not found -- entry in root (ftw doesn't alter "\" for root) */
  104.          /* look for "\" separator */
  105.          lastsep = strrchr (name, '\\');
  106.          if (lastsep != NULL) {
  107.             /* found -- now copy remainder beyond separator to buffer */
  108.             lastsep++;
  109.             strcpy (fname, lastsep);
  110.          } else {
  111.             /* still not found -- blank file name */
  112.             strcpy (fname, "\0");
  113.          } /* endif */
  114.       } /* endif */
  115.  
  116.       /* compare current file against each pattern mask in array */
  117.       i = 0;
  118.       while (strcmp(refmasks[i], "\0") != 0) {
  119.          rc = fnmatch (refmasks[i], strlwr(fname), _FNM_OS2);
  120.  
  121.          if (rc == 0) {
  122.             /* current file matches one or more patterns in mask */
  123.  
  124.             /* clear R-O attribute if set so file can be deleted */
  125.             if (st->st_mode != S_IWRITE) {
  126.                #ifndef DEBUG     /* compiling production version */
  127.                chmod (name, S_IWRITE);
  128.                #endif
  129.             } /* endif */
  130.  
  131.             #ifndef DEBUG        /* compiling production version */
  132.             /* delete current file */
  133.             if ((rc = remove (name)) == 0) {
  134.                printf ("Deleted:  %s\n", name);
  135.             } else {
  136.                printf ("Could not delete:\t%s\n", name);
  137.             } /* endif */
  138.             #else                /* compiling debug version */
  139.             printf ("%s\t%s\n", ((st->st_mode & S_IWRITE) ? "r-w" : "r-o"), name);
  140.             #endif
  141.  
  142.             break;
  143.          }
  144.          i++;
  145.  
  146.       } /* endwhile */
  147.  
  148.    }
  149.    return (0);
  150. }
  151.  
  152.  
  153.