diff --git a/Makefile b/Makefile index 26ffb7f..21c3842 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c OBJS = ${SRCS:.c=.o} diff --git a/builtins/cd.c b/builtins/cd.c new file mode 100644 index 0000000..87a4e4d --- /dev/null +++ b/builtins/cd.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */ +/* Updated: 2023/02/21 14:41:58 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int move_folder(char *path, int fd) +{ + char *join; + + if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0) + { + if (chdir(path) == 0) + return (0); + ft_printf("chdir error"); + return (1); + } + else + { + join = ft_strjoin("/", path); + join = ft_strfjoin(get_pwd(fd), join); + if (chdir(join) == 0) + { + free(join); + return (0); + } + free(join); + ft_printf("chdir error"); + return (1); + } +} + +/*int main(int argc, char *argv[]) { + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf("%s\n", cwd); + } else { + perror("getcwd() error"); + return 1; + } + move_folder(argv[1], 1); + if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf("%s\n", cwd); + } else { + perror("getcwd() error"); + return 1; + } + return 0; +}*/ diff --git a/builtins/pwd.c b/builtins/pwd.c index e978d21..65d3f00 100644 --- a/builtins/pwd.c +++ b/builtins/pwd.c @@ -6,15 +6,36 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ -/* Updated: 2023/02/17 17:08:00 by erey-bet ### ########.fr */ +/* Updated: 2023/02/21 14:34:50 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "../minishell.h" -int pwd(t_list **env, int fd) +int pwd(int fd) { - ft_putstr_fd(get_value_by_key("PWD", env), fd); - write(fd, "\n", 1); + char path[PATH_MAX]; + + if (getcwd(path, sizeof(path)) != NULL) + ft_putstr_fd(path, fd); + else + { + ft_putstr_fd("Error getcwd", fd); + return (1); + } return (0); } + +char *get_pwd(int fd) +{ + char *str; + + str = ft_calloc(PATH_MAX, sizeof(char *)); + if (getcwd(str, PATH_MAX) != NULL) + return (str); + else + { + ft_putstr_fd("Error getcwd", fd); + return (NULL); + } +} diff --git a/minishell.h b/minishell.h index ed865b8..c355236 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet