home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / c_rmdirr.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  2KB  |  114 lines

  1. /* c_rmdirr.c
  2.  *
  3.  * Copyright (c) 1996-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  *
  6.  */
  7.  
  8. #include "syshdrs.h"
  9. #ifdef PRAGMA_HDRSTOP
  10. #    pragma hdrstop
  11. #endif
  12.  
  13. static int
  14. FTPRmdirRecursiveL2(const FTPCIPtr cip)
  15. {
  16.     FTPLineList fileList;
  17.     FTPLinePtr filePtr;
  18.     char *file;
  19.     int result;
  20.  
  21.     result = FTPRemoteGlob(cip, &fileList, "**", kGlobYes);
  22.     if (result != kNoErr) {
  23.         return (result);
  24.     }
  25.  
  26.     for (filePtr = fileList.first;
  27.         filePtr != NULL;
  28.         filePtr = filePtr->next)
  29.     {
  30.         file = filePtr->line;
  31.         if (file == NULL) {
  32.             cip->errNo = kErrBadLineList;
  33.             break;
  34.         }
  35.  
  36.         if ((file[0] == '.') && ((file[1] == '\0') || ((file[1] == '.') && (file[2] == '\0'))))
  37.             continue;    /* Skip . and .. */
  38.  
  39.         if (FTPChdir(cip, file) == kNoErr) {
  40.             /* It was a directory.
  41.              * Go in and wax it.
  42.              */
  43.             result = FTPRmdirRecursiveL2(cip);
  44.  
  45.             if (FTPChdir(cip, "..") != kNoErr) {
  46.                 /* Panic -- we can no longer
  47.                  * cd back to the directory
  48.                  * we were in before.
  49.                  */
  50.                 result = kErrCannotGoToPrevDir;
  51.                 cip->errNo = kErrCannotGoToPrevDir;
  52.                 return (result);
  53.             }
  54.  
  55.             if ((result < 0) && (result != kErrGlobNoMatch))
  56.                 return (result);
  57.  
  58.             result = FTPRmdir(cip, file, kRecursiveNo, kGlobNo);
  59.             if (result != kNoErr) {
  60.                 /* Well, we couldn't remove the empty
  61.                  * directory.  Perhaps we screwed up
  62.                  * and the directory wasn't empty.
  63.                  */
  64.                 return (result);
  65.             }
  66.         } else {
  67.             /* Assume it was a file -- remove it. */
  68.             result = FTPDelete(cip, file, kRecursiveNo, kGlobNo);
  69.             /* Try continuing to remove the rest,
  70.              * even if this failed.
  71.              */
  72.         }
  73.     }
  74.     DisposeLineListContents(&fileList);
  75.  
  76.     return (result);
  77. }     /* FTPRmdirRecursiveL2 */
  78.  
  79.  
  80.  
  81. int
  82. FTPRmdirRecursive(const FTPCIPtr cip, const char *const dir)
  83. {
  84.     int result, result2;
  85.  
  86.     /* Preserve old working directory. */
  87.     (void) FTPGetCWD(cip, cip->buf, cip->bufSize);
  88.  
  89.     result = FTPChdir(cip, dir);
  90.     if (result != kNoErr) {
  91.         return (result);
  92.     }
  93.  
  94.     result = FTPRmdirRecursiveL2(cip);
  95.  
  96.     if (FTPChdir(cip, cip->buf) != kNoErr) {
  97.         /* Could not cd back to the original user directory -- bad. */
  98.         if (result != kNoErr) {
  99.             result = kErrCannotGoToPrevDir;
  100.             cip->errNo = kErrCannotGoToPrevDir;
  101.         }
  102.         return (result);
  103.     }
  104.  
  105.     /* Now rmdir the last node, the root of the tree
  106.      * we just went through.
  107.      */
  108.     result2 = FTPRmdir(cip, dir, kRecursiveNo, kGlobNo);
  109.     if ((result2 != kNoErr) && (result == kNoErr))
  110.         result = result2;
  111.  
  112.     return (result);
  113. }    /* FTPRmdirRecursive */
  114.