3 ms·
memchr is less convenient even than strnlen, because it returns a pointer, so you must subtract 2 pointers to get the size that needs to be used in memcpy. mem
by adrian_b 4y ago
memchr is less convenient even than strnlen, because it returns a pointer, so you must subtract 2 pointers to get the size that needs to be used in memcpy.
memchr is indeed an alternative for someone who would need to use some old libc, without the strnlen function.
The strscpy function is available only inside the Linux kernel, but strnlen should be available in any environment that conforms to POSIX 2008, so strnlen + memcpy is what I would recommend for copying null-terminated strings in any C program.
- saagarjha 4y agoAh, I didn't spot that strnlen had made it into POSIX. Nice!