diff options
author | NeilBrown <neilb@suse.com> | 2016-12-06 12:44:12 -0500 |
---|---|---|
committer | Steve Dickson <steved@redhat.com> | 2016-12-20 13:29:04 -0500 |
commit | 2d230e3e762e9ff543b4d037696a79ff9c328dcf (patch) | |
tree | 95e95bf163c3bc106b102c1507ecd3ee0db9ba94 /support | |
parent | 0cc5c137baad73613441b7475f1e0873e2a5dcc3 (diff) | |
download | nfs-utils-2d230e3e762e9ff543b4d037696a79ff9c328dcf.tar.gz nfs-utils-2d230e3e762e9ff543b4d037696a79ff9c328dcf.tar.xz nfs-utils-2d230e3e762e9ff543b4d037696a79ff9c328dcf.zip |
conffile: add bool support
conf_get_bool() interprets various strings as 'true' or 'false'.
If no suitable value is found, the default is returned.
Signed-off-by: NeilBrown <neilb@suse.com>
Signed-off-by: Steve Dickson <steved@redhat.com>
Diffstat (limited to 'support')
-rw-r--r-- | support/include/conffile.h | 2 | ||||
-rw-r--r-- | support/nfs/conffile.c | 32 |
2 files changed, 34 insertions, 0 deletions
diff --git a/support/include/conffile.h b/support/include/conffile.h index a6d5846..3fe3a78 100644 --- a/support/include/conffile.h +++ b/support/include/conffile.h @@ -36,6 +36,7 @@ #include <sys/queue.h> #include <ctype.h> #include <stdint.h> +#include <stdbool.h> struct conf_list_node { TAILQ_ENTRY(conf_list_node) link; @@ -57,6 +58,7 @@ extern struct sockaddr *conf_get_address(char *, char *); extern struct conf_list *conf_get_list(char *, char *); extern struct conf_list *conf_get_tag_list(char *, char *); extern int conf_get_num(char *, char *, int); +extern _Bool conf_get_bool(char *, char *, _Bool); extern char *conf_get_str(char *, char *); extern char *conf_get_section(char *, char *, char *); extern void conf_init(void); diff --git a/support/nfs/conffile.c b/support/nfs/conffile.c index 6b94ec0..609d782 100644 --- a/support/nfs/conffile.c +++ b/support/nfs/conffile.c @@ -446,6 +446,38 @@ conf_get_num(char *section, char *tag, int def) return def; } +/* + * Return the Boolean value denoted by TAG in section SECTION, or DEF + * if that tags does not exist. + * FALSE is returned for case-insensitve comparisons with 0, f, false, n, no, off + * TRUE is returned for 1, t, true, y, yes, on + * A failure to match one of these results in DEF + */ +_Bool +conf_get_bool(char *section, char *tag, _Bool def) +{ + char *value = conf_get_str(section, tag); + + if (!value) + return def; + if (strcasecmp(value, "1") == 0 || + strcasecmp(value, "t") == 0 || + strcasecmp(value, "true") == 0 || + strcasecmp(value, "y") == 0 || + strcasecmp(value, "yes") == 0 || + strcasecmp(value, "on") == 0) + return true; + + if (strcasecmp(value, "0") == 0 || + strcasecmp(value, "f") == 0 || + strcasecmp(value, "false") == 0 || + strcasecmp(value, "n") == 0 || + strcasecmp(value, "no") == 0 || + strcasecmp(value, "off") == 0) + return false; + return def; +} + /* Validate X according to the range denoted by TAG in section SECTION. */ int conf_match_num(char *section, char *tag, int x) |