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 / Strnpcat.c < prev    next >
C/C++ Source or Header  |  2001-11-19  |  2KB  |  56 lines

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