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_chdirlist.c < prev    next >
C/C++ Source or Header  |  2008-07-13  |  2KB  |  90 lines

  1. /* c_chdirlist.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. #define _CRT_SECURE_NO_WARNINGS 1
  14.  
  15. int
  16. FTPChdirList(FTPCIPtr cip, FTPLineListPtr const cdlist, char *const newCwd, const size_t newCwdSize, int flags)
  17. {
  18.     size_t len;
  19.     char *cdstr;
  20.     FTPLinePtr lp;
  21.     int lastSubDir;
  22.     int mkd, pwd;
  23.     int result;
  24.  
  25.     /* Retain backwards compatibility with earlier library versions. */
  26.     if (flags == kChdirOnly)
  27.         flags = kChdirFullPath;
  28.  
  29.     if ((flags & kChdirFullPath) != 0) {
  30.         len = 0;
  31.         for (lp = cdlist->first; lp != NULL; lp = lp->next) {
  32.             len += strlen(lp->line);
  33.             len++;    /* account for delimiting slash */
  34.         }
  35.         cdstr = malloc(len + 1);
  36.         if (cdstr == NULL)
  37.             return (cip->errNo = kErrMallocFailed);
  38.         cdstr[0] = '\0';
  39.         for (lp = cdlist->first; lp != NULL; lp = lp->next) {
  40.             strcat(cdstr, lp->line);
  41.             if (lp->next != NULL)
  42.                 strcat(cdstr, "/");
  43.         }
  44.         if (FTPChdir3(cip, cdstr, newCwd, newCwdSize, (flags & ~kChdirOneSubdirAtATime)) == kNoErr) {
  45.             free(cdstr);
  46.             return (kNoErr);
  47.         }
  48.         free(cdstr);
  49.     }
  50.  
  51.     if ((flags & kChdirOneSubdirAtATime) != 0) {
  52.         mkd = (flags & kChdirAndMkdir);
  53.         pwd = (flags & kChdirAndGetCWD);
  54.         lastSubDir = 0;
  55.         result = kNoErr;
  56.  
  57.         for (lp = cdlist->first; lp != NULL; lp = lp->next) {
  58.             if (lp->next == NULL)
  59.                 lastSubDir = 1;
  60.  
  61.             if (strcmp(lp->line, ".") == 0) {
  62.                 result = 0;
  63.                 if ((lastSubDir != 0) && (pwd != 0))
  64.                     result = FTPGetCWD(cip, newCwd, newCwdSize);
  65.             } else if ((lastSubDir != 0) && (pwd != 0)) {
  66.                 result = FTPChdirAndGetCWD(cip, (*lp->line != '\0') ? lp->line : "/", newCwd, newCwdSize);
  67.             } else {
  68.                 result = FTPChdir(cip, (*lp->line != '\0') ? lp->line : "/");
  69.             }
  70.             if (result < 0) {
  71.                 if ((mkd != 0) && (*lp->line != '\0')) {
  72.                     if (FTPCmd(cip, "MKD %s", lp->line) == 2) {
  73.                         result = FTPChdir(cip, lp->line);
  74.                     } else {
  75.                         /* couldn't change nor create */
  76.                         cip->errNo = result;
  77.                     }
  78.                 } else {
  79.                     cip->errNo = result;
  80.                 }
  81.             }
  82.             if (result != kNoErr)
  83.                 break;
  84.         }
  85.         return (result);
  86.     }
  87.  
  88.     return (kErrBadParameter);
  89. }    /* FTPChdirList */
  90.