home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / util / recrm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  1.9 KB  |  90 lines

  1. /* recrm: recursive remove */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/util/RCS/recrm.c,v 6.0 1991/12/18 20:25:18 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/util/RCS/recrm.c,v 6.0 1991/12/18 20:25:18 jpo Rel $
  9.  *
  10.  * $Log: recrm.c,v $
  11.  * Revision 6.0  1991/12/18  20:25:18  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include "util.h"
  17. #include <isode/usr.dirent.h>
  18. #include <sys/stat.h>
  19.  
  20. static char currentdir[BUFSIZ];
  21.  
  22. int rmFiles(entry)
  23. struct dirent *entry;
  24. {
  25.     struct stat statbuf;
  26.     char cbuf[BUFSIZ];
  27.  
  28.     if (*entry->d_name == '.' &&
  29.         (strcmp(entry->d_name,".") == 0)
  30.         || (strcmp(entry->d_name,"..") == 0))
  31.         return 0;
  32.     (void) strcpy (cbuf, currentdir);
  33.     (void) strcat (cbuf, "/");
  34.     (void) strcat (cbuf, entry -> d_name);
  35.  
  36.     if (stat(cbuf, &statbuf) != OK) {
  37.         PP_SLOG (LLOG_EXCEPTIONS, cbuf, ("Can't stat file"));
  38.         return 0;
  39.     }
  40.     if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
  41.         return 1;
  42.     else {
  43.         if (unlink(cbuf) == NOTOK) /* ignore rmdir will catch it */
  44.             PP_SLOG(LLOG_EXCEPTIONS, cbuf,
  45.                 ("can't remove file"));
  46.         else
  47.             PP_TRACE(("Removed '%s'", cbuf));
  48.         return 0;
  49.     }
  50. }
  51.  
  52. /* recursive rmdir */
  53. recrm (dir)
  54. char    *dir;
  55. {
  56.     struct dirent **namelist, **ix;
  57.     int             noOfSubdirs, i;
  58.     char        buf[BUFSIZ];
  59.     int        retval = OK;
  60.     char         *cdp;
  61.  
  62.     (void) strcpy (buf, dir); /* for our use */
  63.     (void) strcpy (currentdir, dir); /* for rmFiles use */
  64.     cdp = buf + strlen(buf);
  65.  
  66.     noOfSubdirs = _scandir(buf, &namelist, rmFiles, NULLIFP);
  67.  
  68.     for (i = 0, ix = namelist; i++ < noOfSubdirs && *ix; ix++) {
  69.         *cdp ++ = '/';
  70.         (void) strcpy (cdp, (*ix) -> d_name);
  71.         if (recrm (buf) == NOTOK) {
  72.             retval = NOTOK;
  73.             break;
  74.         }
  75.         
  76.         if (rmdir (buf) == NOTOK) {
  77.             PP_SLOG (LLOG_EXCEPTIONS, buf,
  78.                  ("Can't remove directory"));
  79.             retval = NOTOK;
  80.             break;
  81.         }
  82.         PP_TRACE(("Removed directory '%s'", buf));
  83.         *-- cdp = 0;
  84.     }
  85.  
  86.     if (namelist) free((char *) namelist);
  87.     return retval;
  88. }
  89.  
  90.