// strlcpy.c // PUBLIC DOMAIN // http:///www.purposeful.co.uk/software/yabaplib /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include /* memchr() */ size_t strnlen(const char *str, size_t max_length) {{{ const char *terminator = memchr(str, '\0', max_length); if (terminator) { return (terminator - str); } else { return max_length; } }}} size_t strlcpy(char *dest, const char *src, size_t dest_size) {{{ size_t src_len = strlen(src); if (src_len < dest_size) { memcpy(dest, src, (src_len + 1)); } else { if (dest_size > 0) { memcpy(dest, src, (dest_size - 1)); dest[dest_size - 1] = 0; } } return src_len; }}} size_t strlcat(char *dest, const char *src, size_t dest_size) {{{ size_t dest_len = strnlen(dest, dest_size); return (dest_len + strlcpy(&dest[dest_len], src, (dest_size - dest_len))); }}}