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_getcwd.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  3KB  |  131 lines

  1. /* c_getcwd.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. FTPGetCWD(const FTPCIPtr cip, char *const newCwd, const size_t newCwdSize)
  15. {
  16.     ResponsePtr rp;
  17.     char *l, *r;
  18.     int result;
  19.  
  20.     if (cip == NULL)
  21.         return (kErrBadParameter);
  22.     if (strcmp(cip->magic, kLibraryMagic))
  23.         return (kErrBadMagic);
  24.  
  25.     if ((newCwd == NULL) || (newCwdSize == 0)) {
  26.         result = kErrInvalidDirParam;
  27.         cip->errNo = kErrInvalidDirParam;
  28.     } else {
  29.         rp = InitResponse();
  30.         if (rp == NULL) {
  31.             result = kErrMallocFailed;
  32.             cip->errNo = kErrMallocFailed;
  33.             FTPLogError(cip, kDontPerror, "Malloc failed.\n");
  34.         } else {
  35.             result = RCmd(cip, rp, "PWD");
  36.             if (result == 2) {
  37.                 if ((r = strrchr(rp->msg.first->line, '"')) != NULL) {
  38.                     /* "xxxx" is current directory.
  39.                      * Strip out just the xxxx to copy into the remote cwd.
  40.                      */
  41.                     l = strchr(rp->msg.first->line, '"');
  42.                     if ((l != NULL) && (l != r)) {
  43.                         *r = '\0';
  44.                         ++l;
  45.                         (void) Strncpy(newCwd, l, newCwdSize);
  46.                         *r = '"';    /* Restore, so response prints correctly. */
  47.                     }
  48.                 } else {
  49.                     /* xxxx is current directory.
  50.                      * Mostly for VMS.
  51.                      */
  52.                     if ((r = strchr(rp->msg.first->line, ' ')) != NULL) {
  53.                         *r = '\0';
  54.                         (void) Strncpy(newCwd, (rp->msg.first->line), newCwdSize);
  55.                         *r = ' ';    /* Restore, so response prints correctly. */
  56.                     }
  57.                 }
  58.                 result = kNoErr;
  59.             } else if (result > 0) {
  60.                 result = kErrPWDFailed;
  61.                 cip->errNo = kErrPWDFailed;
  62.             }
  63.             DoneWithResponse(cip, rp);
  64.         }
  65.     }
  66.     return (result);
  67. }    /* FTPGetCWD */
  68.  
  69.  
  70.  
  71.  
  72. int
  73. FTPChdirAndGetCWD(const FTPCIPtr cip, const char *const cdCwd, char *const newCwd, const size_t newCwdSize)
  74. {
  75.     ResponsePtr rp;
  76.     char *l, *r;
  77.     int result;
  78.  
  79.     if (cip == NULL)
  80.         return (kErrBadParameter);
  81.     if (strcmp(cip->magic, kLibraryMagic))
  82.         return (kErrBadMagic);
  83.  
  84.     if ((newCwd == NULL) || (cdCwd == NULL)) {
  85.         result = kErrInvalidDirParam;
  86.         cip->errNo = kErrInvalidDirParam;
  87.     } else {
  88.         if (cdCwd[0] == '\0') {    /* But allow FTPChdir(cip, ".") to go through. */
  89.             result = FTPGetCWD(cip, newCwd, newCwdSize);
  90.             return (result);
  91.         }
  92.         rp = InitResponse();
  93.         if (rp == NULL) {
  94.             result = kErrMallocFailed;
  95.             cip->errNo = kErrMallocFailed;
  96.             FTPLogError(cip, kDontPerror, "Malloc failed.\n");
  97.         } else {
  98.             if (strcmp(cdCwd, "..") == 0)
  99.                 result = RCmd(cip, rp, "CDUP");     
  100.             else
  101.                 result = RCmd(cip, rp, "CWD %s", cdCwd);
  102.             if (result == 2) {
  103.                 l = strchr(rp->msg.first->line, '"');
  104.                 if ((l == rp->msg.first->line) && ((r = strrchr(rp->msg.first->line, '"')) != NULL) && (l != r)) {
  105.                     /* "xxxx" is current directory.
  106.                      * Strip out just the xxxx to copy into the remote cwd.
  107.                      *
  108.                      * This is nice because we didn't have to do a PWD.
  109.                      */
  110.                     *r = '\0';
  111.                     ++l;
  112.                     (void) Strncpy(newCwd, l, newCwdSize);
  113.                     *r = '"';    /* Restore, so response prints correctly. */
  114.                     DoneWithResponse(cip, rp);
  115.                     result = kNoErr;
  116.                 } else {
  117.                     DoneWithResponse(cip, rp);
  118.                     result = FTPGetCWD(cip, newCwd, newCwdSize);
  119.                 }
  120.             } else if (result > 0) {
  121.                 result = kErrCWDFailed;
  122.                 cip->errNo = kErrCWDFailed;
  123.                 DoneWithResponse(cip, rp);
  124.             } else {
  125.                 DoneWithResponse(cip, rp);
  126.             }
  127.         }
  128.     }
  129.     return (result);
  130. }    /* FTPChdirAndGetCWD */
  131.