[monitoring-plugins] utils_disk: add name_prev pointer to struct ...

Kristian Schuster git at monitoring-plugins.org
Sat Mar 11 10:30:12 CET 2023


 Module: monitoring-plugins
 Branch: master
 Commit: 9898a8ad7dabfabfe80785585a5bbc30b678bdb0
 Author: Kristian Schuster <116557017+KriSchu at users.noreply.github.com>
   Date: Sun Feb 19 13:44:04 2023 +0100
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=9898a8a

utils_disk: add name_prev pointer to struct parameter_list

Also added handling of name_prev in np_add_parameter and np_delete_parameter.
This make calling the np_delete_parameter function easier, because it requires
the previous element as second argument.

---

 lib/utils_disk.c | 19 +++++++++++++++++--
 lib/utils_disk.h |  1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index c7c9126..a1181d3 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -46,9 +46,10 @@ np_add_parameter(struct parameter_list **list, const char *name)
   struct parameter_list *current = *list;
   struct parameter_list *new_path;
   new_path = (struct parameter_list *) malloc (sizeof *new_path);
-  new_path->name = (char *) name;
+  new_path->name = (char *) malloc(strlen(name) + 1);
   new_path->best_match = NULL;
   new_path->name_next = NULL;
+  new_path->name_prev = NULL;
   new_path->freespace_bytes = NULL;
   new_path->freespace_units = NULL;
   new_path->freespace_percent = NULL;
@@ -74,13 +75,17 @@ np_add_parameter(struct parameter_list **list, const char *name)
   new_path->dused_inodes_percent = 0;
   new_path->dfree_inodes_percent = 0;
 
+  strcpy(new_path->name, name);
+
   if (current == NULL) {
     *list = new_path;
+    new_path->name_prev = NULL;
   } else {
     while (current->name_next) {
       current = current->name_next;
     }
     current->name_next = new_path;
+    new_path->name_prev = current;
   }
   return new_path;
 }
@@ -89,6 +94,9 @@ np_add_parameter(struct parameter_list **list, const char *name)
 struct parameter_list *
 np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
 {
+  if (item == NULL) {
+    return NULL;
+  }
   struct parameter_list *next;
 
   if (item->name_next)
@@ -96,10 +104,17 @@ np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
   else
     next = NULL;
 
-  free(item);
+  if (next)
+    next->name_prev = prev;
+
   if (prev)
     prev->name_next = next;
 
+  if (item->name) {
+    free(item->name);
+  }
+  free(item);
+
   return next;
 }
 
diff --git a/lib/utils_disk.h b/lib/utils_disk.h
index bf52e4c..3b5a45f 100644
--- a/lib/utils_disk.h
+++ b/lib/utils_disk.h
@@ -24,6 +24,7 @@ struct parameter_list
   char *group;
   struct mount_entry *best_match;
   struct parameter_list *name_next;
+  struct parameter_list *name_prev;
   uintmax_t total, available, available_to_root, used,
     inodes_free, inodes_free_to_root, inodes_used, inodes_total;
   double dfree_pct, dused_pct;



More information about the Commits mailing list