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 / Strn / DStrCpy.c < prev    next >
C/C++ Source or Header  |  2001-11-19  |  835b  |  44 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. const char *
  7. DStrCpy(DStr *const dst, const char *const src)
  8. {
  9.     size_t srcLen, allocSize;
  10.     char *cp;
  11.  
  12.     if (IS_DSTR_CORRUPT(dst))
  13.         return NULL;
  14.  
  15.     if (dst->s == src)
  16.         return (dst->s);
  17.  
  18.     srcLen = strlen(src) + 1 /* copy NUL byte also */;
  19.     if (srcLen > 0x00FFFFFF)
  20.         return NULL;
  21.     if (dst->allocSize < srcLen) {
  22.         /* Need to resize buffer before copying. */
  23.         allocSize = (srcLen + 16) & 0xFFFFFFF0;
  24.         if (dst->s == NULL) {
  25.             cp = calloc(allocSize, (size_t) 1);
  26.             if (cp == NULL)
  27.                 return NULL;
  28.         } else {
  29.             cp = realloc(dst->s, allocSize);
  30.             if (cp == NULL)
  31.                 return NULL;
  32.             memset(cp, 0, allocSize);
  33.         }
  34.         dst->s = cp;
  35.         dst->allocSize = allocSize;
  36.     } else {
  37.         cp = dst->s;
  38.     }
  39.  
  40.     memcpy(cp, src, srcLen);
  41.     dst->len = srcLen - 1;
  42.     return (cp);
  43. }    /* DStrCpy */
  44.