🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3731

Re: gets() and getchar

#3731
From: Mark Brader
Date: Tue, 15 Aug 1995 17:12
23 lines
1426 bytes
rich@kastle.com writes:
> *Not* *once* have I found the library implementations of these two
> functions adequate.  Every time I've used them I've had to augment,
> with delimiter-placement code after strncpy, and extra length
> calculations with strncat.

Note that if your intention is merely to copy up to some number of
characters and place a null character after them, then calling strncpy()
and placing a null character does more work than you need.  Since it
zero-fills, it's like using calloc() when malloc() would do.

The way to avoid this is to place a null character at the *start* of
the destination, then use strncat().

	#define STRNCPY(d,s,n) (*(d) = '\0', strncat((d), (s), (n)-1))

					/* -1 assuming that you want to
				 	write at must n bytes altogether */
-- 
Mark Brader, msb@sq.com      "C takes the point of view that the programmer
SoftQuad Inc., Toronto        is always right"           -- Michael DeCorte

This article is in the public domain.
Technical Details
Message-ID: <1995Aug15.171236.3809@sq.com>
Path: arc1-ca.pugleaf.net!in2.uu.net!utcsri!utnut!torn!sq!msb
References: <Pine.ULT.3.91.950807194041.19202A-100000@mars> <40fn05$686@kruuna.helsinki.fi> <40fng8$7ep@kruuna.helsinki.fi> <1995Aug15.121547.11587@friend.kastle.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!in2.uu.net!utcsri!utnut!torn!sq!msb
From: msb@sq.com (Mark Brader)
Subject: Re: gets() and getchar
Message-ID: <1995Aug15.171236.3809@sq.com>
Organization: SoftQuad Inc., Toronto, Canada
References: <Pine.ULT.3.91.950807194041.19202A-100000@mars> <40fn05$686@kruuna.helsinki.fi> <40fng8$7ep@kruuna.helsinki.fi> <1995Aug15.121547.11587@friend.kastle.com>
Date: Tue, 15 Aug 1995 17:12:36 GMT