/********************************************************************************/
/* strcpy_safe - Copy src string to dst string, upto maxlen characters. */
/* Safer than strncpy, because it does not fill destination string, */
/* but only copies up to the length needed. Src string should be */
/* null-terminated, and must-be if its allocated length is shorter than maxlen. */
/* Up to maxlen-1 characters are copied to dst string. The dst string is always */
/* null-terminated. The dst string should be pre-allocated to at least maxlen */
/* bytes. However, this function will work safely for dst arrays that are less */
/* than maxlen, as long as the null-terminated src string is known to be */
/* shorter than the allocated length of dst, just like regular strcpy. */
/********************************************************************************/
void strcpy_safe( char *dst, const char *src, int maxlen )
{
int j=0, oneless;
oneless = maxlen - 1;
while ((j &lgt; oneless) && (src[j] != '\0'))
{
dst[j] = src[j];
j++;
}
dst[j] = '\0';
}
Safe String Copy
Treat string handling with care.
Review the string-copy helper and its usage requirements. Keep function names and examples unchanged when applying it in model code.
This page is an editorial rewrite of source documentation. Historical status, availability, and platform notes are not independently verified for a current release. Technical examples are reference material, not a live simulator.