But beware that strncpy by itself doesn't guarantee a \0 at the end, thus potentially causing other problems.
Code:
#include <stdio.h>
#include <string.h>
void safer_strncpy ( char *dst, const char *src, size_t len ) {
strncpy( dst, src, len );
if ( dst[len-1] != '\0' ) {
fprintf(stderr,"%s truncated at %lu characters\n", src, (unsigned long)len-1 );
}
dst[len-1] = '\0';
}
int main ( ) {
char dst[5];
safer_strncpy( dst, "this", sizeof(dst) );
printf("%s\n",dst);
safer_strncpy( dst, "world", sizeof(dst) );
printf("%s\n",dst);
return 0;
}