summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2023-09-30 10:55:49 (GMT)
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2023-09-30 10:55:49 (GMT)
commit51aa8b2d9d3812b74fb4d15da712a31d549d928b (patch)
tree4407b9a070e72ceaeee239a3819ccee710a404f4
parent6947a8cea9335c2eeefb8929aa663db49ecac5a1 (diff)
downloadmonitoring-plugins-51aa8b2d9d3812b74fb4d15da712a31d549d928b.tar.gz
Document new np_add_regex more and add error handling
-rw-r--r--lib/utils_disk.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index 34401e2..884f005 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -41,15 +41,40 @@ np_add_name (struct name_list **list, const char *name)
41 *list = new_entry; 41 *list = new_entry;
42} 42}
43 43
44/* Initialises a new regex at the begin of list via regcomp(3) */ 44/* @brief Initialises a new regex at the begin of list via regcomp(3)
45 *
46 * @details if the regex fails to compile the error code of regcomp(3) is returned
47 * and list is not modified, otherwise list is modified to point to the new
48 * element
49 * @param list Pointer to a linked list of regex_list elements
50 * @param regex the string containing the regex which should be inserted into the list
51 * @param clags the cflags parameter for regcomp(3)
52 */
45int 53int
46np_add_regex (struct regex_list **list, const char *regex, int cflags) 54np_add_regex (struct regex_list **list, const char *regex, int cflags)
47{ 55{
48 struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); 56 struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry);
49 new_entry->next = *list;
50 *list = new_entry;
51 57
52 return regcomp(&new_entry->regex, regex, cflags); 58 if (new_entry == NULL) {
59 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
60 strerror(errno));
61 }
62
63 int regcomp_result = regcomp(&new_entry->regex, regex, cflags);
64
65 if (!regcomp_result) {
66 // regcomp succeded
67 new_entry->next = *list;
68 *list = new_entry;
69
70 return 0;
71 } else {
72 // regcomp failed
73 free(new_entry);
74
75 return regcomp_result;
76 }
77
53} 78}
54 79
55/* Initialises a new parameter at the end of list */ 80/* Initialises a new parameter at the end of list */