xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
Commit 1a6c8eb83fc16ab0284af91409e748a41c957e4a

Add get_one_of_def() function to utils/env.c
Author: xaizek
Author date (UTC): 2012-10-22 13:01
Committer name: xaizek
Committer date (UTC): 2012-10-22 13:01
Parent(s): 6a19ad1673494d577ca310d251976e14724ecb63
Signing key:
Tree: 0eb767e3e8a56b78878cb524c961c44ebb138b03
File Lines added Lines deleted
src/utils/env.c 28 3
src/utils/env.h 7 3
tests/Makefile 2 2
tests/env/get_one_of_def.c 72 0
tests/env/test.c 20 0
tests/filetype/find_program.c 2 1
tests/filetype/test.c 2 1
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 : */
File tests/env/test.c added (mode: 100644) (index 000000000..b7cc4124d)
1 #include "seatest.h"
2
3 #include "../../src/utils/env.h"
4
5 void get_one_of_def_tests(void);
6
7 static void
8 all_tests(void)
9 {
10 get_one_of_def_tests();
11 }
12
13 int
14 main(int argc, char *argv[])
15 {
16 return run_tests(all_tests) == 0;
17 }
18
19 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
20 /* vim: set cinoptions+=t0 : */
File tests/filetype/find_program.c changed (mode: 100644) (index 6c45314db..493c2f9ae)
... ... find_program_tests(void)
42 42 test_fixture_end(); test_fixture_end();
43 43 } }
44 44
45 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab : */
45 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
46 /* vim: set cinoptions+=t0 : */
File tests/filetype/test.c changed (mode: 100644) (index 29fe5f0f9..edf0dae1a)
... ... main(int argc, char **argv)
44 44 return run_tests(all_tests) == 0; return run_tests(all_tests) == 0;
45 45 } }
46 46
47 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab : */
47 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
48 /* vim: set cinoptions+=t0 : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master