aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2014-07-16 01:42:54 -0500
committerCharles Kerr <charles.kerr@canonical.com>2014-07-16 01:42:54 -0500
commit53bf4a35a2fb23a97756fd758ad7de84a1565688 (patch)
tree6463cefb4e223de812e87de2cb1b9a05d9f6b75b
parentb2fe95c6d86114b2681763df845a2817ee0fe234 (diff)
downloadayatana-indicator-session-53bf4a35a2fb23a97756fd758ad7de84a1565688.tar.gz
ayatana-indicator-session-53bf4a35a2fb23a97756fd758ad7de84a1565688.tar.bz2
ayatana-indicator-session-53bf4a35a2fb23a97756fd758ad7de84a1565688.zip
make the os-release parser slightly more readable.
-rw-r--r--src/service.c47
1 files changed, 22 insertions, 25 deletions
diff --git a/src/service.c b/src/service.c
index faff4d4..4c32f8b 100644
--- a/src/service.c
+++ b/src/service.c
@@ -339,51 +339,48 @@ get_os_release (void)
if ((io = g_io_channel_new_file ("/etc/os-release", "r", NULL)))
{
- GString * gstr = g_string_new (NULL);
+ GString * key = g_string_new (NULL);
for (;;)
{
- char *in, *out;
- const char *val;
GIOStatus status;
+ char *in;
+ GString * val;
+ gsize i;
/* read a line */
- status = g_io_channel_read_line_string (io, gstr, NULL, NULL);
+ status = g_io_channel_read_line_string (io, key, NULL, NULL);
if (status == G_IO_STATUS_EOF)
break;
/* ignore blank lines & comments */
- if (!gstr->len || gstr->str[0]=='#')
+ if (!key->len || key->str[0]=='#')
continue;
- /* split into name=value */
- in = strchr(gstr->str, '=');
+ /* split into key=value */
+ in = strchr(key->str, '=');
if (!in)
continue;
*in++ = '\0';
- /* remove quotes and newline; un-escape */
- val = out = in;
- while (*in)
+ /* remove quotes and newlines; unescape escape sequences */
+ val = g_string_new(in);
+ for (i=0; i<val->len; )
{
- if ((*in=='\'') || (*in=='"') || (*in=='\n')) /* skip chars */
- {
- ++in;
- }
- else
- {
- if ((*in=='\\') && !*++in) /* handle an escaped char */
- break;
-
- *out++ = *in++;
- }
+ if (val->str[i]=='\\' && ((i+1)<val->len)) /* unescape */
+ g_string_erase (val, i++, 1);
+
+ else if ((val->str[i]=='\'') || (val->str[i]=='"') || (val->str[i]=='\n')) /* skip */
+ g_string_erase (val, i, 1);
+
+ else /* keep */
+ i++;
}
- *out = '\0'; /* zero terminate */
-
- g_hash_table_insert (hash, g_strdup(gstr->str), g_strdup(val));
+
+ g_hash_table_insert (hash, g_strdup(key->str), g_string_free(val,FALSE));
}
- g_string_free(gstr, TRUE);
+ g_string_free(key, TRUE);
g_io_channel_unref(io);
}