From 7e6fe22791bf45537879a5c61d002734918223a4 Mon Sep 17 00:00:00 2001 From: william Date: Sat, 25 Mar 2017 23:00:02 +0100 Subject: [PATCH 1/2] cd fonctionnel mais pas l'option -P --- 42sh/Makefile | 538 ++++++++++++------------ 42sh/src/builtin/bt_cd_process_dotdot.c | 32 -- 42sh/src/builtin/builtin_cd.c | 72 ++-- 3 files changed, 305 insertions(+), 337 deletions(-) delete mode 100644 42sh/src/builtin/bt_cd_process_dotdot.c diff --git a/42sh/Makefile b/42sh/Makefile index d4a7f423..7f741ccd 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -13,8 +13,7 @@ NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -FLAGS = -Wall -Wextra -Werror #-fsanitize=address#-fvisibility=hidden +FLAGS = -Wall -Wextra -Werror -fsanitize=address#-fvisibility=hidden D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -30,290 +29,289 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ -builtin/bt_cd_process_dotdot.c\ -builtin/bt_env_opt.c\ -builtin/bt_read_get.c\ -builtin/bt_read_term.c\ -builtin/builtin_cd.c\ -builtin/builtin_echo.c\ -builtin/builtin_env.c\ -builtin/builtin_exit.c\ -builtin/builtin_export.c\ -builtin/builtin_func.c\ -builtin/builtin_hash.c\ -builtin/builtin_history.c\ -builtin/builtin_math.c\ -builtin/builtin_read.c\ -builtin/builtin_setenv.c\ -builtin/builtin_unset.c\ -builtin/builtin_unsetenv.c\ -builtin/is_builtin.c\ -completion/c_arrow.c\ -completion/c_clear.c\ -completion/c_find_abspath.c\ +completion/c_pathsolver.c\ +completion/c_init.c\ +completion/c_printer.c\ completion/c_find_binary.c\ completion/c_find_env.c\ -completion/c_find_files.c\ -completion/c_init.c\ -completion/c_match.c\ -completion/c_match_glob.c\ -completion/c_match_update.c\ -completion/c_misc.c\ -completion/c_output.c\ +completion/c_clear.c\ completion/c_parser.c\ -completion/c_pathsolver.c\ -completion/c_printer.c\ +completion/c_arrow.c\ completion/c_sizing.c\ -completion/c_terminal.c\ completion/completion.c\ -exec/ast_free.c\ -exec/bad_fd.c\ -exec/exec_ampersand.c\ -exec/exec_and_if.c\ -exec/exec_bang.c\ -exec/exec_case_branch.c\ -exec/exec_elif.c\ -exec/exec_else.c\ -exec/exec_func.c\ -exec/exec_leaf.c\ -exec/exec_or_if.c\ -exec/exec_pipe.c\ -exec/exec_reset.c\ -exec/exec_semi.c\ -exec/exec_var.c\ -exec/fd_is_valid.c\ -exec/ft_exec.c\ -exec/ft_findexec.c\ -exec/is_function.c\ -exec/mark_process_status.c\ -exec/node_copy.c\ -exec/pfree_cmd.c\ -exec/pfree_cond.c\ -exec/pfree_func.c\ -exec/pfree_list.c\ -exec/pfree_subshell.c\ -exec/plaunch_brace.c\ -exec/plaunch_builtin.c\ -exec/plaunch_case.c\ -exec/plaunch_empty.c\ -exec/plaunch_file.c\ -exec/plaunch_for.c\ -exec/plaunch_function.c\ -exec/plaunch_if.c\ -exec/plaunch_subshell.c\ -exec/plaunch_until.c\ -exec/plaunch_while.c\ -exec/process_launch.c\ -exec/process_redirect.c\ -exec/process_set.c\ -exec/process_setgroup.c\ -exec/process_setsig.c\ -exec/pset_brace.c\ -exec/pset_case.c\ -exec/pset_cmd.c\ -exec/pset_for.c\ -exec/pset_if.c\ -exec/pset_subshell.c\ -exec/pset_until.c\ -exec/pset_while.c\ -exec/redir_copy.c\ -exec/redir_free.c\ -exec/redirect_dgreat.c\ -exec/redirect_dless.c\ -exec/redirect_great.c\ -exec/redirect_greatand.c\ -exec/redirect_less.c\ -exec/redirect_lessand.c\ -exec/set_exitstatus.c\ -exec/token_to_argv.c\ -glob/command_getoutput.c\ -glob/dir_glob.c\ -glob/esc_print.c\ -glob/expand_bquote.c\ -glob/expand_brace.c\ -glob/expand_esc.c\ -glob/expand_home.c\ -glob/expand_var.c\ -glob/ft_strsplit_esc.c\ -glob/ft_strsplit_spe.c\ -glob/gen_tab_esc.c\ -glob/glob.c\ -glob/is_char_esc.c\ -glob/lib_perso/ft_ld_back.c\ -glob/lib_perso/ft_ld_clear.c\ -glob/lib_perso/ft_ld_copy.c\ -glob/lib_perso/ft_ld_del.c\ -glob/lib_perso/ft_ld_front.c\ -glob/lib_perso/ft_ld_new.c\ -glob/lib_perso/ft_ld_order.c\ -glob/lib_perso/ft_ld_pushback.c\ -glob/lib_perso/ft_ld_pushfront.c\ -glob/lib_perso/ft_ld_reverse.c\ -glob/lib_perso/ft_ld_size.c\ -glob/lib_perso/ft_ld_swap.c\ -glob/lib_perso/ft_ld_to_tab.c\ -glob/lib_perso/ft_memrealloc.c\ -glob/lib_perso/ft_strjoinf.c\ -glob/lib_perso/ft_strsubf.c\ -glob/lib_perso/ft_tabdel.c\ -glob/lib_perso/ft_tablen.c\ -glob/match_pattern.c\ -glob/tab_esc_copy.c\ -glob/word_is_assignment.c\ -hash_table/ft_add_hash.c\ -hash_table/hash.c\ -hash_table/hash_free.c\ -hash_table/hash_str.c\ -hash_table/is_hash.c\ -history/add_str_in_history.c\ -history/history.c\ -history/history_parsing.c\ -history/history_parsing_toolz.c\ -history/history_parsing_toolz_2.c\ -history/list_toolz.c\ +completion/c_match_glob.c\ +completion/c_find_files.c\ +completion/c_terminal.c\ +completion/c_output.c\ +completion/c_misc.c\ +completion/c_match.c\ +completion/c_match_update.c\ +completion/c_find_abspath.c\ history/surch_in_history.c\ -job_control/builtin_bg.c\ -job_control/builtin_fg.c\ -job_control/builtin_jobs.c\ -job_control/do_job_notification.c\ -job_control/has_running_job.c\ -job_control/has_stopped_job.c\ -job_control/job_addprocess.c\ -job_control/job_cmp_id.c\ -job_control/job_format.c\ -job_control/job_format_head.c\ -job_control/job_free.c\ -job_control/job_getprocess.c\ -job_control/job_getrank.c\ -job_control/job_hup_all.c\ -job_control/job_is_completed.c\ -job_control/job_is_stopped.c\ -job_control/job_notify_change.c\ -job_control/job_notify_new.c\ -job_control/job_remove.c\ -job_control/job_run.c\ -job_control/job_update_id.c\ -job_control/job_update_status.c\ -job_control/job_wait.c\ -job_control/mark_job_as_running.c\ -job_control/pprint_brace.c\ -job_control/pprint_case.c\ -job_control/pprint_cmd.c\ -job_control/pprint_for.c\ -job_control/pprint_function.c\ -job_control/pprint_if.c\ -job_control/pprint_subshell.c\ -job_control/pprint_until.c\ +history/history_parsing_toolz_2.c\ +history/history_parsing_toolz.c\ +history/list_toolz.c\ +history/add_str_in_history.c\ +history/history_parsing.c\ +history/history.c\ job_control/pprint_while.c\ -job_control/process_cmp_pid.c\ -job_control/process_format.c\ -job_control/process_free.c\ -job_control/put_job_in_background.c\ -job_control/put_job_in_foreground.c\ -job_control/sigchld_handler.c\ +job_control/mark_job_as_running.c\ +job_control/pprint_case.c\ +job_control/job_notify_new.c\ job_control/sigint_handler.c\ +job_control/job_update_status.c\ +job_control/job_format_head.c\ +job_control/job_is_completed.c\ +job_control/job_wait.c\ job_control/sigtstp_handler.c\ +job_control/job_free.c\ +job_control/pprint_for.c\ +job_control/pprint_if.c\ +job_control/job_hup_all.c\ +job_control/put_job_in_foreground.c\ +job_control/job_is_stopped.c\ +job_control/process_free.c\ job_control/sigttin_handler.c\ job_control/sigttou_handler.c\ -lexer/do_lexer_routine.c\ -lexer/get_lexer_stack.c\ -lexer/get_lexer_stack2.c\ -lexer/get_reserved_words.c\ -lexer/get_state_global.c\ -lexer/get_state_redir.c\ -lexer/insert_newline.c\ -lexer/isrw_delim.c\ -lexer/keep_last_type.c\ -lexer/lexer_backslash.c\ -lexer/lexer_bquote.c\ -lexer/lexer_curly_braces.c\ -lexer/lexer_default.c\ -lexer/lexer_delim.c\ -lexer/lexer_destroy.c\ -lexer/lexer_dquote.c\ -lexer/lexer_end.c\ -lexer/lexer_great.c\ -lexer/lexer_heredoc.c\ -lexer/lexer_init.c\ -lexer/lexer_less.c\ -lexer/lexer_lex.c\ -lexer/lexer_newline.c\ -lexer/lexer_number.c\ -lexer/lexer_paren.c\ -lexer/lexer_quote.c\ -lexer/lexer_sep.c\ -lexer/lexer_word.c\ -lexer/token_append.c\ -lexer/token_cmp_type.c\ -lexer/token_free.c\ -lexer/token_init.c\ -lexer/token_print.c\ -line_editing/control_features.c\ -line_editing/copy_cut_paste/copy_cut_paste.c\ -line_editing/copy_cut_paste/underline_down.c\ -line_editing/copy_cut_paste/underline_end.c\ -line_editing/copy_cut_paste/underline_function.c\ -line_editing/copy_cut_paste/underline_home.c\ -line_editing/copy_cut_paste/underline_left.c\ -line_editing/copy_cut_paste/underline_reset.c\ -line_editing/copy_cut_paste/underline_right.c\ -line_editing/copy_cut_paste/underline_up.c\ -line_editing/ft_prompt.c\ -line_editing/get_key.c\ -line_editing/init_line/init_history.c\ -line_editing/init_line/init_line.c\ -line_editing/init_line/init_termcaps.c\ -line_editing/lib_line_editing/ft_nb_line.c\ -line_editing/lib_line_editing/tool_line.c\ -line_editing/lib_line_editing/tool_line_2.c\ -line_editing/lib_line_editing/toolz.c\ -line_editing/lib_line_editing/toolz2.c\ -line_editing/lib_line_editing/toolz_parseur.c\ -line_editing/lib_line_editing/toolz_termcaps.c\ -line_editing/move_term/home_end.c\ -line_editing/move_term/move_left_and_right.c\ -line_editing/move_term/move_to_word.c\ -line_editing/move_term/move_up_and_down.c\ -line_editing/print_del_completion/completion.c\ -line_editing/print_del_completion/print_and_del.c\ -line_editing/print_del_completion/queue.c\ -line_editing/readline.c\ -line_editing/resize.c\ -main/data_exit.c\ -main/data_init.c\ -main/data_singleton.c\ -main/ft_putast.c\ -main/main.c\ -main/shell_init.c\ -main/shell_reset.c\ -parser/add_bang.c\ -parser/add_case.c\ +job_control/builtin_jobs.c\ +job_control/process_cmp_pid.c\ +job_control/pprint_subshell.c\ +job_control/job_remove.c\ +job_control/builtin_bg.c\ +job_control/job_run.c\ +job_control/has_stopped_job.c\ +job_control/pprint_cmd.c\ +job_control/job_cmp_id.c\ +job_control/pprint_brace.c\ +job_control/job_addprocess.c\ +job_control/job_update_id.c\ +job_control/do_job_notification.c\ +job_control/job_getrank.c\ +job_control/has_running_job.c\ +job_control/process_format.c\ +job_control/job_getprocess.c\ +job_control/job_notify_change.c\ +job_control/job_format.c\ +job_control/put_job_in_background.c\ +job_control/pprint_until.c\ +job_control/pprint_function.c\ +job_control/builtin_fg.c\ +job_control/sigchld_handler.c\ +parser/tree_func_free.c\ +parser/parser_destroy.c\ +parser/tree_wrapper.c\ +parser/pop_stack.c\ parser/add_cmd.c\ -parser/add_condition.c\ +parser/parser_init.c\ +parser/aggregate_sym.c\ +parser/redir_init.c\ +parser/do_parser_routine.c\ +parser/stack_init.c\ +parser/add_number.c\ parser/add_func.c\ parser/add_loop.c\ -parser/add_number.c\ -parser/add_redir.c\ -parser/add_sep.c\ -parser/add_subshell.c\ -parser/aggregate_sym.c\ -parser/build_tree.c\ -parser/do_parser_routine.c\ -parser/error_syntax.c\ -parser/eval_sym.c\ -parser/ft_parse.c\ -parser/heredoc_parser.c\ -parser/parser_destroy.c\ -parser/parser_init.c\ -parser/pop_stack.c\ -parser/produce_sym.c\ -parser/push_stack.c\ parser/read_stack.c\ -parser/redir_init.c\ -parser/stack_init.c\ +parser/push_stack.c\ +parser/add_sep.c\ +parser/ft_parse.c\ +parser/add_condition.c\ +parser/produce_sym.c\ +parser/heredoc_parser.c\ +parser/add_case.c\ parser/sym_free.c\ -parser/tree_func_free.c\ -parser/tree_wrapper.c +parser/add_subshell.c\ +parser/build_tree.c\ +parser/eval_sym.c\ +parser/error_syntax.c\ +parser/add_bang.c\ +parser/add_redir.c\ +main/data_exit.c\ +main/ft_putast.c\ +main/main.c\ +main/shell_reset.c\ +main/data_singleton.c\ +main/data_init.c\ +main/shell_init.c\ +line_editing/lib_line_editing/toolz_termcaps.c\ +line_editing/lib_line_editing/tool_line_2.c\ +line_editing/lib_line_editing/tool_line.c\ +line_editing/lib_line_editing/toolz.c\ +line_editing/lib_line_editing/toolz_parseur.c\ +line_editing/lib_line_editing/ft_nb_line.c\ +line_editing/lib_line_editing/toolz2.c\ +line_editing/resize.c\ +line_editing/control_features.c\ +line_editing/print_del_completion/queue.c\ +line_editing/print_del_completion/completion.c\ +line_editing/print_del_completion/print_and_del.c\ +line_editing/get_key.c\ +line_editing/ft_prompt.c\ +line_editing/readline.c\ +line_editing/move_term/move_up_and_down.c\ +line_editing/move_term/home_end.c\ +line_editing/move_term/move_to_word.c\ +line_editing/move_term/move_left_and_right.c\ +line_editing/init_line/init_termcaps.c\ +line_editing/init_line/init_history.c\ +line_editing/init_line/init_line.c\ +line_editing/copy_cut_paste/underline_home.c\ +line_editing/copy_cut_paste/underline_down.c\ +line_editing/copy_cut_paste/underline_end.c\ +line_editing/copy_cut_paste/underline_reset.c\ +line_editing/copy_cut_paste/underline_function.c\ +line_editing/copy_cut_paste/copy_cut_paste.c\ +line_editing/copy_cut_paste/underline_right.c\ +line_editing/copy_cut_paste/underline_up.c\ +line_editing/copy_cut_paste/underline_left.c\ +glob/expand_home.c\ +glob/expand_bquote.c\ +glob/dir_glob.c\ +glob/ft_strsplit_esc.c\ +glob/expand_var.c\ +glob/glob.c\ +glob/word_is_assignment.c\ +glob/match_pattern.c\ +glob/lib_perso/ft_ld_front.c\ +glob/lib_perso/ft_ld_new.c\ +glob/lib_perso/ft_ld_size.c\ +glob/lib_perso/ft_tablen.c\ +glob/lib_perso/ft_ld_pushfront.c\ +glob/lib_perso/ft_ld_swap.c\ +glob/lib_perso/ft_ld_order.c\ +glob/lib_perso/ft_strsubf.c\ +glob/lib_perso/ft_ld_reverse.c\ +glob/lib_perso/ft_ld_to_tab.c\ +glob/lib_perso/ft_tabdel.c\ +glob/lib_perso/ft_strjoinf.c\ +glob/lib_perso/ft_ld_del.c\ +glob/lib_perso/ft_ld_back.c\ +glob/lib_perso/ft_memrealloc.c\ +glob/lib_perso/ft_ld_copy.c\ +glob/lib_perso/ft_ld_pushback.c\ +glob/lib_perso/ft_ld_clear.c\ +glob/expand_brace.c\ +glob/tab_esc_copy.c\ +glob/ft_strsplit_spe.c\ +glob/esc_print.c\ +glob/expand_esc.c\ +glob/is_char_esc.c\ +glob/command_getoutput.c\ +glob/gen_tab_esc.c\ +lexer/lexer_delim.c\ +lexer/lexer_paren.c\ +lexer/get_state_redir.c\ +lexer/lexer_quote.c\ +lexer/lexer_dquote.c\ +lexer/token_free.c\ +lexer/token_cmp_type.c\ +lexer/lexer_sep.c\ +lexer/lexer_heredoc.c\ +lexer/token_init.c\ +lexer/lexer_less.c\ +lexer/insert_newline.c\ +lexer/get_lexer_stack2.c\ +lexer/lexer_init.c\ +lexer/keep_last_type.c\ +lexer/isrw_delim.c\ +lexer/lexer_lex.c\ +lexer/lexer_destroy.c\ +lexer/lexer_word.c\ +lexer/lexer_newline.c\ +lexer/get_lexer_stack.c\ +lexer/lexer_great.c\ +lexer/lexer_curly_braces.c\ +lexer/lexer_end.c\ +lexer/get_reserved_words.c\ +lexer/do_lexer_routine.c\ +lexer/token_append.c\ +lexer/get_state_global.c\ +lexer/lexer_bquote.c\ +lexer/lexer_default.c\ +lexer/token_print.c\ +lexer/lexer_backslash.c\ +lexer/lexer_number.c\ +builtin/builtin_func.c\ +builtin/builtin_export.c\ +builtin/builtin_unsetenv.c\ +builtin/builtin_setenv.c\ +builtin/builtin_unset.c\ +builtin/builtin_math.c\ +builtin/bt_read_term.c\ +builtin/builtin_echo.c\ +builtin/builtin_hash.c\ +builtin/bt_env_opt.c\ +builtin/builtin_exit.c\ +builtin/builtin_cd.c\ +builtin/builtin_read.c\ +builtin/is_builtin.c\ +builtin/bt_read_get.c\ +builtin/builtin_history.c\ +builtin/builtin_env.c\ +exec/pset_brace.c\ +exec/pset_if.c\ +exec/pfree_func.c\ +exec/plaunch_subshell.c\ +exec/pset_case.c\ +exec/exec_ampersand.c\ +exec/exec_and_if.c\ +exec/exec_var.c\ +exec/exec_case_branch.c\ +exec/redirect_greatand.c\ +exec/redir_copy.c\ +exec/redir_free.c\ +exec/token_to_argv.c\ +exec/exec_pipe.c\ +exec/pfree_cond.c\ +exec/pset_for.c\ +exec/pset_until.c\ +exec/exec_func.c\ +exec/exec_or_if.c\ +exec/node_copy.c\ +exec/pfree_list.c\ +exec/redirect_dless.c\ +exec/exec_bang.c\ +exec/redirect_dgreat.c\ +exec/mark_process_status.c\ +exec/plaunch_for.c\ +exec/is_function.c\ +exec/pset_while.c\ +exec/exec_else.c\ +exec/ast_free.c\ +exec/exec_semi.c\ +exec/plaunch_while.c\ +exec/bad_fd.c\ +exec/exec_leaf.c\ +exec/plaunch_if.c\ +exec/plaunch_case.c\ +exec/process_redirect.c\ +exec/process_setgroup.c\ +exec/ft_exec.c\ +exec/set_exitstatus.c\ +exec/plaunch_brace.c\ +exec/plaunch_empty.c\ +exec/redirect_great.c\ +exec/process_set.c\ +exec/exec_reset.c\ +exec/plaunch_until.c\ +exec/pset_cmd.c\ +exec/redirect_lessand.c\ +exec/ft_findexec.c\ +exec/redirect_less.c\ +exec/pset_subshell.c\ +exec/fd_is_valid.c\ +exec/exec_elif.c\ +exec/plaunch_builtin.c\ +exec/pfree_subshell.c\ +exec/plaunch_file.c\ +exec/process_setsig.c\ +exec/plaunch_function.c\ +exec/process_launch.c\ +exec/pfree_cmd.c\ +hash_table/ft_add_hash.c\ +hash_table/is_hash.c\ +hash_table/hash.c\ +hash_table/hash_free.c\ +hash_table/hash_str.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/42sh/src/builtin/bt_cd_process_dotdot.c b/42sh/src/builtin/bt_cd_process_dotdot.c deleted file mode 100644 index 1d64cd1b..00000000 --- a/42sh/src/builtin/bt_cd_process_dotdot.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* bt_cd_process_dotdot.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */ -/* Updated: 2017/03/25 21:19:07 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -#define CDERR_0 "cd : %s: No such file or directory" -#define CDERR_1 "cd : %s: Permission denied" -#define CDERR_2 "cd : %s: Not a directory" -#define CDERR_3 "cd : unable to proceed: %s" - -int bt_cd_process_dotdot(char *target) -{ - if (access(target, F_OK)) - return (SH_ERR(CDERR_0, target)); - if (access(target, X_OK)) - return (SH_ERR(CDERR_1, target)); - if (!is_directory(target)) - return (SH_ERR(CDERR_2, target)); - if (chdir(target)) - return (SH_ERR(CDERR_3, target)); - setwd("PWD"); - return (0); -} diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 3a8882dd..af086139 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -19,6 +19,10 @@ #define CD_USAGE "usage: cd [-L|-P] [dir]" #define CDERR_0 "cd: too many arguments" #define CDERR_1 "cd %s not set" +#define CDERR_2 "cd : %s: No such file or directory" +#define CDERR_3 "cd : %s: Permission denied" +#define CDERR_4 "cd : %s: Not a directory" +#define CDERR_5 "cd : unable to proceed: %s" static t_cliopts g_cdopts[] = { @@ -27,43 +31,42 @@ static t_cliopts g_cdopts[] = {0, NULL, 0, 0, NULL, 0}, }; -static char *cd_operand_exist(char *arg) +int cd_file_autorisations(char *target, int flag) { - char *target; - - target = NULL; - if (!arg) - { - if (!(target = ft_strdup(ft_getenv(data_singleton()->env, "HOME")))) - SH_ERR(CDERR_1, "HOME"); - } - return (target); + (void)flag; + if (access(target, F_OK)) + return (SH_ERR(CDERR_2, target)); + if (!is_directory(target)) + return (SH_ERR(CDERR_3, target)); + if (access(target, X_OK)) + return (SH_ERR(CDERR_4, target)); + if (chdir(target)) + return (SH_ERR(CDERR_5, target)); + setwd("PWD"); + return (0); } -static char *cd_operand_begin(char *arg) +static char *cd_operand_exist(char *arg) { - char *target; + char *tget; - if (arg && arg[0]) + tget = NULL; + if (!arg) { - if (arg[0] == '/') - target = ft_strdup(arg); - else if (!ft_strncmp(arg, "./", 2) || !ft_strncmp(arg, "../", 3) - || !ft_strcmp(arg, ".") || !ft_strcmp(arg, "..")) - target = ft_str3join(ft_getenv(data_singleton()->env, - "PWD"), "/", arg); - else if (!ft_strcmp(arg, "-")) - { - if (!(target = ft_strdup(ft_getenv(data_singleton()->env, - "OLDPWD")))) - SH_ERR(CDERR_1, "OLDPWD"); - } - else - target = ft_str3join(".", "/", arg); + if (!(tget = ft_strdup(ft_getenv(data_singleton()->env, "HOME")))) + return (SH_ERR(CDERR_1, "HOME") ? NULL : NULL); } else - target = arg; - return (target); + { + if (!ft_strcmp(arg, "-")) + { + if (!(tget = ft_strdup(ft_getenv(data_singleton()->env, "OLDPWD")))) + return (SH_ERR(CDERR_1, "OLDPWD") ? NULL : NULL); + return (tget); + } + return (tget = ft_strdup(arg)); + } + return (tget); } void setwd(char *var) @@ -72,7 +75,7 @@ void setwd(char *var) cwd = getcwd(NULL, 0); builtin_setenv(NULL, (char*[]){"cd", var, cwd, NULL}, NULL); - free(cwd); + ft_strdel(&cwd); } int builtin_cd(const char *path, char *const av[], @@ -86,17 +89,16 @@ int builtin_cd(const char *path, char *const av[], (void)envp; (void)path; data.flag = CD_OPT_L; - ret = 0; if (cliopts_get((char **)av, g_cdopts, &data)) return (1); if (data.av_data[0] && data.av_data[1]) return (SH_ERR(CDERR_0) && SH_ERR(CD_USAGE)); if (!(target = cd_operand_exist(*data.av_data))) - target = cd_operand_begin(*data.av_data); + return (1); oldpwd = getcwd(NULL, 0); - if ((ret = bt_cd_process_dotdot(target))) + if (!(ret = cd_file_autorisations(target, data.flag))) builtin_setenv(NULL, (char*[]){"cd", "OLDPWD", oldpwd, NULL}, NULL); - free(target); - free(oldpwd); + ft_strdel(&target); + ft_strdel(&oldpwd); return (ret); } From 2d19687c582fc3c7baa89af2a7bba1f5b0f56601 Mon Sep 17 00:00:00 2001 From: william Date: Sat, 25 Mar 2017 23:04:51 +0100 Subject: [PATCH 2/2] rectif env quand multiples args --- 42sh/src/builtin/builtin_env.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index f2c6284b..73c3f826 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -25,10 +25,15 @@ static t_cliopts g_env_opts[] = static int bt_env_getcustom(char ***av, t_env_data *data) { + char **split; + if (!av || !*av || !data) return (1); while (**av && ft_strchr(**av, '=')) { + split = ft_strsplit(**av, '='); + bt_env_opt_u(split[0], data); + ft_tabdel(&split); data->custom_env = ft_sstradd(data->custom_env, **av); ++(*av); }