summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/developer/lists.rst14
-rw-r--r--lib/frrstr.c16
-rw-r--r--lib/frrstr.h19
-rw-r--r--vtysh/vtysh.c2
4 files changed, 40 insertions, 11 deletions
diff --git a/doc/developer/lists.rst b/doc/developer/lists.rst
index 58c17248dd..fc47a67e42 100644
--- a/doc/developer/lists.rst
+++ b/doc/developer/lists.rst
@@ -115,7 +115,7 @@ Functions provided:
+------------------------------------+------+------+------+---------+------------+
| _find_lt, _find_gteq | -- | -- | -- | yes | yes |
+------------------------------------+------+------+------+---------+------------+
-| use with for_each() macros | yes | yes | yes | yes | yes |
+| use with frr_each() macros | yes | yes | yes | yes | yes |
+------------------------------------+------+------+------+---------+------------+
@@ -176,7 +176,7 @@ Common iteration macros
The following iteration macros work across all data structures:
-.. c:function:: for_each(Z, &head, item)
+.. c:function:: frr_each(Z, &head, item)
Equivalent to:
@@ -187,7 +187,7 @@ The following iteration macros work across all data structures:
Note that this will fail if the list is modified while being iterated
over.
-.. c:function:: for_each_safe(Z, &head, item)
+.. c:function:: frr_each_safe(Z, &head, item)
Same as the previous, but the next element is pre-loaded into a "hidden"
variable (named ``Z_safe``.) Equivalent to:
@@ -206,7 +206,7 @@ The following iteration macros work across all data structures:
tables is resized while iterating. This will cause items to be
skipped or iterated over twice.
-.. c:function:: for_each_from(Z, &head, item, from)
+.. c:function:: frr_each_from(Z, &head, item, from)
Iterates over the list, starting at item ``from``. This variant is "safe"
as in the previous macro. Equivalent to:
@@ -363,7 +363,7 @@ are several functions exposed to insert data:
itemtype *prev = NULL, *item;
- for_each_safe(Z, head, item) {
+ frr_each_safe(Z, head, item) {
if (something) {
Z_add_after(head, prev, item);
break;
@@ -585,7 +585,7 @@ Iteration:
struct item *i;
pthread_rwlock_rdlock(&itemhead_rwlock);
- for_each(itemlist, &itemhead, i) {
+ frr_each(itemlist, &itemhead, i) {
/* lock must remain held while iterating */
...
}
@@ -602,7 +602,7 @@ Head removal (pop) and deallocation:
pthread_rwlock_unlock(&itemhead_rwlock);
/* i might still be visible for another thread doing an
- * for_each() (but won't be returned by another pop()) */
+ * frr_each() (but won't be returned by another pop()) */
...
pthread_rwlock_wrlock(&itemhead_rwlock);
diff --git a/lib/frrstr.c b/lib/frrstr.c
index fbbc890ec6..c575c0b568 100644
--- a/lib/frrstr.c
+++ b/lib/frrstr.c
@@ -178,7 +178,7 @@ char *frrstr_replace(const char *str, const char *find, const char *replace)
return nustr;
}
-bool begins_with(const char *str, const char *prefix)
+bool frrstr_startswith(const char *str, const char *prefix)
{
if (!str || !prefix)
return false;
@@ -192,6 +192,20 @@ bool begins_with(const char *str, const char *prefix)
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)
{
for (; *str != '\0'; str++)
diff --git a/lib/frrstr.h b/lib/frrstr.h
index d40ca14ba5..3a935c90cb 100644
--- a/lib/frrstr.h
+++ b/lib/frrstr.h
@@ -109,6 +109,7 @@ void frrstr_strvec_free(vector v);
* the replacement on 'str'. This must be freed by the caller.
*/
char *frrstr_replace(const char *str, const char *find, const char *replace);
+
/*
* Prefix match for string.
*
@@ -119,9 +120,23 @@ char *frrstr_replace(const char *str, const char *find, const char *replace);
* prefix to look for
*
* Returns:
- * true str starts with prefix, false otherwise
+ * true if str starts with prefix, false otherwise
+ */
+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 begins_with(const char *str, const char *prefix);
+bool frrstr_endswith(const char *str, const char *suffix);
/*
* Check the string only contains digit characters.
diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c
index f1a5eca74b..a0b119c3eb 100644
--- a/vtysh/vtysh.c
+++ b/vtysh/vtysh.c
@@ -3347,7 +3347,7 @@ static void vtysh_update_all_instances(struct vtysh_client *head_client)
dir = opendir(vtydir);
if (dir) {
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")) {
if (n == MAXIMUM_INSTANCES) {
fprintf(stderr,