🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3043

Re: definition of strNcpy

#3043
From: David Spencer
Date: Fri, 18 Dec 1992 13:47
33 lines
1546 bytes
lipson@dms.agames.com (Pete Lipson) writes:

>  if you say:   strncpy(dst,"abcdef",4)

> does the standard really say you get five characters appended to the
>dst (the chars 'a' 'b' 'c' 'd' AND the '\0') ???
>  the man-page says it moves AT MOST the count (4 in this case) characters
>but it ALSO says it returns a pointer to a NULL-TERMINATED result.

Only if it hits a null before it copies n chars:

7.11.2.4:

"The strncpy function copies not more than n characters (characters
that follow a null character are not copied) from the array pointed to
by s2 to the array pointed to by s1. [134]"

"[134] Thus, if there is no null character in the first n characters
that can end up in the array pointed to by s2, the result will not be
null-terminated."

I suspect you are getting 4 chars from s2 and the '\0' is already in
s1.

BTW, the standard also says:

"If the array pointed to by s2 is a string that is shorter than n
characters, null characters are appended to the copy in the array
pointed to by s1, until n characters in all have been written"

I wonder how many implementations do this?

dhs   spencer@panix.com
Technical Details
Message-ID: <1992Dec18.134739.484@panix.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!news.ans.net!cmcl2!panix!spencer
References: <1992Dec18.015228.6094@dms.agames.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!news.ans.net!cmcl2!panix!spencer
From: spencer@panix.com (David Spencer)
Subject: Re: definition of strNcpy
Message-ID: <1992Dec18.134739.484@panix.com>
Date: Fri, 18 Dec 1992 13:47:39 GMT
References: <1992Dec18.015228.6094@dms.agames.com>
Organization: PANIX Public Access Unix, NYC
Lines: 33