diff --git a/Makefile b/Makefile index 1d3b21f..f9432dd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,19 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # +# Updated: 2024/02/16 21:56:22 by tomoron ### ########.fr # +# # +# **************************************************************************** # + CC = cc SRCS_RAW = main.c\ lst_cmd.c\ + cd.c\ lst_env.c\ exec.c\ exit.c\ @@ -16,6 +29,7 @@ OBJS_DIR = objs/ SRCS_DIR = srcs/ SRCS = $(addprefix $(SRCS_DIR), $(SRCS_RAW)) OBJS = $(addprefix $(OBJS_DIR), $(SRCS_RAW:.c=.o)) + FLAGS = -Wall -Wextra -Werror -g LIBFT = libft/libft.a NAME = minishell @@ -45,4 +59,3 @@ fclean: clean re: fclean all .PHONY: all clean fclean re - diff --git a/cd.c b/cd.c new file mode 100644 index 0000000..3edb055 --- /dev/null +++ b/cd.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marde-vr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */ +/* Updated: 2024/02/16 21:51:28 by marde-vr ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int cd(t_cmd *args, t_env *env) +{ + char *cwd; + char *new_wd; + + cwd = getcwd(0, 0); + if (args->next->next && args->next->next->type == ARG) + { + ft_printf_fd(2, "minishell: cd: too many arguments\n"); + return (1); + } + if (!args->next || args->next->type != ARG) + new_wd = ft_get_env(env, "HOME"); + else + { + new_wd = args->next->token; + if (chdir(new_wd) == -1) + perror("chdir"); + } + ft_putstr(cwd); + (void)args; + return (0); +} diff --git a/srcs/exec.c b/srcs/exec.c index 6cf0886..56a248b 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/02/16 21:52:02 by tomoron ### ########.fr */ +/* Updated: 2024/02/16 21:56:40 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,8 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env) exit_bt(parsed_cmd, env); else if (!ft_strcmp(parsed_cmd->token, "pwd")) g_return_code = pwd(); + else if (!ft_strcmp(parsed_cmd->token, "cd")) + g_return_code = cd(parsed_cmd); else return (STDIN_FILENO); return (STDOUT_FILENO); diff --git a/srcs/main.c b/srcs/main.c index 781a330..4f08020 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/02/16 21:29:05 by tomoron ### ########.fr */ +/* Updated: 2024/02/16 21:57:11 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */