From d31a696cadb0bba0914d76aad0eb48c6e7962b8e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Aug 2023 18:13:01 +0200 Subject: Introduce np_add_regex() --- lib/utils_disk.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib/utils_disk.c') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 582d3ea1..ce02fdff 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -40,6 +40,17 @@ np_add_name (struct name_list **list, const char *name) *list = new_entry; } +/* Initialises a new regex at the begin of list via regcomp(3) */ +int +np_add_regex (struct regex_list **list, const char *regex, int cflags) +{ + struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); + new_entry->next = *list; + *list = new_entry; + + return regcomp(&new_entry->regex, regex, cflags); +} + /* Initialises a new parameter at the end of list */ struct parameter_list * np_add_parameter(struct parameter_list **list, const char *name) -- cgit v1.2.3-74-g34f1 From 1f694195b4a6beef30bbbbffa5835a993be97a9c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Aug 2023 18:26:12 +0200 Subject: Introduce np_find_regmatch() --- lib/utils_disk.c | 25 +++++++++++++++++++++++++ lib/utils_disk.h | 1 + 2 files changed, 26 insertions(+) (limited to 'lib/utils_disk.c') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index ce02fdff..34401e21 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -29,6 +29,7 @@ #include "common.h" #include "utils_disk.h" #include "gl/fsusage.h" +#include void np_add_name (struct name_list **list, const char *name) @@ -207,6 +208,30 @@ np_find_name (struct name_list *list, const char *name) return FALSE; } +/* Returns TRUE if name is in list */ +bool +np_find_regmatch (struct regex_list *list, const char *name) +{ + int len; + regmatch_t m; + + if (name == NULL) { + return false; + } + + len = strlen(name); + + for (; list; list = list->next) { + /* Emulate a full match as if surrounded with ^( )$ + by checking whether the match spans the whole name */ + if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { + return true; + } + } + + return false; +} + int np_seen_name(struct name_list *list, const char *name) { diff --git a/lib/utils_disk.h b/lib/utils_disk.h index bda088f6..6b83ac74 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -42,6 +42,7 @@ void np_add_name (struct name_list **list, const char *name); int np_find_name (struct name_list *list, const char *name); int np_seen_name (struct name_list *list, const char *name); int np_add_regex (struct regex_list **list, const char *regex, int cflags); +bool np_find_regmatch (struct regex_list *list, const char *name); struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); -- cgit v1.2.3-74-g34f1 From 51aa8b2d9d3812b74fb4d15da712a31d549d928b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:55:49 +0200 Subject: Document new np_add_regex more and add error handling --- lib/utils_disk.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'lib/utils_disk.c') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 34401e21..884f0052 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) *list = new_entry; } -/* Initialises a new regex at the begin of list via regcomp(3) */ +/* @brief Initialises a new regex at the begin of list via regcomp(3) + * + * @details if the regex fails to compile the error code of regcomp(3) is returned + * and list is not modified, otherwise list is modified to point to the new + * element + * @param list Pointer to a linked list of regex_list elements + * @param regex the string containing the regex which should be inserted into the list + * @param clags the cflags parameter for regcomp(3) + */ int np_add_regex (struct regex_list **list, const char *regex, int cflags) { struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); - new_entry->next = *list; - *list = new_entry; - return regcomp(&new_entry->regex, regex, cflags); + if (new_entry == NULL) { + die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), + strerror(errno)); + } + + int regcomp_result = regcomp(&new_entry->regex, regex, cflags); + + if (!regcomp_result) { + // regcomp succeded + new_entry->next = *list; + *list = new_entry; + + return 0; + } else { + // regcomp failed + free(new_entry); + + return regcomp_result; + } + } /* Initialises a new parameter at the end of list */ -- cgit v1.2.3-74-g34f1 From 128a24be2296af175c5e7adf875f9d0e8a619278 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:59:26 +0200 Subject: Fix typo --- lib/utils_disk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/utils_disk.c') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 884f0052..f5ac0b30 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -63,7 +63,7 @@ np_add_regex (struct regex_list **list, const char *regex, int cflags) int regcomp_result = regcomp(&new_entry->regex, regex, cflags); if (!regcomp_result) { - // regcomp succeded + // regcomp succeeded new_entry->next = *list; *list = new_entry; -- cgit v1.2.3-74-g34f1