1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Introduce dm_strncpy

Should be faster then strncpy - since we could avoid clearing 4KB pages
with each strncpy(...,PATH_MAX).
Also it's easy to check whether string fit - and eventually avoid
to continue working we incomplete string.
This commit is contained in:
Zdenek Kabelac 2012-02-23 22:45:43 +00:00
parent 499a161640
commit b3d10d7bf1
3 changed files with 20 additions and 0 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.73 - Version 1.02.73 -
==================================== ====================================
Add dm_strncpy() function as a faster strncpy() replacement.
Version 1.02.72 - 23rd February 2012 Version 1.02.72 - 23rd February 2012
==================================== ====================================

View File

@ -1211,6 +1211,15 @@ void dm_unescape_colons_and_at_signs(char *src,
char **substr_first_unquoted_colon, char **substr_first_unquoted_colon,
char **substr_first_unquoted_at_sign); char **substr_first_unquoted_at_sign);
/*
* Replacement for strncpy() function.
*
* Copies no more than n bytes from string pointed by src to the buffer
* pointed by dest and ensure string is finished with '\0'.
* Returns 0 if the whole string does not fit.
*/
int dm_strncpy(char *dest, const char *src, size_t n);
/************************** /**************************
* file/stream manipulation * file/stream manipulation
**************************/ **************************/

View File

@ -410,3 +410,13 @@ void dm_unescape_colons_and_at_signs(char *src,
*substr_first_unquoted_at_sign = arr_substr_first_unquoted[1]; *substr_first_unquoted_at_sign = arr_substr_first_unquoted[1];
} }
int dm_strncpy(char *dest, const char *src, size_t n)
{
if (memccpy(dest, src, 0, n))
return 1;
if (n > 0)
dest[n - 1] = '\0';
return 0;
}