Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libc/string/strncpy.c
Show All 38 Lines | |||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <string.h> | #include <string.h> | ||||
/* | /* | ||||
* Copy src to dst, truncating or null-padding to always copy n bytes. | * Copy src to dst, truncating or null-padding to always copy n bytes. | ||||
* Return dst. | * Return dst. | ||||
*/ | */ | ||||
#ifdef WEAK_STRNCPY | |||||
__weak_reference(__strncpy, strncpy); | |||||
#endif | |||||
char * | char * | ||||
strncpy(char * __restrict dst, const char * __restrict src, size_t n) | #ifdef WEAK_STRNCPY | ||||
__strncpy | |||||
#else | |||||
strncpy | |||||
#endif | |||||
(char * __restrict dst, const char * __restrict src, size_t n) | |||||
{ | { | ||||
if (n != 0) { | if (n != 0) { | ||||
char *d = dst; | char *d = dst; | ||||
const char *s = src; | const char *s = src; | ||||
do { | do { | ||||
if ((*d++ = *s++) == '\0') { | if ((*d++ = *s++) == '\0') { | ||||
/* NUL pad the remaining n-1 bytes */ | /* NUL pad the remaining n-1 bytes */ | ||||
while (--n != 0) | while (--n != 0) | ||||
*d++ = '\0'; | *d++ = '\0'; | ||||
break; | break; | ||||
} | } | ||||
} while (--n != 0); | } while (--n != 0); | ||||
} | } | ||||
return (dst); | return (dst); | ||||
} | } |