While `sstrncpy` guarantees a null terminated string, some compilers don't get
the memo and complain about the buffer size being equal to the size provided to
*strncpy(3)*. This *is* a potential source of error with *strncpy(3)*, because
if the source string is longer than the buffer, the buffer is not null
terminated. That is the precise reason `sstrncpy` exists in the first place.
Make these compilers happy by decreasing the size passed to *strncpy(3)* by
one.
#endif
char *sstrncpy(char *dest, const char *src, size_t n) {
- strncpy(dest, src, n);
- dest[n - 1] = '\0';
+ if (n > 0) {
+ strncpy(dest, src, n - 1);
+ dest[n - 1] = 0;
+ }
return dest;
} /* char *sstrncpy */