diff options
| author | datamuc <m@rbfh.de> | 2023-10-06 11:01:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-06 11:01:01 +0200 |
| commit | 5886cfbcb0f9650fc538efa7d825cc4015beb5bb (patch) | |
| tree | 7168db8bc00b5a3c4308004e66735ebbf382eb27 /lib | |
| parent | 1ad7e163fad45d33a44f398fb2416f1b9086469a (diff) | |
| parent | be0e475339346907c86de5d797f0fab1e071f75c (diff) | |
| download | monitoring-plugins-5886cfbcb0f9650fc538efa7d825cc4015beb5bb.tar.gz | |
Merge branch 'master' into merge-jitter
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils_disk.c | 61 | ||||
| -rw-r--r-- | lib/utils_disk.h | 8 |
2 files changed, 69 insertions, 0 deletions
diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 582d3ea1..f5ac0b30 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c | |||
| @@ -29,6 +29,7 @@ | |||
| 29 | #include "common.h" | 29 | #include "common.h" |
| 30 | #include "utils_disk.h" | 30 | #include "utils_disk.h" |
| 31 | #include "gl/fsusage.h" | 31 | #include "gl/fsusage.h" |
| 32 | #include <string.h> | ||
| 32 | 33 | ||
| 33 | void | 34 | void |
| 34 | np_add_name (struct name_list **list, const char *name) | 35 | np_add_name (struct name_list **list, const char *name) |
| @@ -40,6 +41,42 @@ np_add_name (struct name_list **list, const char *name) | |||
| 40 | *list = new_entry; | 41 | *list = new_entry; |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 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 | */ | ||
| 53 | int | ||
| 54 | np_add_regex (struct regex_list **list, const char *regex, int cflags) | ||
| 55 | { | ||
| 56 | struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); | ||
| 57 | |||
| 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 succeeded | ||
| 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 | |||
| 78 | } | ||
| 79 | |||
| 43 | /* Initialises a new parameter at the end of list */ | 80 | /* Initialises a new parameter at the end of list */ |
| 44 | struct parameter_list * | 81 | struct parameter_list * |
| 45 | np_add_parameter(struct parameter_list **list, const char *name) | 82 | np_add_parameter(struct parameter_list **list, const char *name) |
| @@ -196,6 +233,30 @@ np_find_name (struct name_list *list, const char *name) | |||
| 196 | return FALSE; | 233 | return FALSE; |
| 197 | } | 234 | } |
| 198 | 235 | ||
| 236 | /* Returns TRUE if name is in list */ | ||
| 237 | bool | ||
| 238 | np_find_regmatch (struct regex_list *list, const char *name) | ||
| 239 | { | ||
| 240 | int len; | ||
| 241 | regmatch_t m; | ||
| 242 | |||
| 243 | if (name == NULL) { | ||
| 244 | return false; | ||
| 245 | } | ||
| 246 | |||
| 247 | len = strlen(name); | ||
| 248 | |||
| 249 | for (; list; list = list->next) { | ||
| 250 | /* Emulate a full match as if surrounded with ^( )$ | ||
| 251 | by checking whether the match spans the whole name */ | ||
| 252 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { | ||
| 253 | return true; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | return false; | ||
| 258 | } | ||
| 259 | |||
| 199 | int | 260 | int |
| 200 | np_seen_name(struct name_list *list, const char *name) | 261 | np_seen_name(struct name_list *list, const char *name) |
| 201 | { | 262 | { |
diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 3b5a45f8..6b83ac74 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h | |||
| @@ -10,6 +10,12 @@ struct name_list | |||
| 10 | struct name_list *next; | 10 | struct name_list *next; |
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | struct regex_list | ||
| 14 | { | ||
| 15 | regex_t regex; | ||
| 16 | struct regex_list *next; | ||
| 17 | }; | ||
| 18 | |||
| 13 | struct parameter_list | 19 | struct parameter_list |
| 14 | { | 20 | { |
| 15 | char *name; | 21 | char *name; |
| @@ -35,6 +41,8 @@ struct parameter_list | |||
| 35 | void np_add_name (struct name_list **list, const char *name); | 41 | void np_add_name (struct name_list **list, const char *name); |
| 36 | int np_find_name (struct name_list *list, const char *name); | 42 | int np_find_name (struct name_list *list, const char *name); |
| 37 | int np_seen_name (struct name_list *list, const char *name); | 43 | int np_seen_name (struct name_list *list, const char *name); |
| 44 | int np_add_regex (struct regex_list **list, const char *regex, int cflags); | ||
| 45 | bool np_find_regmatch (struct regex_list *list, const char *name); | ||
| 38 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); | 46 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); |
| 39 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); | 47 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); |
| 40 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); | 48 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); |
