File src/utils/env.c changed (mode: 100644) (index f85048215..87b5cc993) |
17 |
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
18 |
18 |
*/ |
*/ |
19 |
19 |
|
|
|
20 |
|
#include <stdarg.h> /* va_list va_start() va_arg() va_end() */ |
20 |
21 |
#ifdef _WIN32 |
#ifdef _WIN32 |
21 |
22 |
#include <stdio.h> /* sprintf() */ |
#include <stdio.h> /* sprintf() */ |
22 |
23 |
#include <string.h> /* strlen() */ |
#include <string.h> /* strlen() */ |
|
26 |
27 |
#include "env.h" |
#include "env.h" |
27 |
28 |
|
|
28 |
29 |
const char * |
const char * |
29 |
|
env_get(const char *name) |
|
|
30 |
|
env_get(const char name[]) |
30 |
31 |
{ |
{ |
31 |
32 |
return getenv(name); |
return getenv(name); |
32 |
33 |
} |
} |
|
... |
... |
env_get_def(const char name[], const char def[]) |
42 |
43 |
return result; |
return result; |
43 |
44 |
} |
} |
44 |
45 |
|
|
|
46 |
|
const char * |
|
47 |
|
env_get_one_of_def(const char def[], ...) |
|
48 |
|
{ |
|
49 |
|
va_list ap; |
|
50 |
|
const char *env_name; |
|
51 |
|
const char *result = def; |
|
52 |
|
|
|
53 |
|
va_start(ap, def); |
|
54 |
|
|
|
55 |
|
while((env_name = va_arg(ap, const char *)) != NULL) |
|
56 |
|
{ |
|
57 |
|
const char *val = env_get_def(env_name, NULL); |
|
58 |
|
if(val != NULL) |
|
59 |
|
{ |
|
60 |
|
result = val; |
|
61 |
|
break; |
|
62 |
|
} |
|
63 |
|
} |
|
64 |
|
|
|
65 |
|
va_end(ap); |
|
66 |
|
|
|
67 |
|
return result; |
|
68 |
|
} |
|
69 |
|
|
45 |
70 |
void |
void |
46 |
|
env_set(const char *name, const char *value) |
|
|
71 |
|
env_set(const char name[], const char value[]) |
47 |
72 |
{ |
{ |
48 |
73 |
#ifndef _WIN32 |
#ifndef _WIN32 |
49 |
74 |
setenv(name, value, 1); |
setenv(name, value, 1); |
|
... |
... |
env_set(const char *name, const char *value) |
55 |
80 |
} |
} |
56 |
81 |
|
|
57 |
82 |
void |
void |
58 |
|
env_remove(const char *name) |
|
|
83 |
|
env_remove(const char name[]) |
59 |
84 |
{ |
{ |
60 |
85 |
#ifndef _WIN32 |
#ifndef _WIN32 |
61 |
86 |
unsetenv(name); |
unsetenv(name); |
File src/utils/env.h changed (mode: 100644) (index 6ee9735ea..92ac4411b) |
23 |
23 |
/* Environment variables related functions */ |
/* Environment variables related functions */ |
24 |
24 |
|
|
25 |
25 |
/* Returns environment variable value or NULL if it doesn't exist */ |
/* Returns environment variable value or NULL if it doesn't exist */ |
26 |
|
const char * env_get(const char *name); |
|
|
26 |
|
const char * env_get(const char name[]); |
27 |
27 |
/* Returns environment variable value or def if it doesn't exist or empty */ |
/* Returns environment variable value or def if it doesn't exist or empty */ |
28 |
28 |
const char * env_get_def(const char name[], const char def[]); |
const char * env_get_def(const char name[], const char def[]); |
|
29 |
|
/* Returns value of the first environment variable found or def if none of |
|
30 |
|
* specified environment variables are found (or empty). The last name of |
|
31 |
|
* environment variable have to be followed by NULL. */ |
|
32 |
|
const char * env_get_one_of_def(const char def[], ...); |
29 |
33 |
/* Sets new value of environment variable or creates it if it doesn't exist */ |
/* Sets new value of environment variable or creates it if it doesn't exist */ |
30 |
|
void env_set(const char *name, const char *value); |
|
|
34 |
|
void env_set(const char name[], const char value[]); |
31 |
35 |
/* Removes environment variable */ |
/* Removes environment variable */ |
32 |
|
void env_remove(const char *name); |
|
|
36 |
|
void env_remove(const char name[]); |
33 |
37 |
|
|
34 |
38 |
#endif |
#endif |
35 |
39 |
|
|
File tests/Makefile changed (mode: 100644) (index 4b0f2db05..c99426f12) |
1 |
|
dirs := commands column_view completion filetype keys misc options parsing undo |
|
2 |
|
dirs += variables viewcolumns_parser |
|
|
1 |
|
dirs := commands column_view completion env filetype keys misc options parsing |
|
2 |
|
dirs += undo variables viewcolumns_parser |
3 |
3 |
# generate names for build sub-targets (one for each directory) |
# generate names for build sub-targets (one for each directory) |
4 |
4 |
dirs_b := $(addsuffix _b, $(dirs)) |
dirs_b := $(addsuffix _b, $(dirs)) |
5 |
5 |
# generate names for clean sub-targets (one for each directory) |
# generate names for clean sub-targets (one for each directory) |
File tests/env/get_one_of_def.c added (mode: 100644) (index 000000000..bfdaa0059) |
|
1 |
|
#include <stdlib.h> |
|
2 |
|
#include <string.h> |
|
3 |
|
|
|
4 |
|
#include "seatest.h" |
|
5 |
|
|
|
6 |
|
#include "../../src/utils/env.h" |
|
7 |
|
|
|
8 |
|
#define DEFAULT_VAL "default" |
|
9 |
|
#define VAR_A "VAR_A" |
|
10 |
|
#define VAR_A_VAL "VAR_A_VAL" |
|
11 |
|
#define VAR_B "VAR_B" |
|
12 |
|
#define VAR_B_VAL "VAR_B_VAL" |
|
13 |
|
|
|
14 |
|
static void |
|
15 |
|
setup(void) |
|
16 |
|
{ |
|
17 |
|
env_remove(VAR_A); |
|
18 |
|
env_remove(VAR_B); |
|
19 |
|
} |
|
20 |
|
|
|
21 |
|
static void |
|
22 |
|
test_empty_list_returns_def(void) |
|
23 |
|
{ |
|
24 |
|
const char *result; |
|
25 |
|
result = env_get_one_of_def(DEFAULT_VAL, NULL); |
|
26 |
|
assert_string_equal(DEFAULT_VAL, result); |
|
27 |
|
} |
|
28 |
|
|
|
29 |
|
static void |
|
30 |
|
test_none_exist_returns_def(void) |
|
31 |
|
{ |
|
32 |
|
const char *result; |
|
33 |
|
result = env_get_one_of_def(DEFAULT_VAL, VAR_A, VAR_B, NULL); |
|
34 |
|
assert_string_equal(DEFAULT_VAL, result); |
|
35 |
|
} |
|
36 |
|
|
|
37 |
|
static void |
|
38 |
|
test_last_exists_returns_it(void) |
|
39 |
|
{ |
|
40 |
|
const char *result; |
|
41 |
|
env_set(VAR_B, VAR_B_VAL); |
|
42 |
|
result = env_get_one_of_def(DEFAULT_VAL, VAR_A, VAR_B, NULL); |
|
43 |
|
assert_string_equal(VAR_B_VAL, result); |
|
44 |
|
} |
|
45 |
|
|
|
46 |
|
static void |
|
47 |
|
test_all_exist_returns_first(void) |
|
48 |
|
{ |
|
49 |
|
const char *result; |
|
50 |
|
env_set(VAR_A, VAR_A_VAL); |
|
51 |
|
env_set(VAR_B, VAR_B_VAL); |
|
52 |
|
result = env_get_one_of_def(DEFAULT_VAL, VAR_A, VAR_B, NULL); |
|
53 |
|
assert_string_equal(VAR_A_VAL, result); |
|
54 |
|
} |
|
55 |
|
|
|
56 |
|
void |
|
57 |
|
get_one_of_def_tests(void) |
|
58 |
|
{ |
|
59 |
|
test_fixture_start(); |
|
60 |
|
|
|
61 |
|
fixture_setup(setup); |
|
62 |
|
|
|
63 |
|
run_test(test_empty_list_returns_def); |
|
64 |
|
run_test(test_none_exist_returns_def); |
|
65 |
|
run_test(test_last_exists_returns_it); |
|
66 |
|
run_test(test_all_exist_returns_first); |
|
67 |
|
|
|
68 |
|
test_fixture_end(); |
|
69 |
|
} |
|
70 |
|
|
|
71 |
|
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ |
|
72 |
|
/* vim: set cinoptions+=t0 : */ |