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/
Re: stpecpy(): A better string copy function (was: memccpy(3) and stpcpy(3) status in C2x)
Alejandro Colomar (man-pages) Mon, 14 Feb 2022 13:25:27 -0800
- Fwd: stpecpy(): A better string copy functio... Alejandro Colomar (man-pages)
- Fwd: Re: stpecpy(): A better string cop... Alejandro Colomar (man-pages)
- Re: Fwd: Re: stpecpy(): A better st... Alejandro Colomar (man-pages)
- Re: Fwd: Re: stpecpy(): A bette... Alejandro Colomar (man-pages)
- memccpy(3) and stpcpy(3) status... Alejandro Colomar (man-pages)
- Re: memccpy(3) and stpcpy(3... Martin Sebor
- Re: memccpy(3) and stp... Steffen Nurpmeso
- Re: stpecpy(): A b... Alejandro Colomar (man-pages)
- Re: stpecpy(): A better string copy fun... Douglas McIlroy
- Re: stpecpy(): A better string copy... Larry McVoy