{"id":3873,"date":"2019-12-07T00:01:09","date_gmt":"2019-12-07T08:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3873"},"modified":"2019-12-14T08:39:19","modified_gmt":"2019-12-14T16:39:19","slug":"non-standard-function-strlcpy","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3873","title":{"rendered":"Non-Standard Function: <em>strlcpy()<\/em>"},"content":{"rendered":"<p>I though writing a substitute <em>strlcpy()<\/em> function would be easy. Boy was I wrong!<br \/>\n<!--more--><br \/>\nFirst, a look at the <em>strlcpy()<\/em> function&#8217;s prototype:<\/p>\n<p><code>size_t strlcpy(char * restrict dst, const char * restrict src, size_t dstsize);<\/code><\/p>\n<p>For comparison, here is the <em>man<\/em> page definition for the <em>strcpy()<\/em> function:<\/p>\n<p><code>char * stpcpy(char * dst, const char * src);<\/code><\/p>\n<p>In both functions, the contents of string <code>src<\/code> are copied into buffer <code>dst<\/code>. With the <em>strlcpy()<\/em> function, however, only <code>dstsize<\/code> characters maximum are copied, and the value returned is the size of string <code>src<\/code>. (The value returned is always the size of string <code>src<\/code> regardless of how many characters are copied.)<\/p>\n<p>Oh, and the nerds argue over whether the value returned is really a <em>size_t<\/em> value or an <em>int<\/em>. Nerds.<\/p>\n<p>My first attempt was simple enough: Copy up to <code>dstsize<\/code> characters from <code>src<\/code> to <code>dst<\/code>, terminating the operation when the null character is found in string <code>dst<\/code> or the value of <code>dstsize<\/code> was reached. Easy.<\/p>\n<p>But.<\/p>\n<p>The code seemed to run too well. Plus I had concerns about the function&#8217;s return value. So I wisely decided to run a control program first, just to monitor the behavior of the real <em>strlcpy()<\/em> function. Here is the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nvoid test(int size)\r\n{\r\n    char string[] = \"Hello there, Venus\";\r\n    char buffer[19];\r\n    int r;\r\n\r\n    r = strlcpy(buffer,string,size);\r\n\r\n    printf(\"Copied '%s' into '%s', length %d\\n\",\r\n            string,\r\n            buffer,\r\n            r\r\n          );\r\n}\r\n\r\nint main()\r\n{\r\n    test(19);\r\n    test(10);\r\n    test(1);\r\n    test(0);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>main()<\/em> function runs four tests, each with a different value for the number of characters to copy. In the <em>test()<\/em> function, array <code>string[<\/code>] is copied up to <code>size<\/code> characters into <code>buffer<\/code>. Output generated offers the original string, the copy, and the value returned by the <em>strlcpy()<\/em> function. These are all valid data points I must know if I am to properly reverse engineer the function. Here&#8217;s the test run;<\/p>\n<p><code>Copied 19 characters from 'Hello there, Venus' into 'Hello there, Venus', length 18<br \/>\nCopied 10 characters from 'Hello there, Venus' into 'Hello the', length 18<br \/>\nCopied 1 characters from 'Hello there, Venus' into '', length 18<br \/>\nCopied 0 characters from 'Hello there, Venus' into '', length 18<\/code><\/p>\n<p>In the first test, only 18 characters are copied because the null character is encountered.<\/p>\n<p>The second test copies 10 characters (9 plus the null character).<\/p>\n<p>The third and fourth tests copy no characters.<\/p>\n<p>In each test, the value 18 is returned, which is the length of the <code>string[]<\/code> array as would be reported by the <em>strlen()<\/em> function; the string&#8217;s buffer size is 19 when you include the null character.<\/p>\n<p>With this information in hand, I was better able to craft my version of the <em>strlcpy()<\/em> function, ensuring that it exhibited the same behavior as the actual function. I present my solution in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3886\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The non-standard <em>strlcpy()<\/em> function copies a string but limits the destination buffer length &mdash; a much-needed, secure improvement over the standard <em>strcpy()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3873\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3873","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3873"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3873\/revisions"}],"predecessor-version":[{"id":3907,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3873\/revisions\/3907"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}