diff options
author | Thomas Haller <thaller@redhat.com> | 2014-12-18 14:23:28 +0100 |
---|---|---|
committer | Dan Winship <danw@redhat.com> | 2015-01-07 12:31:49 -0500 |
commit | 2ef8f6edfe00836d4995e487edc7d74ca8661c5e (patch) | |
tree | 4deaf865180e1cb10c211a5ba2c50c6446d9e1d4 | |
parent | 675d545bcc614a9e9d734eb05c00d4e01c0797a4 (diff) | |
download | NetworkManager-2ef8f6edfe00836d4995e487edc7d74ca8661c5e.tar.gz |
ifcfg: refactor utils_get_ifcfg_name()
No need to allocate a temporary "base" variable. Just
search for the last '/' ourselves.
All the special handling that g_path_get_basename() does,
for example handling empty filenames and removing trailing
slashes, is not needed.
Thereby fix not to return empty names such as from "ifcfg-".
-rw-r--r-- | src/settings/plugins/ifcfg-rh/utils.c | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/settings/plugins/ifcfg-rh/utils.c b/src/settings/plugins/ifcfg-rh/utils.c index 5b9e9d7965..4378c6ebd8 100644 --- a/src/settings/plugins/ifcfg-rh/utils.c +++ b/src/settings/plugins/ifcfg-rh/utils.c @@ -25,6 +25,8 @@ #include <string.h> #include "nm-core-internal.h" +#include "nm-utils-internal.h" +#include "NetworkManagerUtils.h" #include "utils.h" #include "shvar.h" @@ -205,34 +207,37 @@ utils_cert_path (const char *parent, const char *suffix) const char * utils_get_ifcfg_name (const char *file, gboolean only_ifcfg) { - const char *name = NULL, *start = NULL; - char *base; + const char *name; g_return_val_if_fail (file != NULL, NULL); - base = g_path_get_basename (file); - if (!base) + name = strrchr (file, '/'); + if (!name) + name = file; + else + name++; + if (!*name) return NULL; - /* Find the point in 'file' where 'base' starts. We use 'file' since it's - * const and thus will survive after we free 'base'. - */ - start = file + strlen (file) - strlen (base); - g_assert (strcmp (start, base) == 0); - g_free (base); - - if (!strncmp (start, IFCFG_TAG, strlen (IFCFG_TAG))) - name = start + strlen (IFCFG_TAG); - else if (only_ifcfg == FALSE) { - if (!strncmp (start, KEYS_TAG, strlen (KEYS_TAG))) - name = start + strlen (KEYS_TAG); - else if (!strncmp (start, ROUTE_TAG, strlen (ROUTE_TAG))) - name = start + strlen (ROUTE_TAG); - else if (!strncmp (start, ROUTE6_TAG, strlen (ROUTE6_TAG))) - name = start + strlen (ROUTE6_TAG); +#define MATCH_TAG_AND_RETURN(name, TAG) \ + G_STMT_START { \ + if (strncmp (name, TAG, STRLEN (TAG)) == 0) { \ + name += STRLEN (TAG); \ + if (name[0] == '\0') \ + return NULL; \ + else \ + return name; \ + } \ + } G_STMT_END + + MATCH_TAG_AND_RETURN (name, IFCFG_TAG); + if (!only_ifcfg) { + MATCH_TAG_AND_RETURN (name, KEYS_TAG); + MATCH_TAG_AND_RETURN (name, ROUTE_TAG); + MATCH_TAG_AND_RETURN (name, ROUTE6_TAG); } - return name; + return NULL; } /* Used to get any ifcfg/extra file path from any other ifcfg/extra path |