]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: replace begins_with, add frrstr_endswith 4390/head
authorQuentin Young <qlyoung@cumulusnetworks.com>
Sat, 18 May 2019 01:35:38 +0000 (01:35 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Thu, 23 May 2019 20:52:01 +0000 (20:52 +0000)
* 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>
lib/frrstr.c
lib/frrstr.h
vtysh/vtysh.c

index fbbc890ec69fe49cd9af1913fcc383a7907dcd31..c575c0b568ef7dd5981fadaa003d235f0ea643fc 100644 (file)
@@ -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++)
index d40ca14ba5eae6ebec0151dda98aadbf277d6545..3a935c90cb26062c0d9cf5d97888634aac2e10de 100644 (file)
@@ -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.
index f1a5eca74bc4e21d5cc79e64b3a15fe9b9e9bfbd..a0b119c3ebaf23679efac8f33af7d252667dd505 100644 (file)
@@ -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,