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_chdir3.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  4KB  |  154 lines

  1. /* c_chdir3.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. int
  14. FTPChdir3(FTPCIPtr cip, const char *const cdCwd, char *const newCwd, const size_t newCwdSize, int flags)
  15. {
  16.     char *cp, *startcp;
  17.     int result = kErrBadParameter;
  18.     int lastSubDir;
  19.     int mkd, pwd, one, ful;
  20.     int chdir_err;
  21.     int did_chdir, did_mkdir;
  22.  
  23.     if (cip == NULL)
  24.         return (kErrBadParameter);
  25.     if (strcmp(cip->magic, kLibraryMagic))
  26.         return (kErrBadMagic);
  27.  
  28.     if (cdCwd == NULL) {
  29.         result = kErrInvalidDirParam;
  30.         cip->errNo = kErrInvalidDirParam;
  31.         return result;
  32.     }
  33.  
  34.     /* You can choose try fullpath, one-at-a-time, or both.
  35.      * If none is chosen, assume fullpath.
  36.      */
  37.     if ((flags & (kChdirFullPath|kChdirOneSubdirAtATime)) == 0)
  38.         flags |= kChdirFullPath;
  39.         
  40.     mkd = (flags & kChdirAndMkdir);
  41.     pwd = (flags & kChdirAndGetCWD);
  42.     one = (flags & kChdirOneSubdirAtATime);
  43.     ful = (flags & kChdirFullPath);
  44.  
  45.     chdir_err = kNoErr;
  46.     if (ful != 0) {
  47.         did_chdir = 0;
  48.         did_mkdir = 0;
  49.         if (pwd != 0) {
  50.             result = FTPChdirAndGetCWD(cip, cdCwd, newCwd, newCwdSize);
  51.             if (result == kNoErr) {
  52.                 did_chdir = 1;
  53.             } else {
  54.                 chdir_err = result;
  55.             }
  56.         } else {
  57.             result = FTPChdir(cip, cdCwd);
  58.             if (result == kNoErr) {
  59.                 did_chdir = 1;
  60.             } else {
  61.                 chdir_err = result;
  62.             }
  63.         }
  64.         if ((did_chdir == 0) && (mkd != 0)) {
  65.             result = FTPMkdir(cip, cdCwd, kRecursiveYes);
  66.             if (result == kNoErr) {
  67.                 did_mkdir = 1;
  68.             }
  69.         }
  70.         /* If we just created the directory, chdir to it now. */
  71.         if ((did_mkdir != 0) && (pwd != 0)) {
  72.             result = FTPChdirAndGetCWD(cip, cdCwd, newCwd, newCwdSize);
  73.             if (result == kNoErr) {
  74.                 did_chdir = 1;
  75.             } else {
  76.                 chdir_err = result;
  77.             }
  78.         } else if (did_mkdir != 0) {
  79.             result = FTPChdir(cip, cdCwd);
  80.             if (result == kNoErr) {
  81.                 did_chdir = 1;
  82.             } else {
  83.                 chdir_err = result;
  84.             }
  85.         }
  86.         if (did_chdir != 0) {
  87.             return (kNoErr);
  88.         } else if (one == 0) {
  89.             /* Fullpath didn't work, and one-at-a-time
  90.              * wasn't selected, so quit now.
  91.              */
  92.             return (chdir_err);
  93.         }
  94.     }
  95.  
  96.     if (one != 0) {
  97.         cp = cip->buf;
  98.         cp[cip->bufSize - 1] = '\0';
  99.         (void) Strncpy(cip->buf, cdCwd, cip->bufSize);
  100.         if (cp[cip->bufSize - 1] != '\0')
  101.             return (kErrBadParameter);
  102.  
  103.         if ((cdCwd[0] == '\0') || (strcmp(cdCwd, ".") == 0)) {
  104.             result = 0;
  105.             if (pwd != 0)
  106.                 result = FTPGetCWD(cip, newCwd, newCwdSize);
  107.             return (result);
  108.         }
  109.  
  110.         lastSubDir = 0;
  111.         do {
  112.             startcp = cp;
  113.             cp = StrFindLocalPathDelim(cp);
  114.             if (cp != NULL) {
  115.                 /* If this is the first slash in an absolute
  116.                  * path, then startcp will be empty.  We will
  117.                  * use this below to treat this as the root
  118.                  * directory.
  119.                  */
  120.                 *cp++ = '\0';
  121.             } else {
  122.                 lastSubDir = 1;
  123.             }
  124.             if (strcmp(startcp, ".") == 0) {
  125.                 result = 0;
  126.                 if ((lastSubDir != 0) && (pwd != 0))
  127.                     result = FTPGetCWD(cip, newCwd, newCwdSize);
  128.             } else if ((lastSubDir != 0) && (pwd != 0)) {
  129.                 result = FTPChdirAndGetCWD(cip, (*startcp != '\0') ? startcp : "/", newCwd, newCwdSize);
  130.             } else {
  131.                 result = FTPChdir(cip, (*startcp != '\0') ? startcp : "/");
  132.             }
  133.             if (result < 0) {
  134.                 if ((mkd != 0) && (*startcp != '\0')) {
  135.                     if (FTPCmd(cip, "MKD %s", startcp) == 2) {
  136.                         if (pwd != 0) {
  137.                             result = FTPChdirAndGetCWD(cip, startcp, newCwd, newCwdSize);
  138.                         } else {
  139.                             result = FTPChdir(cip, startcp);
  140.                         }
  141.                     } else {
  142.                         /* couldn't change nor create */
  143.                         cip->errNo = result;
  144.                     }
  145.                 } else {
  146.                     cip->errNo = result;
  147.                 }
  148.             }
  149.         } while ((!lastSubDir) && (result == 0));
  150.     }
  151.  
  152.     return (result);
  153. }    /* FTPChdir3 */
  154.