Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!usenet.ins.cwru.edu!abvax!iccgcc!browns From: browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) Newsgroups: comp.std.c Subject: Re: strncat is insufficient Message-ID: <620.26d6d0cd@iccgcc.decnet.ab.com> Date: 26 Aug 90 00:26:04 GMT References: <17418@haddock.ima.isc.com> <13598@smoke.BRL.MIL> <1990Aug23.132400.3654@zorch.SF-Bay.ORG> <587@array.UUCP> Lines: 23 In article <587@array.UUCP>, colin@array.UUCP (Colin Plumb) writes: > char *strlimcat(char *dst, char const *src, int maxlen) > { > int len; > > len = strlen(dst); > if (len < maxlen) > strncpy(dst+len, src, maxlen-len); > return dst; > } Caution: This will not necessarily put a '\0' at the end of the string. 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. This implementation is a fertile source of bugs that seem to bite every C programmer at least once (and not always early in a career!). Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A. (216) 371-0043 The opinions expressed are mine. Mine alone! Nobody else is responsible for them or even endorses them--except my cat Dexter, and he signed the power of attorney only under my threat to cut off his Cat Chow!