stpcpy, stpncpy, wcpcpy, wcpncpy in lib C [PSARC/2009/084 Self Review]

James Carlson james.d.carlson at sun.com
Tue Feb 10 06:00:37 PST 2009


Casper.Dik at Sun.COM writes:
> >My primary interest was |stpcpy()| to get performance of the locale
> >codepaths fixed (we assume that we check at the _beginning_ that all
> >allocated buffers have sufficient size (since the use of something like
> >|stplcpy()| would completely kill the performance advantage since more
> >registers and more operations are required for string copies)). Right
> >now these codepaths are seveal _factors_ (more than 2x) _slower_ than on
> >Linux and one of the primary "root cause" is the over-use of inefficient
> >buffer handling (whic hwe try to partially address with the use of
> >|stpcpy()|).
> 
> stplcpy would be difficult to implement, except if you add another pointer
> (pointer to the base of the buffer)

It only needs to be analogous to strlcpy, not strlcat.

char *
stplcpy(char *dst, const char *src, size_t dlen)
{
	/* allow for terminating NUL in all cases */
	if (dlen-- == 0)
		return (dst);
	while (dlen-- > 0) {
		if ((*dst++ = *src++) == '\0)
			return (dst - 1);
	}
	*dst = '\0';
	return (dst);
}

-- 
James Carlson, Solaris Networking              <james.d.carlson at sun.com>
Sun Microsystems / 35 Network Drive        71.232W   Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757   42.496N   Fax +1 781 442 1677



More information about the opensolaris-arc mailing list