diff options
author | stderr64 <linuxwizard@voidnet.dy.fi> | 2024-01-28 23:14:53 +0200 |
---|---|---|
committer | stderr64 <linuxwizard@voidnet.dy.fi> | 2024-01-28 23:14:53 +0200 |
commit | 4d8e92033a32e17b0f5875aa67f0ccc67a212cc7 (patch) | |
tree | daf721d1b40cfe2c181f37009e6c45947a2185b4 /str_fns.h | |
parent | bee0ea1cf58e1dd7ca9221105ddade3d5b4ddcbe (diff) | |
download | CWebHook-4d8e92033a32e17b0f5875aa67f0ccc67a212cc7.tar.gz CWebHook-4d8e92033a32e17b0f5875aa67f0ccc67a212cc7.tar.zst |
Added string copy function that only copies specified amount of characters
Diffstat (limited to 'str_fns.h')
-rw-r--r-- | str_fns.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/str_fns.h b/str_fns.h new file mode 100644 index 0000000..d580231 --- /dev/null +++ b/str_fns.h @@ -0,0 +1,20 @@ +bool str_copy( char *dststr, char *srcstr, size_t max_chars ){ + if ( max_chars <= 0 ) + return false; + if ( srcstr == NULL ) + return false; + size_t src_len = (size_t)strnlen( srcstr, max_chars ); + if ( src_len <= 0 ) + return false; + bool need_null = false; + if ( src_len >= max_chars ) + need_null = true; + size_t copied_chars = 0; + for ( ; srcstr[copied_chars] != '\0' && copied_chars < max_chars; copied_chars++ ) + dststr[copied_chars] = srcstr[copied_chars]; + if ( need_null ) + dststr[copied_chars + 1] = '\0'; + if ( strncmp((const char*)dststr, (const char*)srcstr, max_chars) != 0 ) + return false; + return true; +} |