diff --git a/42sh/VAR b/42sh/VAR deleted file mode 100644 index 7813ba73..00000000 --- a/42sh/VAR +++ /dev/null @@ -1,23 +0,0 @@ -$TOKEN201703241737_NAME -21sh_error -42ShellTester -42sh -42sh_error -Makefile -README.md -STDBUG -VAR -auteur -donovan_segaults_06-02 -file -includes -libft -minishell_error -new_file -objs -pdf -sample -scriptheader.sh -src -update_makefile.sh -write_on_stdout_and_stderr diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index fd7e5477..5d0cf86e 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ -/* Updated: 2017/03/24 23:17:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 04:05:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,11 @@ int builtin_math(const char *path, char *const av[], char *const envp[]); int builtin_func(const char *path, char *const av[], char *const envp[]); int bt_env_geti(char ***av, t_env_data *data); + +int get_cdpath(char *arg); +int process_dotdot(char *target); +int process_symlink(char *target); + int error_msg(char *msg); #endif diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 8ab633b5..27ef1a47 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -192,6 +192,7 @@ str/ft_strstr.c\ str/ft_strsub.c\ str/ft_strtok.c\ str/ft_strtrim.c\ +sys/create_directory.c\ sys/dup2_close.c\ sys/fd_replace.c\ sys/ft_getenv.c\ diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index c7a046cb..229af35c 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/25 01:42:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 04:07:20 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ char *ft_getenv(char **env, char *key); int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); int is_directory(const char *path); +char *create_direcotry(const char *path, const char *old_pathnames); int dup2_close(int fd1, int fd2); int fd_replace(int fd1, int fd2); diff --git a/42sh/libft/src/sys/create_directory.c b/42sh/libft/src/sys/create_directory.c new file mode 100644 index 00000000..9df0fcd5 --- /dev/null +++ b/42sh/libft/src/sys/create_directory.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* create_directory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/25 03:18:18 by ariard #+# #+# */ +/* Updated: 2017/03/25 04:07:10 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *create_directory(const char *path, const char *old_pathnames) +{ + char *new_pathnames; + char *newdir; + char *semi; + + new_pathnames = ft_strdup(old_pathnames); + temp = new_pathnames; + while (new_pathnames) + { + if ((semi = ft_strchr(new_pathnames, ":"))) + *semi = 0; + if (!is_directory(newdir = ft_str3join(new_pathnames, "/", path))) + ft_strdel(&newdir); + else + break; + new_pathnames += new_pathnames + 1; + } + ft_strdel(temp); + return (newdir); +} diff --git a/42sh/newfile b/42sh/newfile deleted file mode 100644 index e69de29b..00000000 diff --git a/42sh/src/builtin/bt_cd_getpath.c b/42sh/src/builtin/bt_cd_getpath.c new file mode 100644 index 00000000..addbbb06 --- /dev/null +++ b/42sh/src/builtin/bt_cd_getpath.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_cd_getpath.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/25 03:52:52 by ariard #+# #+# */ +/* Updated: 2017/03/25 04:05:06 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int get_cdpath(char *arg) +{ + char *cdpath; + char *target; + + if (!(cdpath = ft_getenv(data_singleton()->env, "CDPATH"))) + { + if (!is_directory(target = ft_str3join(".", "/", arg))) + ft_strdel(&target); + } + else + target = create_directory(arg, cdpath); + return (target); +} diff --git a/42sh/src/builtin/bt_cd_process_dotdot.c b/42sh/src/builtin/bt_cd_process_dotdot.c new file mode 100644 index 00000000..e4043b79 --- /dev/null +++ b/42sh/src/builtin/bt_cd_process_dotdot.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_cd_process_dotdot.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */ +/* Updated: 2017/03/25 04:01:03 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int process_dotdot(char *target) +{ + return (0); +} diff --git a/42sh/father/son/litte_son/file b/42sh/src/builtin/bt_cd_process_symlink.c similarity index 100% rename from 42sh/father/son/litte_son/file rename to 42sh/src/builtin/bt_cd_process_symlink.c diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index b0e75244..48e28ebd 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/25 00:52:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 02:05:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,6 @@ static char *bt_cd_target(char *arg) DG("doing -"); if (!(target = ft_getenv(data_singleton()->env, "OLDPWD"))) SH_ERR(CDERR_2, "OLDPWD"); - DG("found OLDPWD %s", target); } else target = arg; diff --git a/42sh/src/builtin/builtin_new_cd.c b/42sh/src/builtin/builtin_new_cd.c index c053bf9c..3a0fef44 100644 --- a/42sh/src/builtin/builtin_new_cd.c +++ b/42sh/src/builtin/builtin_new_cd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 02:00:40 by ariard #+# #+# */ -/* Updated: 2017/03/25 02:04:47 by ariard ### ########.fr */ +/* Updated: 2017/03/25 04:05:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,61 @@ #define CD_OPT_P (1 << 1) #define HAS_CDOPT_P(x) (x & CD_OPT_P) #define HAS_CDOPT_L(x) (x & CD_OPT_L) +#define CDERR_0 "cd: too many arguments" +#define CDERR_1 "cd %s not set" static t_cliopts g_cdopts[] = { - {'P', NULL, CD_OPT_P, CD_OPT_L, NULL}, - {'L', NULL, CD_OPT_L, CD_OPT_P, NULL}, - {0, NULL, 0, 0, NULL}, + {'P', NULL, CD_OPT_P, CD_OPT_L, NULL, 0}, + {'L', NULL, CD_OPT_L, CD_OPT_P, NULL, 0}, + {0, NULL, 0, 0, NULL, 0}, }; +static char *cd_operand_exist(char *arg) +{ + char *target; + + if (!arg) + { + if (!(target = ft_getenv(data_singleton()->env, "HOME"))) + SH_ERR(CDERR_1, "HOME"); + } + else + target = arg; + return (target); +} + +static char *cd_operand_begin(char *arg) +{ + char *target; + + if (arg && arg[0]) + { + if (arg[0] = '/') + target = arg; + else if (arg[0] = '.') + target = ft_str3join(ft_getenv(&data_singleton()->env, + "PWD"), "/", arg); + else if (ft_strcmp(arg, "-") == 0) + { + if (!(target = ft_getenv(data_singleton()->env, "OLDPWD"))) + SH_ERR(CDERR_2, "OLDPWD"); + } + else + target = get_cdpath(arg); + } + return (target); +} + +void setwd(char *var) +{ + char *cwd; + + cwd = getcwd(NULL, 0); + builtin_setenv(NULL, (char*[]){"cd", var, cwd, NULL}, NULL); + free(cwd); +} + int builtin_cd(const char *path, char *const av[], char *const av[], char *const envp[]) { @@ -35,4 +82,17 @@ int builtin_cd(const char *path, char *const av[], data.flag = CD_OPT_L; 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))) + return (1); + setwd("OLDPWD"); + if (!target) + target = cd_operand_begin(*data.av_data); + if (HAS_CDOPT_P(data.flag)) + process_symlink(target); + else + process_dotdot(target); + setwd("PWD"); + return (0); +} diff --git a/42sh/test.c b/42sh/test.c deleted file mode 100644 index 4a4f1bfd..00000000 --- a/42sh/test.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "stdio.h" - -int main(void) -{ - printf("hello"); - return (0); -} diff --git a/42sh/testlink/litte_son b/42sh/testlink/litte_son deleted file mode 120000 index de9514bb..00000000 --- a/42sh/testlink/litte_son +++ /dev/null @@ -1 +0,0 @@ -father/son/litte_son \ No newline at end of file