lib: replace begins_with, add frrstr_endswith

* Change 'begins_with' to 'frrstr_startswith' for consistency
* Add suffix checker, frrstr_endswith()
* Update vtysh to use the new function

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
Quentin Young 2019-05-18 01:35:38 +00:00
parent 528628cb2e
commit 90cf59eccf
3 changed files with 33 additions and 4 deletions

View file

@ -178,7 +178,7 @@ char *frrstr_replace(const char *str, const char *find, const char *replace)
return nustr; return nustr;
} }
bool begins_with(const char *str, const char *prefix) bool frrstr_startswith(const char *str, const char *prefix)
{ {
if (!str || !prefix) if (!str || !prefix)
return false; return false;
@ -192,6 +192,20 @@ bool begins_with(const char *str, const char *prefix)
return strncmp(str, prefix, lenprefix) == 0; return strncmp(str, prefix, lenprefix) == 0;
} }
bool frrstr_endswith(const char *str, const char *suffix)
{
if (!str || !suffix)
return false;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return false;
return strncmp(&str[lenstr - lensuffix], suffix, lensuffix) == 0;
}
int all_digit(const char *str) int all_digit(const char *str)
{ {
for (; *str != '\0'; str++) for (; *str != '\0'; str++)

View file

@ -109,6 +109,7 @@ void frrstr_strvec_free(vector v);
* the replacement on 'str'. This must be freed by the caller. * the replacement on 'str'. This must be freed by the caller.
*/ */
char *frrstr_replace(const char *str, const char *find, const char *replace); char *frrstr_replace(const char *str, const char *find, const char *replace);
/* /*
* Prefix match for string. * Prefix match for string.
* *
@ -119,9 +120,23 @@ char *frrstr_replace(const char *str, const char *find, const char *replace);
* prefix to look for * prefix to look for
* *
* Returns: * Returns:
* true str starts with prefix, false otherwise * true if str starts with prefix, false otherwise
*/ */
bool begins_with(const char *str, const char *prefix); bool frrstr_startswith(const char *str, const char *prefix);
/*
* Suffix match for string.
*
* str
* string to check for suffix match
*
* suffix
* suffix to look for
*
* Returns:
* true if str ends with suffix, false otherwise
*/
bool frrstr_endswith(const char *str, const char *suffix);
/* /*
* Check the string only contains digit characters. * Check the string only contains digit characters.

View file

@ -3347,7 +3347,7 @@ static void vtysh_update_all_instances(struct vtysh_client *head_client)
dir = opendir(vtydir); dir = opendir(vtydir);
if (dir) { if (dir) {
while ((file = readdir(dir)) != NULL) { while ((file = readdir(dir)) != NULL) {
if (begins_with(file->d_name, "ospfd-") if (frrstr_startswith(file->d_name, "ospfd-")
&& ends_with(file->d_name, ".vty")) { && ends_with(file->d_name, ".vty")) {
if (n == MAXIMUM_INSTANCES) { if (n == MAXIMUM_INSTANCES) {
fprintf(stderr, fprintf(stderr,