2 ms·
The string.h `n' versions aren't for creating C-style strings, but rather for manipulating 0-padded fixed-size buffers. stpncpy is no exception: the destination
by tom_ 8y ago
The string.h `n' versions aren't for creating C-style strings, but rather for manipulating 0-padded fixed-size buffers. stpncpy is no exception: the destination is padded if the source is smaller (of course, unnecessary for C-style strings), and the destination is not 0-terminated if the source is too large (not a great idea if you're trying to create a C-style string).
Quite why the stdio.h `n' versions do something different, I couldn't say. They should probably have chosen a different letter, as this stuff has been enough of a pitfall for long enough without adding yet another one into the mix. (One option: following the example of BSD-style `strlcpy', as popularized by Ulrich Drepper, `l'. Too late now, of course.)