My Own strlcpy() Function

Armed with information about how the non-standard strlcpy() function is implemented by my compiler (see last week’s Lesson), and fully testing its input and output, I was better able to craft my own version. Granted, it’s not the way I would have coded things on my own, but the point is to recreate the function exactly so it can be used as a substitute.

Here is my reverse-engineered solution to the strlcpy() function, which includes the main() and test() functions from last week’s sample code:

#include <stdio.h>

size_t strlcpy(char *restrict dst, const char *restrict src, size_t dstsize)
{
    int offset;

    /* duplicate the string up to dstsize */
    offset = 0;
        /* guard against silly dstsize values */
    if( dstsize > 0 )
    {
        while( *(src+offset) != '\0' )
        {
            /* bail if dstsize is met */
            if( offset==dstsize )
            {
                offset--;
                break;
            }

            /* duplicate the string */
            *(dst+offset) = *(src+offset);
            offset++;
        }
    }
    /* always remember to cap a created string! */
    *(dst+offset) = '\0';
    
    /* return the string length of src */
    while( *(src+offset) != '\0' )
        offset++;

    return(offset);
}

void test(int size)
{
    char string[] = "Hello there, Venus";
    char buffer[19];
    int r;

    r = strlcpy(buffer,string,size);

    printf("Copied '%s' into '%s', length %d\n",
            string,
            buffer,
            r
          );
}

int main()
{
    test(19);
    test(10);
    test(1);
    test(0);

    return(0);
}

My strlcpy() function uses int variable offset to track copying from src to dst. An if test immediately determines whether the dstsize value is valid:

if( dstsize > 0 )

If not, the dst string is capped (a must) at Line 27:

*(dst+offset) = '\0';

When the dstsize value is legitimate (greater than zero) a while loop at Line 12 copies the characters from src to dst. Line 15 ensures that only dstsize characters (or fewer) are copied. Copying takes place at Line 22. And the offset variable is incremented at Line 23.

The final while loop at Line 30 counts the length of the scr string. I couldn’t use the strlen() function because doing so requires including the string.h header file. This inclusion results in a slew of warnings and errors because of my strlcpy() function and its conflicting definition with strlcpy() supplied by the system’s C library.

My code’s output matched that of the control program, so I think I’m good. Had I not cross-checked my version with the one supplied with the compiler, it would have returned different values. In fact, the man page for strlcpy() contains extensive notes and examples to explain how the return value is used. It’s interesting information, especially if you are to properly concoct your own, similar — and compatible — function.

Next week I tackle the strlcat() function.

2 thoughts on “My Own strlcpy() Function

  1. You use a Mac don’t you? Does Valgrind work for you? I read a comment somewhere recently by someone complaining that it doesn’t work on Darwin which I think is the most recent version of MacOS.

  2. I tried installed it using Homebrew. The report came back that valgrind is incompatible with Mac OS “High Sierra” onward. Bummer.

    Many command line utilities aren’t available on the Mac. I forget the last one I checked out, something recommended to me. It was a Linux programming tool that the Mac lacked. Or, I should say, that was unavailable via Homebrew.

Leave a Reply