From 1233fce096d9bf039f3657217f35811c7eddf2f1 Mon Sep 17 00:00:00 2001 From: Kyungjun Lee Date: Mon, 15 Apr 2024 15:26:18 +0900 Subject: [PATCH] adding comments --- codes/builtins/builtin_cd.c | 12 +++++++++--- codes/builtins/builtin_echo.c | 12 ++++++++++-- codes/builtins/builtin_env.c | 10 ++-------- codes/builtins/builtin_exit.c | 15 +++++++++------ codes/builtins/builtin_export.c | 13 ++++++++++++- codes/builtins/builtin_pwd.c | 6 ++---- codes/includes/builtin.h | 4 ++-- codes/includes/types.h | 12 ++---------- codes/includes/utils.h | 3 ++- codes/input/input_sighandler.c | 2 +- codes/loader/ldpre_ast.c | 6 +++--- codes/loader/ldpre_ast_exec.c | 4 ++-- codes/loader/ldpre_ast_pipe.c | 4 ++-- codes/loader/ldpre_ast_redir.c | 20 ++++++++++---------- 14 files changed, 68 insertions(+), 55 deletions(-) diff --git a/codes/builtins/builtin_cd.c b/codes/builtins/builtin_cd.c index 91da94b..66de70b 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/09 20:29:12 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:05:25 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int builtin_cd(char *args[], t_ld_map_env *env) { char *pwd; - if (!builtin_check_argument_count((const char **)args, 2)) + if (!builtin_check_argument_count((const char **)args, 2, "cd")) return (EXIT_FAILURE); if (args[1] != NULL) { @@ -38,7 +38,7 @@ int builtin_cd(char *args[], t_ld_map_env *env) } else if (ft_strchr(args[1], '/') != NULL && ld_chdir("cd", args[1]) != TRUE) - return (no_such_file_or_directory(NULL, pwd)); + return (no_such_file_or_directory(args[1], pwd)); else if (ft_strchr(args[1], '/') == NULL && chdir(args[1]) != 0) return (no_such_file_or_directory(args[1], pwd)); ldpre_env_add(ft_strdup("PWD"), do_getcwd_f(NULL, 0), env); @@ -49,6 +49,12 @@ int builtin_cd(char *args[], t_ld_map_env *env) /* static int no_such_file_or_directory(char *path, char *pwd) +:path: path failed to move into. it is from argument, so do not free this.. +(arguments will be freed later) +:pwd: CWD before CD job was tried. as ld_chdir function breaks directories +and gradually changes directory, CWD even if cd was unsuccessful may have +been changed. this function will restore CWD and free the string. +:return: execution result (always 1) used to avoid line constraint, aka 'norm function'... */ diff --git a/codes/builtins/builtin_echo.c b/codes/builtins/builtin_echo.c index eacc533..b3890cb 100644 --- a/codes/builtins/builtin_echo.c +++ b/codes/builtins/builtin_echo.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/05 12:45:54 by kyungjle #+# #+# */ -/* Updated: 2024/04/09 18:01:44 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:01:47 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ int builtin_echo(char *args[], t_ld_map_env *env) t_bool print_nl; (void) env; + index = 1; print_nl = parse_args(args, &index); while (args[index] != NULL) { @@ -40,13 +41,20 @@ int builtin_echo(char *args[], t_ld_map_env *env) return (EXIT_SUCCESS); } +/* +static t_bool parse_args(char *args[], int *index) +:args: arguments to be checked +:index: pointer of index variable to be changed +:return: TRUE if `-n` argument was given + +checks for `-n` in arguments, and moves the index if it is an argument +*/ static t_bool parse_args(char *args[], int *index) { t_bool ret; int i; ret = TRUE; - *index = 1; while (args[*index] != NULL) { if (args[*index][0] != '-') diff --git a/codes/builtins/builtin_env.c b/codes/builtins/builtin_env.c index f3b3491..95e9dd0 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/09 15:01:34 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:02:39 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #include "utils.h" int builtin_env(char *args[], t_ld_map_env *env); -static int builtin_env_print(t_ld_map_env *env); /* int builtin_env(char *args[], t_ld_map_env *env) @@ -23,15 +22,10 @@ int builtin_env(char *args[], t_ld_map_env *env) :return: execution result (always 0) */ int builtin_env(char *args[], t_ld_map_env *env) -{ - (void)args; - return (builtin_env_print(env)); -} - -static int builtin_env_print(t_ld_map_env *env) { t_ld_map_node *node; + (void)args; node = (env->contents)->next; while (node != NULL) { diff --git a/codes/builtins/builtin_exit.c b/codes/builtins/builtin_exit.c index c4133be..92b67b3 100644 --- a/codes/builtins/builtin_exit.c +++ b/codes/builtins/builtin_exit.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:41 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/10 16:34:46 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:13:18 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,8 @@ int builtin_exit(char *args[], t_ld_map_env *env); static t_bool check_digit(const char *c); -t_bool builtin_check_argument_count(const char *args[], int limit); +t_bool builtin_check_argument_count(const char *args[], + int limit, char *cmd); /* int builtin_exit(char *args[], t_ld_map_env *env) @@ -26,7 +27,7 @@ will return with argument given (0 if no argument given) */ int builtin_exit(char *args[], t_ld_map_env *env) { - if (!builtin_check_argument_count((const char **) args, 2)) + if (!builtin_check_argument_count((const char **) args, 2, "exit")) return (1); if (args[1] != NULL) { @@ -70,14 +71,15 @@ static t_bool check_digit(const char *c) } /* -t_bool builtin_check_argument_count(const char *args[], int limit) +t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd) :args: argument list to be checked :limit: limit +:cmd: command name for printing trace :return: true if arguments are given less than limit checks for how many arguments given, checking for max argument count */ -t_bool builtin_check_argument_count(const char *args[], int limit) +t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd) { int count; @@ -89,7 +91,8 @@ t_bool builtin_check_argument_count(const char *args[], int limit) } if (count > limit) { - write(2, "exit: too many arguments\n", 25); + write(2, cmd, ft_strlen(cmd)); + write(2, ": too many arguments\n", 21); return (FALSE); } else diff --git a/codes/builtins/builtin_export.c b/codes/builtins/builtin_export.c index f74bf54..caf406b 100644 --- a/codes/builtins/builtin_export.c +++ b/codes/builtins/builtin_export.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:43 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/09 15:04:29 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:10:47 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,11 @@ int builtin_export(char *args[], t_ld_map_env *env) return (EXIT_SUCCESS); } +/* +static int builtin_export_print(t_ld_map_env *env) +:env: env to print +:return: execution result (always 0) +*/ static int builtin_export_print(t_ld_map_env *env) { char **envp; @@ -70,6 +75,12 @@ static int builtin_export_print(t_ld_map_env *env) return (EXIT_SUCCESS); } +/* +static t_bool cmp(const void *c1, const void *c2) + +compare function for ft_qsort. compare up to `=` delimeter. +this will sort env keys ascending. +*/ static t_bool cmp(const void *c1, const void *c2) { unsigned char *c1_ptr; diff --git a/codes/builtins/builtin_pwd.c b/codes/builtins/builtin_pwd.c index 5a79ad5..80ede05 100644 --- a/codes/builtins/builtin_pwd.c +++ b/codes/builtins/builtin_pwd.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:45 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/09 15:12:32 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:11:11 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,8 @@ int builtin_pwd(char *args[], t_ld_map_env *env) if (pwd == NULL) { pwd = do_getcwd_f(NULL, 0); - printf("%s\n", pwd); ldpre_env_add(ft_strdup("PWD"), pwd, env); } - else - printf("%s\n", pwd); + printf("%s\n", pwd); return (EXIT_SUCCESS); } diff --git a/codes/includes/builtin.h b/codes/includes/builtin.h index bfa1ae6..57b2920 100644 --- a/codes/includes/builtin.h +++ b/codes/includes/builtin.h @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:05:01 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/05 12:59:12 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:05:08 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int builtin_echo(char *args[], t_ld_map_env *env); // builtin_exit.c int builtin_exit(char *args[], t_ld_map_env *env); -t_bool builtin_check_argument_count(const char *args[], int limit); +t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd); // builtin_export.c diff --git a/codes/includes/types.h b/codes/includes/types.h index 06f4828..6992842 100644 --- a/codes/includes/types.h +++ b/codes/includes/types.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* types.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dogwak +#+ +:+ +#+ */ +/* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/12 22:18:03 by kyungjle #+# #+# */ -/* Updated: 2024/04/09 15:31:58 by dogwak ### ########.fr */ +/* Updated: 2024/04/15 15:16:39 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,14 +33,6 @@ typedef enum e_bool FALSE = 0 } t_bool; -// TODO ast travel status code -typedef enum e_ast_status -{ - AST_SUCCESS = 0, - AST_FAIL_REDIR = -2, - AST_SUCCESS_PIPE = -10 -} t_ast_status; - extern volatile sig_atomic_t g_sigint; # define INPUT_READLINE 2 diff --git a/codes/includes/utils.h b/codes/includes/utils.h index 6691c72..f540b98 100644 --- a/codes/includes/utils.h +++ b/codes/includes/utils.h @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/13 19:54:44 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/09 20:12:51 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:16:27 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ t_ld_map_node **ldpre_env_searchkey_f(char *key, t_ld_map_env *map); t_bool ldpre_env_validate_key(char *key); // ft_qsort.c + void ft_qsort(void **arr, int left, int right, t_bool (*cmp)(const void*, const void*)); diff --git a/codes/input/input_sighandler.c b/codes/input/input_sighandler.c index 4cbfb70..3369dd1 100644 --- a/codes/input/input_sighandler.c +++ b/codes/input/input_sighandler.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/12 22:19:05 by kyungjle #+# #+# */ -/* Updated: 2024/04/10 14:23:49 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:24:53 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/codes/loader/ldpre_ast.c b/codes/loader/ldpre_ast.c index 9f547e9..582928e 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/09 15:50:43 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:14:44 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,6 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env, else if (ast->node_type == EXP_PRE_RAPPEND || ast->node_type == EXP_PRE_RWRITE) return (ldpre_ast_wpopen(ast, env, exec, heredoc)); - dprintf(2, "unknown node type..!!! %d %s", ast->node_type, ast->pcmd[0]); - return (-2); + printf("unknown node type..!!! %d %s", ast->node_type, ast->pcmd[0]); + return (-42); } diff --git a/codes/loader/ldpre_ast_exec.c b/codes/loader/ldpre_ast_exec.c index 86ac200..807c477 100644 --- a/codes/loader/ldpre_ast_exec.c +++ b/codes/loader/ldpre_ast_exec.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/08 20:04:57 by kyungjle #+# #+# */ -/* Updated: 2024/04/09 15:50:43 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:17:32 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ int ldpre_ast_exec(t_ast_node *ast, t_ld_map_env *env, while (exec->next != NULL) exec = exec->next; exec->next = node; - return (AST_SUCCESS_PIPE); + return (0); } return (exitcode); } diff --git a/codes/loader/ldpre_ast_pipe.c b/codes/loader/ldpre_ast_pipe.c index f281b18..196a370 100644 --- a/codes/loader/ldpre_ast_pipe.c +++ b/codes/loader/ldpre_ast_pipe.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/08 18:08:58 by kyungjle #+# #+# */ -/* Updated: 2024/04/10 00:28:07 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:17:02 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,5 +69,5 @@ static int run_pipe(t_ast_node *ast, t_ld_map_env *env, redirect_fd(&preserve_fd[0], STDIN_FD, pipe_fd[0]); ldpre_ast(ast->right, env, exec, heredoc); restore_fd(preserve_fd[0], STDIN_FD); - return (AST_SUCCESS_PIPE); + return (0); } diff --git a/codes/loader/ldpre_ast_redir.c b/codes/loader/ldpre_ast_redir.c index 584697c..d5ce6c7 100644 --- a/codes/loader/ldpre_ast_redir.c +++ b/codes/loader/ldpre_ast_redir.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/08 18:56:03 by kyungjle #+# #+# */ -/* Updated: 2024/04/09 20:41:36 by kyungjle ### ########.fr */ +/* Updated: 2024/04/15 15:17:23 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,18 +32,18 @@ int ldpre_ast_wopen(t_ast_node *ast, t_ld_map_env *env, left_isfile = TRUE; if (ldpre_ast_redir_outfile((ast->left)->pcmd[0], ast->node_type) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } if ((ast->right)->node_type == NODE_FILE) { if (ldpre_ast_redir_outfile((ast->right)->pcmd[0], ast->node_type) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } else { if (ldpre_ast(ast->right, env, exec, heredoc) < 0) - return (AST_FAIL_REDIR); + return (-1); } if (!left_isfile) return (ldpre_ast(ast->left, env, exec, heredoc)); @@ -61,16 +61,16 @@ int ldpre_ast_ropen(t_ast_node *ast, t_ld_map_env *env, right_isfile = TRUE; if (ldpre_ast_redir_infile((ast->right)->pcmd[0], heredoc, ast->node_type, env) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } if ((ast->left)->node_type == NODE_FILE) { if (ldpre_ast_redir_infile((ast->left)->pcmd[0], heredoc, ast->node_type, env) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } else if (ldpre_ast(ast->left, env, exec, heredoc) < 0) - return (AST_FAIL_REDIR); + return (-1); if (!right_isfile) return (ldpre_ast(ast->right, env, exec, heredoc)); return (0); @@ -87,18 +87,18 @@ int ldpre_ast_wpopen(t_ast_node *ast, t_ld_map_env *env, right_isfile = TRUE; if (ldpre_ast_redir_outfile((ast->right)->pcmd[0], ast->node_type) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } if ((ast->left)->node_type == NODE_FILE) { if (ldpre_ast_redir_outfile((ast->left)->pcmd[0], ast->node_type) == FALSE) - return (AST_FAIL_REDIR); + return (-1); } else { if (ldpre_ast(ast->left, env, exec, heredoc) < 0) - return (AST_FAIL_REDIR); + return (-1); } if (!right_isfile && ast->right != NULL) return (ldpre_ast(ast->right, env, exec, heredoc));