Skip to content

Commit 3eb2adb

Browse files
mdouchapevik
authored andcommitted
lib: Add helper function for reading boolean sysconf files
Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Martin Doucha <[email protected]>
1 parent 94892d6 commit 3eb2adb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

include/tst_sys_conf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ int tst_sys_conf_save(const struct tst_path_val *conf);
2828
void tst_sys_conf_restore(int verbose);
2929
void tst_sys_conf_dump(void);
3030

31+
int tst_read_bool_sys_param(const char *filename);
32+
3133
#endif

lib/tst_sys_conf.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdio.h>
88
#include <unistd.h>
99
#include <string.h>
10+
#include <ctype.h>
1011

1112
#define TST_NO_DEFAULT_MAIN
1213
#include "tst_test.h"
@@ -145,3 +146,37 @@ void tst_sys_conf_restore(int verbose)
145146
}
146147
}
147148

149+
int tst_read_bool_sys_param(const char *filename)
150+
{
151+
char buf[PATH_MAX];
152+
int i, fd, ret;
153+
154+
fd = open(filename, O_RDONLY);
155+
156+
if (fd < 0)
157+
return -1;
158+
159+
ret = read(fd, buf, PATH_MAX - 1);
160+
SAFE_CLOSE(fd);
161+
162+
if (ret < 1)
163+
return -1;
164+
165+
buf[ret] = '\0';
166+
167+
for (i = 0; buf[i] && !isspace(buf[i]); i++)
168+
;
169+
170+
buf[i] = '\0';
171+
172+
if (isdigit(buf[0])) {
173+
tst_parse_int(buf, &ret, INT_MIN, INT_MAX);
174+
return ret;
175+
}
176+
177+
if (!strcasecmp(buf, "N"))
178+
return 0;
179+
180+
/* Assume that any other value than 0 or N means the param is enabled */
181+
return 1;
182+
}

0 commit comments

Comments
 (0)