[LTP] [PATCH 08/10] lib: Add helper function for reading boolean sysconf files

Martin Doucha mdoucha@suse.cz
Tue Jan 21 17:44:22 CET 2025


Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 include/tst_sys_conf.h |  2 ++
 lib/tst_sys_conf.c     | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/include/tst_sys_conf.h b/include/tst_sys_conf.h
index 4c85767be..6bbf39672 100644
--- a/include/tst_sys_conf.h
+++ b/include/tst_sys_conf.h
@@ -28,4 +28,6 @@ int tst_sys_conf_save(const struct tst_path_val *conf);
 void tst_sys_conf_restore(int verbose);
 void tst_sys_conf_dump(void);
 
+int tst_read_bool_sys_param(const char *filename);
+
 #endif
diff --git a/lib/tst_sys_conf.c b/lib/tst_sys_conf.c
index c0981dcb1..91203ea9e 100644
--- a/lib/tst_sys_conf.c
+++ b/lib/tst_sys_conf.c
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <ctype.h>
 
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
@@ -145,3 +146,37 @@ void tst_sys_conf_restore(int verbose)
 	}
 }
 
+int tst_read_bool_sys_param(const char *filename)
+{
+	char buf[PATH_MAX];
+	int i, fd, ret;
+
+	fd = open(filename, O_RDONLY);
+
+	if (fd < 0)
+		return -1;
+
+	ret = read(fd, buf, PATH_MAX - 1);
+	SAFE_CLOSE(fd);
+
+	if (ret < 1)
+		return -1;
+
+	buf[ret] = '\0';
+
+	for (i = 0; buf[i] && !isspace(buf[i]); i++)
+		;
+
+	buf[i] = '\0';
+
+	if (isdigit(buf[0])) {
+		tst_parse_int(buf, &ret, INT_MIN, INT_MAX);
+		return ret;
+	}
+
+	if (!strcasecmp(buf, "N"))
+		return 0;
+
+	/* Assume that any other value than 0 or N means the param is enabled */
+	return 1;
+}
-- 
2.47.0



More information about the ltp mailing list