C strlcat implementation I have to reproduce some basic functions in C and one of them is strlcat.

After reading the man and testing the strlcat from &lt;bsd/string.h&gt; library, I wrote the following code which seems to be a working copy of the function:

#include &lt;string.h&gt;

size_t  ft_strlcat(char *dst, const char *src, size_t size)
{
    size_t  lsize;
    size_t  dsize;

    lsize = 0;
    dsize = strlen(dst);
    while (*dst &amp;&amp; size &gt; 0 &amp;&amp; size--)
    {
        dst++;
        lsize++;
    }
    while (*src &amp;&amp; size &gt; 1 &amp;&amp; size--)
        *dst++ = *src++;
    if (size == 1)
        *dst = '\0';
    return (dsize + lsize);
}


Is this implementation missing something?
Using freebsd strlcat as a guide.


"length" with C strings typical does not include the null character.  "size" usually does include the null character.  Suggest name change

// dsize = strlen(dst);
dlen = strlen(dst);

Code walks the array and repeatedly adjust 3 variables.  Could be done adjusting only 2.

while (*dst &amp;&amp; size &gt; 0 &amp;&amp; size--) {
    dst++;
    lsize++;
}
// vs.
size_t remaining_size = size;
while (*dst &amp;&amp; remaining_size &gt; 0) {
  dst++;
  remaining_size--;
}
size_t lsize = size - remaining_size;

The below could use memcpy() or strcpy().  These functions are typically optimized for speedy execution, especially long strings.  The key performance assessment should be based on long strings and with sufficient space.  Little need to optimize for undersized values of size.

while (*src &amp;&amp; size &gt; 1 &amp;&amp; size--)
  *dst++ = *src++;
// vs.
memcpy(dst, src, ...);

Code does not handle overlapping strings.  Should use restrict to indicate that.

size_t ft_strlcat(char * restrict dst, const char * restrict src, size_t size);

Pedantic code would test for overflow in dsize + lsize, but let us skip that corner case for now.
Initialize when declaring, if possible.

// size_t  lsize;
// lsize = 0;
size_t  lsize = 0;



Some lightly tested code follows.  

size_t ft2_strlcat(char *restrict dst, const char *restrict src, size_t size) {
  size_t s_length = strlen(src);
  size_t remaining_size = size;

  while (*dst &amp;&amp; remaining_size &gt; 0) {
    dst++;
    remaining_size--;
  }
  // Length of destination is the lesser of `size` and 
  // offset of the null character, if any. 
  size_t d_length = size - remaining_size;

  if (d_length &lt; remaining_size) {
    // We know there is at least room to write the \0

    size_t copy_length = remaining_size - 1;
    if (s_length &lt; copy_length) {
      copy_length = s_length;
    }
    memcpy(dst, src, copy_length);
    dst[copy_length] = '\0';
  }
  return d_length + s_length;
}