diff options
author | David Kaspar [Dee'Kej] <dkaspar@redhat.com> | 2017-05-25 14:43:55 +0200 |
---|---|---|
committer | Dee'Kej <deekej@linuxmail.org> | 2017-05-25 17:50:55 +0200 |
commit | 2090d09b90b46c87794e432ea68b7b252403aa9b (patch) | |
tree | 1709521081948a2fedbbe9f3b507521875d48f60 /src | |
parent | 0fe96e5f3705d5c7e968d5fe06989390d63502c5 (diff) | |
download | initscripts-2090d09b90b46c87794e432ea68b7b252403aa9b.tar initscripts-2090d09b90b46c87794e432ea68b7b252403aa9b.tar.gz initscripts-2090d09b90b46c87794e432ea68b7b252403aa9b.tar.bz2 initscripts-2090d09b90b46c87794e432ea68b7b252403aa9b.tar.xz initscripts-2090d09b90b46c87794e432ea68b7b252403aa9b.zip |
rename_device.c: rewrite of isCfg() function
* .orig & .old now recognized as config files
* code cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/rename_device.c | 44 |
1 files 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) { |