home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / older_versions / libncftp-3.2.2-src.tar.bz2 / libncftp-3.2.2-src.tar / libncftp-3.2.2 / Strn / Strncat.c < prev    next >
C/C++ Source or Header  |  2001-11-19  |  1KB  |  47 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6.  
  7. /*
  8.  * Concatenate src on the end of dst.  The resulting string will have at most
  9.  * n-1 characters, not counting the NUL terminator which is always appended
  10.  * unlike strncat.  The other big difference is that strncpy uses n as the
  11.  * max number of characters _appended_, while this routine uses n to limit
  12.  * the overall length of dst.
  13.  */
  14. char *
  15. Strncat(char *const dst, const char *const src, const size_t n)
  16. {
  17.     register size_t i;
  18.     register char *d;
  19.     register const char *s;
  20.  
  21.     if (n != 0 && ((i = strlen(dst)) < (n - 1))) {
  22.         d = dst + i;
  23.         s = src;
  24.         /* If they specified a maximum of n characters, use n - 1 chars to
  25.          * hold the copy, and the last character in the array as a NUL.
  26.          * This is the difference between the regular strncpy routine.
  27.          * strncpy doesn't guarantee that your new string will have a
  28.          * NUL terminator, but this routine does.
  29.          */
  30.         for (++i; i<n; i++) {
  31.             if ((*d++ = *s++) == 0) {
  32. #if (STRN_ZERO_PAD == 1)
  33.                 /* Pad with zeros. */
  34.                 for (; i<n; i++)
  35.                     *d++ = 0;
  36. #endif    /* STRN_ZERO_PAD */
  37.                 return dst;
  38.             }
  39.         }
  40.         /* If we get here, then we have a full string, with n - 1 characters,
  41.          * so now we NUL terminate it and go home.
  42.          */
  43.         *d = 0;
  44.     }
  45.     return (dst);
  46. }    /* Strncat */
  47.