strcpy, strncpy, strcat, strncat

Daniel R. Levy levy at ttrdc.UUCP
Mon Sep 1 09:28:33 AEST 1986


In article <6632 at sun.uucp>, guy at 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 at sun.com (or guy at 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



More information about the Comp.lang.c mailing list