home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / older_versions / libncftp-3.1.5-src.tar.gz / libncftp-3.1.5-src.tar / libncftp-3.1.5 / Strn / Strncpy.c < prev    next >
C/C++ Source or Header  |  2002-02-01  |  1KB  |  46 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. const char gStrnLibVersion[] = "@(#) Strn 3.1.1";
  7.  
  8. /*
  9.  * Copy src to dst, truncating or null-padding to always copy n-1 bytes.
  10.  * Return dst.
  11.  */
  12. char *
  13. Strncpy(char *const dst, const char *const src, const size_t n)
  14. {
  15.     register char *d;
  16.     register const char *s;
  17.     register size_t i;
  18.  
  19.     d = dst;
  20.     *d = 0;
  21.     if (n != 0) {
  22.         s = src;
  23.         /* If they specified a maximum of n characters, use n - 1 chars to
  24.          * hold the copy, and the last character in the array as a NUL.
  25.          * This is the difference between the regular strncpy routine.
  26.          * strncpy doesn't guarantee that your new string will have a
  27.          * NUL terminator, but this routine does.
  28.          */
  29.         for (i=1; i<n; i++) {
  30.             if ((*d++ = *s++) == 0) {
  31. #if (STRN_ZERO_PAD == 1)
  32.                 /* Pad with zeros. */
  33.                 for (; i<n; i++)
  34.                     *d++ = 0;
  35. #endif    /* STRN_ZERO_PAD */
  36.                 return dst;
  37.             }
  38.         }
  39.         /* If we get here, then we have a full string, with n - 1 characters,
  40.          * so now we NUL terminate it and go home.
  41.          */
  42.         *d = 0;
  43.     }
  44.     return (dst);
  45. }    /* Strncpy */
  46.