summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2021-03-16 10:57:35 +0100
committerBastien Nocera <hadess@hadess.net>2021-03-16 11:05:03 +0100
commit31e31d5250b59cb91ac4580572c67b321dc7269d (patch)
treeeeea035d35f1ea5c417120237ea10b21efc5f9d5
parente186dac0c69edbb53b1456a25cb076e20eb576d6 (diff)
downloadlibgudev-31e31d5250b59cb91ac4580572c67b321dc7269d.tar.gz
gudev: Ignore trailing linefeeds in sysfs attr
sysfs attributes which contain text will almost always contain a '\n' at the end so that they can be cat'ed cleanly from the shell. Fix g_udev_device_get_sysfs_attr_as_boolean to take this into account by cutting off the string before the first '\n' if it is present. Closes: #7
-rw-r--r--gudev/gudevdevice.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/gudev/gudevdevice.c b/gudev/gudevdevice.c
index e14f367..27ffc52 100644
--- a/gudev/gudevdevice.c
+++ b/gudev/gudevdevice.c
@@ -896,6 +896,17 @@ out:
return result;
}
+static char *
+truncate_at_linefeed (const char *value)
+{
+ const char *p;
+
+ p = strchr (value, '\n');
+ if (!p)
+ return NULL;
+ return g_strndup (value, p - value);
+}
+
/**
* g_udev_device_get_sysfs_attr_as_boolean:
* @device: A #GUdevDevice.
@@ -916,16 +927,20 @@ g_udev_device_get_sysfs_attr_as_boolean (GUdevDevice *device,
const gchar *name)
{
gboolean result;
- const gchar *s;
+ const gchar *raw;
+ g_autofree char *truncated = NULL;
+ const char *s;
g_return_val_if_fail (G_UDEV_IS_DEVICE (device), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
result = FALSE;
- s = g_udev_device_get_sysfs_attr (device, name);
- if (s == NULL)
+ raw = g_udev_device_get_sysfs_attr (device, name);
+ if (raw == NULL)
goto out;
+ truncated = truncate_at_linefeed (raw);
+ s = truncated ?: raw;
if (strcmp (s, "1") == 0 ||
g_ascii_strcasecmp (s, "true") == 0 ||
g_ascii_strcasecmp (s, "y") == 0) {