Good point. Here's a revised version (also incorporating other suggestions
made)
size_t sstrlen(const char *s, size_t n) {
size_t i;
for(i = 0; (*(s+i) != '\0' && i < n); i++);
return i;
}
char *sstrncpy(char *dst, size_t n1, const char *src, size_t n2) {
if (sstrlen(src, n2) > (n1 - 1)) {
errno = ENOSPC;
dst[0] = NULL;
return NULL;
}
strncpy(dst, src, n2);
return dst;
}
Something similar could be done with strncat as well. Note that I
don't return the number of bytes written because I wanted to remain
consistent with the existing strncpy.
Chris