ifcfg-rh: let reader preserve the last line even without newline at end of file

This commit is contained in:
Thomas Haller 2016-11-01 15:50:31 +01:00
parent 82ccec22ca
commit eb307bbbb6
3 changed files with 21 additions and 1 deletions

View file

@ -630,6 +630,8 @@ svOpenFileInternal (const char *name, gboolean create, GError **error)
/* we'd use g_strsplit() here, but we want a list, not an array */
for (p = arena; (q = strchr (p, '\n')) != NULL; p = q + 1)
s->lineList = g_list_append (s->lineList, g_strndup (p, q - p));
if (p[0])
s->lineList = g_list_append (s->lineList, g_strdup (p));
g_free (arena);
/* closefd is set if we opened the file read-only, so go ahead and

View file

@ -16,4 +16,4 @@ CIPHER_GROUP="TKIP CCMP WEP40 WEP104"
KEY_MGMT=WPA-PSK
WPA_ALLOW_WPA=yes
WPA_ALLOW_WPA2=yes
LAST_ENTRY=no-newline

View file

@ -2400,6 +2400,22 @@ test_read_wifi_leap_secret_flags (gconstpointer test_data)
g_object_unref (connection);
}
static void
test_ifcfg_no_trailing_newline (void)
{
gs_free_error GError *error = NULL;
shvarFile *sv;
gs_free char *value = NULL;
sv = svOpenFile (TEST_IFCFG_DIR "/network-scripts/ifcfg-test-wifi-wpa-psk", &error);
nmtst_assert_success (sv, error);
value = svGetValueString (sv, "LAST_ENTRY");
g_assert_cmpstr (value, ==, "no-newline");
svCloseFile (sv);
}
static void
test_read_wifi_wpa_psk (void)
{
@ -9446,6 +9462,8 @@ int main (int argc, char **argv)
g_test_add_data_func (TPATH "wwan/write-gsm", GUINT_TO_POINTER (TRUE), test_write_mobile_broadband);
g_test_add_data_func (TPATH "wwan/write-cdma", GUINT_TO_POINTER (FALSE), test_write_mobile_broadband);
g_test_add_func (TPATH "no-trailing-newline", test_ifcfg_no_trailing_newline);
g_test_add_func (TPATH "utils/name", test_utils_name);
g_test_add_func (TPATH "utils/path", test_utils_path);
g_test_add_func (TPATH "utils/ignore", test_utils_ignore);