diff --git a/codes/Makefile b/codes/Makefile index e50f7a0..ef09954 100644 --- a/codes/Makefile +++ b/codes/Makefile @@ -41,7 +41,8 @@ UTIL_FILES = utils/do_exit.c \ utils/ld_chdir.c \ utils/ld_errno_file.c \ utils/ld_map_functions.c \ - utils/ld_map_functions2.c + utils/ld_map_functions2.c \ + utils/ft_qsort.c BUILTIN_FILES = builtins/builtin_cd.c \ builtins/builtin_echo.c \ builtins/builtin_exit.c \ diff --git a/codes/builtins/builtin_cd.c b/codes/builtins/builtin_cd.c index 9daad6d..1d48820 100644 --- a/codes/builtins/builtin_cd.c +++ b/codes/builtins/builtin_cd.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:33 by kyungjle #+# #+# */ -/* Updated: 2024/04/05 12:45:42 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 14:51:09 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int builtin_cd(char *args[], t_ld_map_env *env) } } else if ((ft_strchr(args[1], '/') != NULL && ld_chdir(args[1]) != TRUE) - || (ft_strchr(args[1], '/') == NULL && !chdir(args[1]))) + || (ft_strchr(args[1], '/') == NULL && chdir(args[1]))) return (EXIT_FAILURE); key_pwd = ft_strdup("PWD"); if (key_pwd == NULL) diff --git a/codes/builtins/builtin_env.c b/codes/builtins/builtin_env.c index edf30d0..0a5488d 100644 --- a/codes/builtins/builtin_env.c +++ b/codes/builtins/builtin_env.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:39 by kyungjle #+# #+# */ -/* Updated: 2024/04/05 13:00:50 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 14:39:38 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,10 @@ static int builtin_env_print(t_ld_map_env *env) { t_ld_map_node *node; - node = env->contents; + node = (env->contents)->next; while (node != NULL) { - if (ldpre_env_validate_key(node->key) == TRUE) - printf("%s=%s\n", node->key, node->value); + printf("%s=%s\n", node->key, node->value); node = node->next; } return (EXIT_SUCCESS); diff --git a/codes/builtins/builtin_export.c b/codes/builtins/builtin_export.c index eab2431..effb239 100644 --- a/codes/builtins/builtin_export.c +++ b/codes/builtins/builtin_export.c @@ -6,15 +6,17 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:43 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/05 14:05:37 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 15:19:43 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtin.h" #include "utils.h" -int builtin_export(char *args[], t_ld_map_env *env); -static int builtin_export_print(t_ld_map_env *env); +int builtin_export(char *args[], t_ld_map_env *env); +static int builtin_export_print(t_ld_map_env *env); +static t_bool cmp(const void *c1, const void *c2); + int builtin_export(char *args[], t_ld_map_env *env) { @@ -50,8 +52,32 @@ static int builtin_export_print(t_ld_map_env *env) char **envp; envp = ldpre_env_toenvp_f(env); + ft_qsort((void **)envp, 0, env->count - 1, cmp); for (int i = 0; envp[i] != NULL; i++) printf("%s\n", envp[i]); free_ft_split(envp); return (EXIT_SUCCESS); } + +static t_bool cmp(const void *c1, const void *c2) +{ + unsigned char *c1_ptr; + unsigned char *c2_ptr; + + if (c1 == c2) + return (FALSE); + c1_ptr = (unsigned char *) c1; + c2_ptr = (unsigned char *) c2; + while (*c1_ptr != '=' && *c2_ptr != '=') + { + if (*c1_ptr != *c2_ptr) + break ; + c1_ptr++; + c2_ptr++; + } + if (*c1_ptr == '=') + c1_ptr = (unsigned char *)"\0"; + if (*c2_ptr == '=') + c2_ptr = (unsigned char *)"\0"; + return (*c1_ptr < *c2_ptr); +} diff --git a/codes/loader/ldexec_env_exitcode.c b/codes/loader/ldexec_env_exitcode.c index 8cdf7b1..ea502cb 100644 --- a/codes/loader/ldexec_env_exitcode.c +++ b/codes/loader/ldexec_env_exitcode.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/21 13:44:12 by nicknamemoh #+# #+# */ -/* Updated: 2024/03/21 17:39:55 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 14:23:31 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,7 @@ char *ldexec_env_exitcode_fetch_f(t_ld_map_env *env) :param env: environment variable map :return: itoa'ed last exitcode -Fetches `$?` from map. `$?` is registered when previous execution finished, -so it might not exist. bash's default is 0, so we follow bash's behavior. +Fetches `$?` from map. `$?` is always at first node of env map. `$?` is not a valid key, so created special methods. user cannot access, modify, delete to this environment variable, since fetch/add/remove interface @@ -31,13 +30,10 @@ validates the key before doing action. char *ldexec_env_exitcode_fetch_f(t_ld_map_env *env) { char *ret; - t_ld_map_node **nodes; + t_ld_map_node *node; - nodes = ldpre_env_searchkey("?", env); - if (nodes == NULL) - ret = ft_itoa(0); - else - ret = ft_strdup(nodes[0]->value); + node = env->contents; + ret = ft_strdup(node->value); if (ret == NULL) do_exit("ldexec_env_exitcode_fetch_f.malloc"); return (ret); @@ -52,17 +48,14 @@ Update/Adds last status code(`$?`) to map */ void ldexec_env_exitcode_update(int code, t_ld_map_env *env) { - t_ld_map_node **nodes; t_ld_map_node *node; + t_ld_map_node *next; - nodes = ldpre_env_searchkey("?", env); - if (nodes != NULL) - { - nodes[1]->next = nodes[0]->next; - free(nodes[0]->key); - free(nodes[0]->value); - free(nodes[0]); - } + node = env->contents; + next = node->next; + free(node->key); + free(node->value); + free(node); node = malloc(1 * sizeof(t_ld_map_node)); if (node == NULL) do_exit("ldexec_env_exitcode_update.malloc"); @@ -73,5 +66,6 @@ void ldexec_env_exitcode_update(int code, t_ld_map_env *env) if (node->value == NULL) do_exit("ldexec_env_exitcode_update.malloc"); node->next = NULL; - ld_map_node_attach(env, node); + node->next = next; + env->contents = node; } diff --git a/codes/loader/ldpre_ast.c b/codes/loader/ldpre_ast.c index aa5fe57..bf4acec 100644 --- a/codes/loader/ldpre_ast.c +++ b/codes/loader/ldpre_ast.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 11:20:45 by kyungjle #+# #+# */ -/* Updated: 2024/04/05 12:54:20 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 15:18:01 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env, { case NODE_COMMAND: { + // TODO 커맨드 실행 구조 검토 + // 파이프일때 손자까지 만들 필요가 있나... t_ld_exec_nodes *node; t_bool free_flag; pid_t pid; diff --git a/codes/loader/ldpre_ast_redir.c b/codes/loader/ldpre_ast_redir.c index a13e4c2..19165b9 100644 --- a/codes/loader/ldpre_ast_redir.c +++ b/codes/loader/ldpre_ast_redir.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 18:41:08 by kyungjle #+# #+# */ -/* Updated: 2024/04/05 12:44:40 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 14:56:04 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,6 +73,7 @@ static t_bool infile_heredoc(char *filename, if (access(heredoc.heredoc_name, F_OK | W_OK | R_OK) != 0) return (ld_errno_file("ldpre_ast_redir.access", heredoc.heredoc_name)); delim_len = ft_strlen(filename); + // TODO heredoc delim에는 param expansion 없이 quote removal만 진행되여야 함 filename = ldpre_param_quote_f(ft_strdup(filename), env, NULL); expansion = (ft_strlen(filename) == delim_len); fd = dup(heredoc.stdin_fd); diff --git a/codes/main.c b/codes/main.c index 1d159e7..2f8692d 100644 --- a/codes/main.c +++ b/codes/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: nicknamemohaji +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/12 22:17:43 by kyungjle #+# #+# */ -/* Updated: 2024/04/01 13:03:53 by nicknamemoh ### ########.fr */ +/* Updated: 2024/04/05 14:26:21 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ volatile sig_atomic_t g_sigint = FALSE; +static void ld_env_set_exitcode(t_ld_map_env *env); + int main(int argc, char *argv[], char *envp[]) { t_ld_map_env *env; @@ -22,6 +24,7 @@ int main(int argc, char *argv[], char *envp[]) (void)argc; (void)argv; env = ldpre_env_fromenvp_f(envp); + ld_env_set_exitcode(env); while (1) { input = input_readline_f(); @@ -41,3 +44,18 @@ int main(int argc, char *argv[], char *envp[]) rl_clear_history(); return (0); } + +static void ld_env_set_exitcode(t_ld_map_env *env) +{ + t_ld_map_node *node; + + node = malloc(1 * sizeof(t_ld_map_node)); + if (node == NULL) + do_exit("main.ld_env_set_exitcode.malloc"); + node->key = ft_strdup("?"); + node->value = ft_itoa(0); + if (node->key == NULL || node->value == NULL) + do_exit("main.ld_env_set_exitcode.malloc"); + node->next = env->contents; + env->contents = node; +} diff --git a/codes/utils/free_ft_split.c b/codes/utils/free_ft_split.c index 3398766..5579601 100644 --- a/codes/utils/free_ft_split.c +++ b/codes/utils/free_ft_split.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/13 19:53:33 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/04 15:44:11 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 14:57:55 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/codes/utils/ft_qsort.c b/codes/utils/ft_qsort.c index e545cf9..92056fc 100644 --- a/codes/utils/ft_qsort.c +++ b/codes/utils/ft_qsort.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_qsort.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dogwak +#+ +:+ +#+ */ +/* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 17:59:28 by dogwak #+# #+# */ -/* Updated: 2024/04/04 19:42:41 by dogwak ### ########.fr */ +/* Updated: 2024/04/05 15:15:17 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,8 @@ t_bool compare(void *pa, void *pb) - if a is greater than b -> *pa > *pb, descending order. - if a is less than b -> *pa < *pb, ascending order. + if a is greater than b -> return *pa > *pb, descending order. + if a is less than b -> return *pa < *pb, ascending order. */ void ft_qsort(void **arr, int left, int right, t_bool (*cmp)(const void*, const void*)) diff --git a/codes/utils/ld_map_functions.c b/codes/utils/ld_map_functions.c index 2419692..71aee27 100644 --- a/codes/utils/ld_map_functions.c +++ b/codes/utils/ld_map_functions.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/19 00:09:12 by nicknamemoh #+# #+# */ -/* Updated: 2024/03/21 17:38:55 by kyungjle ### ########.fr */ +/* Updated: 2024/04/05 15:15:08 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,20 +116,20 @@ char **ldpre_env_toenvp_f(t_ld_map_env *map) ret = malloc(sizeof(char *) * (map->count + 1)); if (ret == NULL) do_exit("ldpre_env_toenvp_f.malloc"); - index = -1; - node = map->contents; - while (++index < map->count) + index = 0; + node = (map->contents)->next; + while (++index <= map->count) { - ret[index] = malloc(sizeof(char) * (1 + 1 + ret[index - 1] = malloc(sizeof(char) * (1 + 1 + ft_strlen(node->key) + ft_strlen(node->value))); - if (ret == NULL) + if (ret[index - 1] == NULL) do_exit("ldpre_env_toenvp_f.malloc"); - ft_strlcpy(ret[index], node->key, ft_strlen(node->key) + 1); - ft_strlcat(ret[index], "=", ft_strlen(ret[index]) + 2); - ft_strlcat(ret[index], node->value, - ft_strlen(ret[index]) + ft_strlen(node->value) + 1); + ft_strlcpy(ret[index - 1], node->key, ft_strlen(node->key) + 1); + ft_strlcat(ret[index - 1], "=", ft_strlen(ret[index - 1]) + 2); + ft_strlcat(ret[index - 1], node->value, + ft_strlen(ret[index - 1]) + ft_strlen(node->value) + 1); node = node->next; } - ret[index] = NULL; + ret[index - 1] = NULL; return (ret); } diff --git a/codes/utils/ld_map_functions2.c b/codes/utils/ld_map_functions2.c index 5da20c7..f2f980f 100644 --- a/codes/utils/ld_map_functions2.c +++ b/codes/utils/ld_map_functions2.c @@ -93,6 +93,7 @@ void ldpre_env_add(char *key, char *value, t_ld_map_env *map) node->value = value_copy; node->next = NULL; ld_map_node_attach(map, node); + map->count++; return ; }