groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stpecpy(): A better string copy function (was: memccpy(3) and stpcpy


From: Alejandro Colomar (man-pages)
Subject: Re: stpecpy(): A better string copy function (was: memccpy(3) and stpcpy(3) status in C2x)
Date: Mon, 14 Feb 2022 22:25:13 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.1


On 2/14/22 21:34, Steffen Nurpmeso wrote:
> I personally like my
> 
>   /*! Copy \a{src} to \a{dst}, return pointer to NUL in \a{dst}.
>    * Returns \NIL if \a{dst} is not large enough; \a{dst} will always be
>    * terminated unless \a{n} was 0 on entry. */
>   EXPORT char *su_cs_pcopy_n(char *dst, char const *src, uz n);

Hmm, that's difficult to chain, isn't it?  You need to recalculate n
every time, as if chaining strlcpy(3) (not using strlcat(3)).
Also, it forces you to check for NULL _before_ calculating the length,
which is not a big requirement, but might be dangerous for users (it's
easy to misuse).


> 
> But mostly i either use memcpy if possible, or
> 
>   /*! Copy at most \a{n} bytes of \a{src} to \a{dst}, and return \a{dst} 
> again.
>    * Returns \NIL if \a{dst} is not large enough; \a{dst} will always be
>    * terminated unless \a{n} was 0 on entry.
>    * Also see \r{su_cs_pcopy_n()}. */
>   EXPORT char *su_cs_copy_n(char *dst, char const *src, uz n);

This one is what I used until a short time ago (I returned an int:
0-SUCCESS, 1-TRUNC, but the concept is the same).  When you don't need
to chain, it's quite good.  Basically, it's similar to strlcpy(3),
simpler to check for truncation, but you lose the free calculation of
the length.


Thanks for sharing that.

Cheers,

Alex


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]