diff options
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; +} |