From 2090d09b90b46c87794e432ea68b7b252403aa9b Mon Sep 17 00:00:00 2001 From: "David Kaspar [Dee'Kej]" Date: Thu, 25 May 2017 14:43:55 +0200 Subject: rename_device.c: rewrite of isCfg() function * .orig & .old now recognized as config files * code cleanup --- src/rename_device.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/rename_device.c b/src/rename_device.c index d49205ed..6d6030eb 100644 --- a/src/rename_device.c +++ b/src/rename_device.c @@ -104,20 +104,40 @@ char *read_subchannels(char *path) { #endif +/* + * Taken from systemd: + * https://github.com/systemd/systemd/blob/master/src/basic/string-util.c#L49 + */ +char* endswith(const char *s, const char *postfix) { + size_t sl, pl; + + sl = strlen(s); + pl = strlen(postfix); + + if (pl == 0) + return (char*) s + sl; + + if (sl < pl) + return NULL; + + if (memcmp(s + sl - pl, postfix, pl) != 0) + return NULL; + + return (char*) s + sl - pl; +} + int isCfg(const struct dirent *dent) { - int len = strlen(dent->d_name); - - if (strncmp(dent->d_name,"ifcfg-",6)) - return 0; - if (strstr(dent->d_name,"rpmnew") || - strstr(dent->d_name,"rpmsave") || - strstr(dent->d_name,"rpmorig")) - return 0; - if (dent->d_name[len-1] == '~') - return 0; - if (!strncmp(dent->d_name+len-4,".bak",4)) + if (strncmp(dent->d_name, "ifcfg-",6) || + endswith(dent->d_name, ".rpmnew") != NULL || + endswith(dent->d_name, ".rpmsave") != NULL || + endswith(dent->d_name, ".rpmorig") != NULL || + endswith(dent->d_name, ".orig") != NULL || + endswith(dent->d_name, ".old") != NULL || + endswith(dent->d_name, ".bak") != NULL || + endswith(dent->d_name, "~") != NULL) return 0; - return 1; + else + return 1; } static inline char *dequote(char *start, char *end) { -- cgit v1.2.1