Article View: comp.std.c
Article #1619Re: strncat is insufficient
From: Stan Brown, Oak Road Systems
Date: Sun, 26 Aug 1990 00:26
Date: Sun, 26 Aug 1990 00:26
23 lines
1407 bytes
1407 bytes
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!
Technical Details
Message-ID:
<620.26d6d0cd@iccgcc.decnet.ab.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!usenet.ins.cwru.edu!abvax!iccgcc!brownsReferences:
<17418@haddock.ima.isc.com> <13598@smoke.BRL.MIL> <1990Aug23.132400.3654@zorch.SF-Bay.ORG> <587@array.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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