diff options
| author | Sumit Bose <sbose@redhat.com> | 2015-06-16 16:58:23 +0200 |
|---|---|---|
| committer | Jakub Hrozek <jhrozek@redhat.com> | 2015-06-19 18:48:13 +0200 |
| commit | a99845006f96f9d1e7af871ec67c71cee8408a62 (patch) | |
| tree | 25a873d57d0f695652263dfc05de141859ff5a69 /src | |
| parent | 8d4dedea12e2b71f83a1b0e5f0fc5cdb706dcf98 (diff) | |
| download | sssd-a99845006f96f9d1e7af871ec67c71cee8408a62.tar.gz sssd-a99845006f96f9d1e7af871ec67c71cee8408a62.tar.xz sssd-a99845006f96f9d1e7af871ec67c71cee8408a62.zip | |
utils: add get_last_x_chars()
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/tests/cmocka/test_string_utils.c | 28 | ||||
| -rw-r--r-- | src/tests/cmocka/test_utils.c | 1 | ||||
| -rw-r--r-- | src/tests/cmocka/test_utils.h | 1 | ||||
| -rw-r--r-- | src/util/string_utils.c | 17 | ||||
| -rw-r--r-- | src/util/util.h | 2 |
5 files changed, 49 insertions, 0 deletions
diff --git a/src/tests/cmocka/test_string_utils.c b/src/tests/cmocka/test_string_utils.c index 5d3fcf4f..4832015a 100644 --- a/src/tests/cmocka/test_string_utils.c +++ b/src/tests/cmocka/test_string_utils.c @@ -192,3 +192,31 @@ void test_guid_blob_to_string_buf(void **state) assert_string_equal(test_data[c].guid_str, str_buf); } } + +void test_get_last_x_chars(void **state) +{ + const char *s; + + s = get_last_x_chars(NULL, 0); + assert_null(s); + + s = get_last_x_chars("abc", 0); + assert_non_null(s); + assert_string_equal(s, ""); + + s = get_last_x_chars("abc", 1); + assert_non_null(s); + assert_string_equal(s, "c"); + + s = get_last_x_chars("abc", 2); + assert_non_null(s); + assert_string_equal(s, "bc"); + + s = get_last_x_chars("abc", 3); + assert_non_null(s); + assert_string_equal(s, "abc"); + + s = get_last_x_chars("abc", 4); + assert_non_null(s); + assert_string_equal(s, "abc"); +} diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c index e7f97f76..d3d00fed 100644 --- a/src/tests/cmocka/test_utils.c +++ b/src/tests/cmocka/test_utils.c @@ -1267,6 +1267,7 @@ int main(int argc, const char *argv[]) cmocka_unit_test(test_replace_whitespaces), cmocka_unit_test(test_reverse_replace_whitespaces), cmocka_unit_test(test_guid_blob_to_string_buf), + cmocka_unit_test(test_get_last_x_chars), cmocka_unit_test_setup_teardown(test_add_strings_lists, setup_add_strings_lists, teardown_add_strings_lists), diff --git a/src/tests/cmocka/test_utils.h b/src/tests/cmocka/test_utils.h index 61ef7e43..8b516316 100644 --- a/src/tests/cmocka/test_utils.h +++ b/src/tests/cmocka/test_utils.h @@ -30,5 +30,6 @@ void test_textual_public_key(void **state); void test_replace_whitespaces(void **state); void test_reverse_replace_whitespaces(void **state); void test_guid_blob_to_string_buf(void **state); +void test_get_last_x_chars(void **state); #endif /* __TESTS__CMOCKA__TEST_UTILS_H__ */ diff --git a/src/util/string_utils.c b/src/util/string_utils.c index 71b2a092..b6037621 100644 --- a/src/util/string_utils.c +++ b/src/util/string_utils.c @@ -108,3 +108,20 @@ errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf, return EOK; } + +const char *get_last_x_chars(const char *str, size_t x) +{ + size_t len; + + if (str == NULL) { + return NULL; + } + + len = strlen(str); + + if (len < x) { + return str; + } + + return (str + len - x); +} diff --git a/src/util/util.h b/src/util/util.h index 786ed303..3d90cf0d 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -628,6 +628,8 @@ char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx, errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf, size_t buf_size); +const char *get_last_x_chars(const char *str, size_t x); + /* from become_user.c */ errno_t become_user(uid_t uid, gid_t gid); struct sss_creds; |
