summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-09-12 16:04:51 +0200
committerThomas Haller <thaller@redhat.com>2018-09-21 10:39:30 +0200
commit95b006c244978fecec9463690477e8b64f743202 (patch)
tree2032c536143fe2254fe9ae119b011168849c566a
parent20a7e489eebe897ef531b52c0dbaed3d87e39c40 (diff)
downloadNetworkManager-95b006c244978fecec9463690477e8b64f743202.tar.gz
dns: refactor create_resolv_conf() to use GString for constructing content
-rw-r--r--src/dns/nm-dns-manager.c53
1 files changed, 23 insertions, 30 deletions
diff --git a/src/dns/nm-dns-manager.c b/src/dns/nm-dns-manager.c
index 6a59b41c7b..33a414831f 100644
--- a/src/dns/nm-dns-manager.c
+++ b/src/dns/nm-dns-manager.c
@@ -586,49 +586,42 @@ create_resolv_conf (char **searches,
char **nameservers,
char **options)
{
- gs_free char *searches_str = NULL;
- gs_free char *nameservers_str = NULL;
- gs_free char *options_str = NULL;
- char *tmp_str;
GString *str;
- int i;
+ gsize i;
- if (searches) {
- tmp_str = g_strjoinv (" ", searches);
- searches_str = g_strconcat ("search ", tmp_str, "\n", NULL);
- g_free (tmp_str);
- }
+ str = g_string_new_len ("# Generated by NetworkManager\n", 245);
- if (options) {
- tmp_str = g_strjoinv (" ", options);
- options_str = g_strconcat ("options ", tmp_str, "\n", NULL);
- g_free (tmp_str);
+ if (searches && searches[0]) {
+ g_string_append (str, "search");
+ for (i = 0; searches[i]; i++) {
+ g_string_append_c (str, ' ');
+ g_string_append (str, searches[i]);
+ }
+ g_string_append_c (str, '\n');
}
- if (nameservers) {
- int num = g_strv_length (nameservers);
-
- str = g_string_new ("");
- for (i = 0; i < num; i++) {
+ if (nameservers && nameservers[0]) {
+ for (i = 0; nameservers[i]; i++) {
if (i == 3) {
- g_string_append (str, "# ");
- g_string_append (str, "NOTE: the libc resolver may not support more than 3 nameservers.");
- g_string_append (str, "\n# ");
- g_string_append (str, "The nameservers listed below may not be recognized.");
- g_string_append_c (str, '\n');
+ g_string_append (str, "# NOTE: the libc resolver may not support more than 3 nameservers.\n");
+ g_string_append (str, "# The nameservers listed below may not be recognized.\n");
}
-
g_string_append (str, "nameserver ");
g_string_append (str, nameservers[i]);
g_string_append_c (str, '\n');
}
- nameservers_str = g_string_free (str, FALSE);
}
- return g_strdup_printf ("# Generated by NetworkManager\n%s%s%s",
- searches_str ?: "",
- nameservers_str ?: "",
- options_str ?: "");
+ if (options && options[0]) {
+ g_string_append (str, "options");
+ for (i = 0; options[i]; i++) {
+ g_string_append_c (str, ' ');
+ g_string_append (str, options[i]);
+ }
+ g_string_append_c (str, '\n');
+ }
+
+ return g_string_free (str, FALSE);
}
static gboolean