strncat is insufficient

Dik T. Winter dik at cwi.nl
Sun Aug 26 11:21:21 AEST 1990


In article <620.26d6d0cd at iccgcc.decnet.ab.com> browns at iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) writes:
 > In article <587 at array.UUCP>, colin at array.UUCP (Colin Plumb) writes:
 > > char *strlimcat(char *dst, char const *src, int maxlen)
....
 > > 		strncpy(dst+len, src, maxlen-len);
....
I would say that strncat is better here.  Consider when maxlen-len is very large
while strlen(src) is very small.  strncpy will do all that zero-filling!
 > 
 > Caution:  This will not necessarily put a '\0' at the end of the string.
True.
 > 
 > Can Karl, or someone else who knows, explain why strncpy was standardized
 > to copy n characters even at the expense of a zero byte; or why no
 > alternative that always terminates the string was provided.
I would not gripe against strncpy, because it truncates or zero fills,
whatever is needed.  In my opinion the gripe is against strncat.
--
dik t. winter, cwi, amsterdam, nederland
dik at cwi.nl



More information about the Comp.std.c mailing list