void test_get (void) { char **env = NULL; size_t len = 0; const char *ret; TEST_FUNCTION ("environ_get"); len = 0; env = nih_str_array_new (NULL); /* Check that an empty table always returns NULL. */ TEST_FEATURE ("with empty table"); ret = environ_get (env, "FOO"); TEST_EQ_P (ret, NULL); assert (nih_str_array_add (&env, NULL, &len, "FOOLISH=no")); assert (nih_str_array_add (&env, NULL, &len, "BAR=BAZ")); /* Check that a key that is present is returned. */ TEST_FEATURE ("with key to be found"); ret = environ_get (env, "BAR"); TEST_EQ_STR (ret, "BAZ"); /* Check that a key that doesn't exist returns NULL. */ TEST_FEATURE ("with key not found"); ret = environ_get (env, "MEEP"); TEST_EQ_P (ret, NULL); /* Check that the key is not prefix-matched. */ TEST_FEATURE ("with key that is prefix of another"); ret = environ_get (env, "FOO"); TEST_EQ_P (ret, NULL); nih_free (env); }
char *path_command(t_context *context, size_t n) { char *pwd; char *home; char *trimmed; pwd = environ_get(context, VAR_PWD); home = environ_get(context, VAR_HOME); if (!pwd) return (ft_strdup(NO_PWD)); pwd = (home ? replace_home(pwd, home) : ft_strdup(pwd)); if (!n) return (pwd); trimmed = n_last_directories(pwd, n); free(pwd); return (trimmed); }
static void fetch_variable(t_context *context, t_token *token) { char *var_value; token->type = CHARS; if (!ft_strcmp(token->lexeme, "?")) var_value = ft_itoa(context->last_status); else { var_value = variables_get(context->vars, token->lexeme); if (!var_value) var_value = environ_get(context, token->lexeme); var_value = (var_value ? ft_strdup(var_value) : ft_strnew(0)); } free(token->lexeme); token->lexeme = var_value; }
void list_commands(t_context *context) { char *path; char **dirs; size_t i; commands_delete(context->commands); path = environ_get(context, VAR_PATH); if (path) { dirs = ft_strsplit(path, PATH_DIR_SEPARATOR); i = 0; while (dirs[i]) { list_commands_in_dir(context->commands, dirs[i]); ++i; } char_array_free(dirs); } }