diff options
author | Mattias Andrée <maandree@kth.se> | 2024-05-05 10:24:40 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-05-05 10:24:40 +0200 |
commit | 3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e (patch) | |
tree | b7717583b99f29028c85c3423cf43b104dfa8943 /libexec_getenv.c | |
download | libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.gz libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.bz2 libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.xz |
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libexec_getenv.c')
-rw-r--r-- | libexec_getenv.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/libexec_getenv.c b/libexec_getenv.c new file mode 100644 index 0000000..5099b28 --- /dev/null +++ b/libexec_getenv.c @@ -0,0 +1,122 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +const char * +libexec_getenv(struct libexec_command *cmd, const char *name) +{ + char **env; + size_t i, len; + + if (!cmd || !name) { + errno = EINVAL; + return NULL; + } + + if (strchr(name, '=')) + return NULL; + + env = cmd->environ ? cmd->environ : environ; + len = strlen(name); + + for (i = 0; env[i]; i++) + if (!strncmp(env[i], name, len) && env[i][len] == '=') + return &env[i][len + 1]; + + return NULL; +} + + +#else + + +int +main(void) +{ + struct libexec_command cmd, ref; + char **env; + + libexec_init_command(&cmd); + memcpy(&ref, &cmd, sizeof(cmd)); + + env = environ; + environ = calloc(12, sizeof(*environ)); + ASSERT_NOT_NULL(environ); + environ[0] = strdup("AA=aa"); + environ[1] = strdup("A=a0"); + environ[2] = strdup("B=b0"); + environ[3] = strdup("A=a1"); + environ[4] = strdup("B=b1"); + environ[5] = strdup("A=a2"); + environ[6] = strdup("B=b2"); + environ[7] = strdup("A=a3"); + environ[8] = strdup("B=b3"); + environ[9] = strdup("BB=bb"); + environ[10] = strdup("X=Y=Z"); + ASSERT_NOT_NULL(environ[0]); + ASSERT_NOT_NULL(environ[1]); + ASSERT_NOT_NULL(environ[2]); + ASSERT_NOT_NULL(environ[3]); + ASSERT_NOT_NULL(environ[4]); + ASSERT_NOT_NULL(environ[5]); + ASSERT_NOT_NULL(environ[6]); + ASSERT_NOT_NULL(environ[7]); + ASSERT_NOT_NULL(environ[8]); + ASSERT_NOT_NULL(environ[9]); + ASSERT_NOT_NULL(environ[10]); + ASSERT_IS_NULL(environ[11]); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(&cmd, "PATH")); + ASSERT_IS_TRUE(errno == 0); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(NULL, "x")); + ASSERT_EQ_INT(errno, EINVAL); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(&cmd, "x")); + ASSERT_IS_TRUE(errno == 0); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(&cmd, NULL)); + ASSERT_EQ_INT(errno, EINVAL); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(&cmd, "X=Y")); + ASSERT_IS_TRUE(errno == 0 || errno == EINVAL); + + errno = 0; + ASSERT_IS_NULL(libexec_getenv(&cmd, "")); + ASSERT_IS_TRUE(errno == 0); + + ASSERT_EQ_STR(libexec_getenv(&cmd, "X"), "Y=Z"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "AA"), "aa"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "BB"), "bb"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "A"), "a0"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "B"), "b0"); + + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + libexec_destroy_command(&cmd); + + ASSERT_IS_NULL(libexec_getenv(&cmd, "PATH")); + ASSERT_IS_NULL(libexec_getenv(NULL, "x")); + ASSERT_IS_NULL(libexec_getenv(&cmd, "x")); + ASSERT_IS_NULL(libexec_getenv(&cmd, NULL)); + ASSERT_IS_NULL(libexec_getenv(&cmd, "X=Y")); + ASSERT_IS_NULL(libexec_getenv(&cmd, "")); + ASSERT_EQ_STR(libexec_getenv(&cmd, "X"), "Y=Z"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "AA"), "aa"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "BB"), "bb"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "A"), "a0"); + ASSERT_EQ_STR(libexec_getenv(&cmd, "B"), "b0"); + + libexec_set_environ(&cmd, environ); + environ = env; + + return 0; +} + + +#endif |