Article View: net.lang.c
Article #9219Re: strcpy, strncpy, strcat, strncat
From: Daniel R. Levy
Date: Sun, 31 Aug 1986 23:28
Date: Sun, 31 Aug 1986 23:28
44 lines
2728 bytes
2728 bytes
In article <6632@sun.uucp>, guy@sun.UUCP writes:
>> It seems odd that "strncat" always adds a trailing '\0'
>> but "strncpy" does not.
>
>It may be considered odd, but that's the way UNIX works.
^^^^
THE UNIX(trademark of AT&T) SYSTEM
>(If you want a version of "strncpy" that copies a
>null-or-end-of-buffer-terminated string to a null-terminated string, clear
>out the null-terminated string and "strcat" the other string to it.)
1) Surely, "strncat()" was meant here. "strcat()" expects a null-terminated
string as input, whereas "strncat()" accepts a maximum count.
2) Why "clear out" the null-terminated string (better, "buffer") to be
copied to? Just null its first character (at least under UNIX(tm)
System V this works with strncat(), dunno about Berzerkeley).
3) Better yet, if one is obsessed over performance (avoiding the check for the
length of the string in the destination buffer), write one's own routine
for this and include it in one's source code. The routine would likely be
under 10 lines long. Or make it a macro. The UNIX(tm) system doesn't
promise to provide the kitchen sink ready-made, rather, it furnishes
the tools to construct it with. Use them.
4) I kind of doubt that Guy will ever read these comments, at least not
unless someone else quotes them (I think my name/site is in his kill file).
But comments from others as to whether I have truly caught some things
that Guy has omitted/missed/forgotten would be welcome.
> Guy Harris
> {ihnp4, decvax, seismo, decwrl, ...}!sun!guy
> guy@sun.com (or guy@sun.arpa)
Dan at the "Deathstar"
--
------------------------------- Disclaimer: The views contained herein are
| dan levy | yvel nad | my own and are not at all those of my em-
| an engihacker @ | ployer or the administrator of any computer
| at&t computer systems division | upon which I may hack.
| skokie, illinois |
-------------------------------- Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
go for it! allegra,ulysses,vax135}!ttrdc!levy
Technical Details
Message-ID:
<1162@ttrdc.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!caip!clyde!cbatt!cbosgd!ihnp4!cuae2!ltuxa!ttrdc!levyReferences:
<2807@watmath.UUCP> <6632@sun.uucp>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!caip!clyde!cbatt!cbosgd!ihnp4!cuae2!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: net.lang.c Subject: Re: strcpy, strncpy, strcat, strncat Message-ID: <1162@ttrdc.UUCP> Date: Sun, 31-Aug-86 19:28:33 EDT Article-I.D.: ttrdc.1162 Posted: Sun Aug 31 19:28:33 1986 Date-Received: Mon, 1-Sep-86 23:04:43 EDT References: <2807@watmath.UUCP> <6632@sun.uucp> Distribution: net Organization: AT&T, Computer Systems Division, Skokie, IL Lines: 44