aboutsummaryrefslogtreecommitdiff
blob: 805b9e6f138e233f679ed162e4c0ff5191a18c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * environment.c
 *
 * Environment utility functions.
 *
 * Copyright 1999-2012 Gentoo Foundation
 * Licensed under the GPL-2
 */

#include "headers.h"
#include "sbutil.h"

static const char * const true_values[] = {
	"1", "true", "yes", NULL,
};

static const char * const false_values[] = {
	"0", "false", "no", NULL,
};

static bool val_is_in(const char *val, const char * const values[])
{
	size_t i = 0;

	while (values[i])
		if (!strcasecmp(val, values[i++]))
			return true;

	return false;
}

static bool env_is_in(const char *env, const char * const values[], bool *set)
{
	const char *val;

	if (unlikely(!env))
		return (*set = false);

	val = getenv(env);
	*set = (val != NULL);
	if (unlikely(!*set))
		return false;

	return val_is_in(val, values);
}

bool is_val_on(const char *val)
{
	return val_is_in(val, true_values);
}
bool is_val_off(const char *val)
{
	return val_is_in(val, false_values);
}

bool is_env_set_on(const char *env, bool *set)
{
	return env_is_in(env, true_values, set);
}
bool is_env_on(const char *env)
{
	bool set;
	return is_env_set_on(env, &set);
}

bool is_env_set_off(const char *env, bool *set)
{
	return env_is_in(env, false_values, set);
}
bool is_env_off(const char *env)
{
	bool set;
	return is_env_set_off(env, &set);
}