🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.atari.st
Article #39561

Re: more on strncpy (was re: laser c question)

#39561
From: Dale Schumacher
Date: Fri, 15 Dec 1989 18:26
44 lines
2570 bytes
[depeche@quiche.cs.mcgill.ca (Sam Alan EZUST) writes...]
> 
> [In message "Re: laser C question" on Dec 13, John Stanley writes:]
[...lots deleted...]
> :   .. although, it would be a real-good-idea if you posted a note on the
> : net mentioning that strncpy doesn't move <n> bytes so other beginning
> : programmers who see your message won't think that it's a correct
> : substitution....
> 
> ok.. I shall. Thanks for your help/reply!!
> 
> -- 
> S. Alan Ezust                                   depeche@calvin.cs.mcgill.ca
> McGill University Department of Computer Science - Montreal, Quebec, Canada

First, a note about strncpy().  The ANSI standard (yeah, I know, "proposed")
describes strncpy() like this:

	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'.[121]
	If copying takes place between objects that overlap, the behaviour
	is undefined.
	
	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.

[121:	Thus, if there is no null character in the first 'n' characters of
	the array pointed to by 's2', the result will not be null-terminated]

Secondly, the dLibs version is (slightly) broken according that the above
description.  It only copies up to the first null character, or 'n'
characters total, and does NOT pad the copy will null characters up to 'n'.
This almost never causes a problem, since the destination is null-terminated
in that case, but I thought I should point it out.  It's also easy to fix,
but I'll leave that as an exercise for the reader :-)

\\   /  Dale Schumacher                         399 Beacon Ave.
 \\ /   (alias: Dalnefre')                      St. Paul, MN  55104-3527
  ><    ..!nic.mr.net!bungia.mn.org!syntel!dal  United States of America
 / \\   "The power of accurate observation is commonly called cynicism
/   \\   by those who have not got it." -George Bernard Shaw
Technical Details
Message-ID: <1989Dec15.182652.3126@stag.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!uakari.primate.wisc.edu!uwm.edu!uwvax!umn-d-ub!umn-cs!nis!pwcs!stag!daemon

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!uakari.primate.wisc.edu!uwm.edu!uwvax!umn-d-ub!umn-cs!nis!pwcs!stag!daemon
From: dal@syntel.mn.org (Dale Schumacher)
Newsgroups: comp.sys.atari.st
Subject: Re: more on strncpy (was re: laser c question)
Message-ID: <1989Dec15.182652.3126@stag.UUCP>
Date: 15 Dec 89 18:26:52 GMT
Sender: daemon@stag.UUCP (The devil himself)
Organization: Mindtools ST Access Group
Lines: 44