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 / u_getcwd.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  2KB  |  93 lines

  1. /* u_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. /* Use getcwd/getwd to get the full path of the current local
  14.  * working directory.
  15.  */
  16. char *
  17. FTPGetLocalCWD(char *buf, size_t size)
  18. {
  19. #ifdef HAVE_GETCWD
  20.     memset(buf, 0, size);
  21.     if ((getcwd(buf, size - 1) == NULL) || (buf[size - 1] != '\0') || (buf[size - 2] != '\0')) {
  22.         memset(buf, 0, size);
  23.         return NULL;    
  24.     }
  25.     return (buf);
  26. #elif defined(HAVE_GETWD) && !defined(_REENTRANT)
  27.     static char *cwdBuf = NULL;
  28.     char *dp;
  29.     
  30.     /* Due to the way getwd is usually implemented, it's
  31.      * important to have a buffer large enough to hold the
  32.      * whole thing.  getwd usually starts at the end of the
  33.      * buffer, and works backwards, returning you a pointer
  34.      * to the beginning of it when it finishes.
  35.      */
  36.     if (size < MAXPATHLEN) {
  37.         /* Buffer not big enough, so use a temporary one,
  38.          * and then copy the first 'size' bytes of the
  39.          * temporary buffer to your 'buf.'
  40.          */
  41.         if (cwdBuf == NULL) {
  42.             cwdBuf = (char *) malloc((size_t) MAXPATHLEN + 16);
  43.             if (cwdBuf == NULL) {
  44.                 return NULL;
  45.             }
  46.         }
  47.         dp = cwdBuf;
  48.     } else {
  49.         /* Buffer is big enough already. */
  50.         dp = buf;
  51.     }
  52.     *dp = '\0';
  53.     if (getwd(dp) == NULL) {
  54.         /* getwd() should write the reason why in the buffer then,
  55.          * according to the man pages.
  56.          */
  57.         (void) Strncpy(buf, ".", size);
  58.         return (NULL);
  59.     }
  60.     return (Strncpy(buf, dp, size));
  61. #elif defined(HAVE_GETWD) && defined(_REENTRANT)
  62.     char wdbuf[MAXPATHLEN + 16];
  63.  
  64.     if (size <= MAXPATHLEN) {
  65.         memset(wdbuf, 0, sizeof(wdbuf));
  66.         if (getwd(wdbuf) == NULL) {
  67.             (void) Strncpy(buf, ".", size);
  68.             return (NULL);
  69.         }
  70.         (void) Strncpy(buf, wdbuf, size);
  71.     } else {
  72.         memset(buf, 0, size);
  73.         if (getwd(buf) == NULL) {
  74.             (void) Strncpy(buf, ".", size);
  75.             return (NULL);
  76.         }
  77.     }
  78.     return (buf);
  79. #elif (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  80.     memset(buf, 0, size);
  81.     if ((GetCurrentDirectory((DWORD) size - 1, buf) < 1) || (buf[size - 1] != '\0') || (buf[size - 2] != '\0')) {
  82.         memset(buf, 0, size);
  83.         return NULL;    
  84.     }
  85.     return buf;
  86. #else
  87.     /* Not a solution, but does anybody not have either of
  88.      * getcwd or getwd?
  89.      */
  90.     --Error--;
  91. #endif
  92. }   /* FTPGetLocalCWD */
  93.