summaryrefslogtreecommitdiff
path: root/lib/igt_sysfs.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2016-07-22 18:03:30 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2016-07-22 18:06:46 +0100
commite3abb2001b9bb9faa2b7217d615daf60c94f42a7 (patch)
treed3253d9295be4b23afa6c6925b9646640adeeacf /lib/igt_sysfs.c
parentb64d10cd9d9e9638acebae5e1199bd1be52aed39 (diff)
lib/sysfs: Provide scanf/printf wrappers
In order to avoid having to build and parse whole strings, use the FILE stream interface. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'lib/igt_sysfs.c')
-rw-r--r--lib/igt_sysfs.c64
1 files changed, 55 insertions, 9 deletions
diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index 633aeab0..f20a7540 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -26,6 +26,7 @@
#include <sys/stat.h>
#include <sys/mount.h>
#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
@@ -219,22 +220,67 @@ out:
return buf;
}
+int igt_sysfs_scanf(int dir, const char *attr, const char *fmt, ...)
+{
+ FILE *file;
+ int fd;
+ int ret = -1;
+
+ fd = openat(dir, attr, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ file = fdopen(fd, "r");
+ if (file) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = vfscanf(file, fmt, ap);
+ va_end(ap);
+
+ fclose(file);
+ }
+ close(fd);
+
+ return ret;
+}
+
+int igt_sysfs_printf(int dir, const char *attr, const char *fmt, ...)
+{
+ FILE *file;
+ int fd;
+ int ret = -1;
+
+ fd = openat(dir, attr, O_WRONLY);
+ if (fd < 0)
+ return -1;
+
+ file = fdopen(fd, "w");
+ if (file) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = vfprintf(file, fmt, ap);
+ va_end(ap);
+
+ fclose(file);
+ }
+ close(fd);
+
+ return ret;
+}
+
bool igt_sysfs_get_boolean(int dir, const char *attr)
{
- char *str;
- bool result;
+ int result;
- str = igt_sysfs_get(dir, attr);
- result = str && atoi(str) > 0;
- free(str);
+ if (igt_sysfs_scanf(dir, attr, "%d", &result) != 1)
+ return false;
return result;
}
bool igt_sysfs_set_boolean(int dir, const char *attr, bool value)
{
- char buf[8];
-
- sprintf(buf, "%d", value);
- return igt_sysfs_set(dir, attr, buf);
+ return igt_sysfs_printf(dir, attr, "%d", value) == 1;
}