diff options
author | Thomas Haller <thaller@redhat.com> | 2016-04-25 15:13:18 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-04-25 15:28:49 +0200 |
commit | 9f6dd5afb7dfd33195f311b6b3a07f0461c68fa0 (patch) | |
tree | fabe56b1321feeafac392a94d2db4b755fefc6ee /src | |
parent | eb4e6945657e96ad19a90228c87c286561c66ede (diff) | |
download | NetworkManager-9f6dd5afb7dfd33195f311b6b3a07f0461c68fa0.tar.gz |
settings: refactor read_hostname_*()
(cherry picked from commit 316359d8b65030df2ac8b34be926bc752a96802c)
Diffstat (limited to 'src')
-rw-r--r-- | src/settings/nm-settings.c | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index a47ae6792a..7475eb5fae 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -484,27 +484,25 @@ get_plugin (NMSettings *self, guint32 capability) static gchar * read_hostname_gentoo (const char *path) { - gchar *contents = NULL, *result = NULL, *tmp; - gchar **all_lines = NULL; - guint line_num, i; + gs_free char *contents = NULL; + gs_strfreev char **all_lines = NULL; + const char *tmp; + guint i; if (!g_file_get_contents (path, &contents, NULL, NULL)) return NULL; + all_lines = g_strsplit (contents, "\n", 0); - line_num = g_strv_length (all_lines); - for (i = 0; i < line_num; i++) { + for (i = 0; all_lines[i]; i++) { g_strstrip (all_lines[i]); if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') continue; if (g_str_has_prefix (all_lines[i], "hostname=")) { tmp = &all_lines[i][NM_STRLEN ("hostname=")]; - result = g_shell_unquote (tmp, NULL); - break; + return g_shell_unquote (tmp, NULL); } } - g_strfreev (all_lines); - g_free (contents); - return result; + return NULL; } #endif @@ -512,15 +510,16 @@ read_hostname_gentoo (const char *path) static gchar * read_hostname_slackware (const char *path) { - gchar *contents = NULL, *result = NULL, *tmp; - gchar **all_lines = NULL; - guint line_num, i, j = 0; + gs_free char *contents = NULL; + gs_strfreev char **all_lines = NULL; + char *tmp; + guint i, j = 0; if (!g_file_get_contents (path, &contents, NULL, NULL)) return NULL; + all_lines = g_strsplit (contents, "\n", 0); - line_num = g_strv_length (all_lines); - for (i = 0; i < line_num; i++) { + for (i = 0; all_lines[i]; i++) { g_strstrip (all_lines[i]); if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') continue; @@ -534,12 +533,9 @@ read_hostname_slackware (const char *path) } j++; } - result = g_shell_unquote (tmp, NULL); - break; + return g_shell_unquote (tmp, NULL); } - g_strfreev (all_lines); - g_free (contents); - return result; + return NULL; } #endif |