From ed45800a6ad9d4cfbd73832d53e4e26bb7645054 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 19 Jul 2020 16:07:08 +0200 Subject: Fixing cd error messages, Fixing glob on links --- .travis.yml | 14 -------------- README.md | 10 +++++----- minishell_test | 2 +- src/builtin/cd.c | 39 ++++++++++++++++++++++++++++++++------- src/error.c | 5 +++-- src/eval/cmd.c | 17 +++++++++++++++-- src/eval/exec.c | 22 +++++++++++----------- src/main.c | 15 ++++++++++++--- src/ms_glob.c | 10 +++++++--- src/utils.c | 5 +++-- 10 files changed, 89 insertions(+), 50 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2697f90..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -os: linux - -language: c - -git: - submodules: true - -python: - - "3.7" - -env: - - LANG=en_US.UTF-8 - -script: make all && cd minishell_test && python3 main.py; cat result.log diff --git a/README.md b/README.md index a7bab3f..d9f52cd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# minishell [![Build Status](https://travis-ci.com/HappyTramp/minishell.svg?token=ZspKVUuPUQ73ZVD6J2w4&branch=master)](https://travis-ci.com/HappyTramp/minishell) +# minishell minishell project of school 42 @@ -17,16 +17,16 @@ You can then read the man pages in ./doc/man or open ./doc/html/index.html in yo ### Mandatory -- [ ] Show a prompt when waiting for a new command +- [x] Show a prompt when waiting for a new command - [x] Search and launch the right executable (based on the *PATH* variable or by using relative or absolute path) like in bash - [ ] It must implement the builtins like in bash: - [x] `echo` with option `-n` - [ ] `cd` without `-` option - [ ] `pwd` without any options - - [ ] `export` without any options - - [ ] `unset` without any options + - [x] `export` without any options + - [x] `unset` without any options - [ ] `env` without any options and any arguments - - [ ] `exit` without any options + - [x] `exit` without any options - [x] `;` in the command should separate commands like in bash - [x] `'` and `"` should work like in bash except for multiline commands - [ ] Redirections `<` `>` `>>` should work like in bash except for file descriptor aggregation diff --git a/minishell_test b/minishell_test index 3a2214f..d589dd6 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit 3a2214f5f6bf3d33166a57912c9a8d18fef808c7 +Subproject commit d589dd61e1c2bfab8572a202244d86da283e1648 diff --git a/src/builtin/cd.c b/src/builtin/cd.c index cdcdaa4..2377ce7 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/06/17 12:36:29 by nahaddac ### ########.fr */ +/* Updated: 2020/07/19 15:55:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,16 +19,41 @@ int builtin_cd(char **argv, t_env env) { - char buf[PATH_MAX]; + char buf[PATH_MAX]; + char *home; (void)env; - if (argv[1] == NULL) + if (argv[1] != NULL && argv[2] != NULL) + { + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putendl_fd(": cd: too many arguments", STDERR_FILENO); return (1); - if (chdir(argv[1]) != -1) + } + if (argv[1] != NULL && argv[1][0] == '\0') + return (0); + if (argv[1] == NULL) { - if (!(getcwd(buf, PATH_MAX))) - return(1); - env_export(env, "PWD", buf); + if ((home = env_search(env, "HOME")) == NULL) + { + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putendl_fd(": cd: HOME not set", STDERR_FILENO); + return (1); + } + argv[1] = home; } + errno = 0; + if (chdir(argv[1]) == -1) + { + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putstr_fd(": cd: ", STDERR_FILENO); + ft_putstr_fd(argv[1], STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + ft_putendl_fd(strerror(errno), STDERR_FILENO); + return (1); + } + if (!(getcwd(buf, PATH_MAX))) + return (1); + if (env_export(env, "PWD", buf) == NULL) + return (2); // FIXME malloc error recognition in builtins and cmd return (0); } diff --git a/src/error.c b/src/error.c index 13a1443..fe5b4b1 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/07/15 13:11:13 by charles ### ########.fr */ +/* Updated: 2020/07/19 15:34:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,8 @@ void error_eval_put(enum e_error id, char *unexpected) t_error *err; err = st_error_get(id); - ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); ft_putstr_fd(unexpected, STDERR_FILENO); ft_putstr_fd(": ", STDERR_FILENO); if (err->msg == NULL) diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 1953325..1e59347 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/07/17 11:43:59 by charles ### ########.fr */ +/* Updated: 2020/07/18 09:31:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ int fork_wrap( int forked_cmd(void *void_param) { t_fork_param_cmd *param; + int ret; param = void_param; ft_vecpop(param->env_local, NULL); @@ -63,7 +64,19 @@ int forked_cmd(void *void_param) if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else - return (execve(param->exec_path, param->argv, (char**)param->env->data)); + { + ret = execve(param->exec_path, param->argv, (char**)param->env->data); + if (ret == -1) + { + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + ft_putstr_fd(param->exec_path, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + ft_putendl_fd(strerror(errno), STDERR_FILENO); + ret = 126; + } + return (ret); + } } int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) diff --git a/src/eval/exec.c b/src/eval/exec.c index e41f994..fb97bdc 100644 --- a/src/eval/exec.c +++ b/src/eval/exec.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:06:11 by charles #+# #+# */ -/* Updated: 2020/04/01 17:06:12 by charles ### ########.fr */ +/* Updated: 2020/07/18 09:39:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,12 @@ ** \return True if valid */ -bool exec_is_path(char *exec_name) -{ - return (ft_strncmp(exec_name, "../", 3) == 0 - || ft_strncmp(exec_name, "./", 2) == 0 - || ft_strncmp(exec_name, "/", 1) == 0); -} +/* bool exec_is_path(char *exec_name) */ +/* { */ +/* return (ft_strncmp(exec_name, "../", 3) == 0 */ +/* || ft_strncmp(exec_name, "./", 2) == 0 */ +/* || ft_strncmp(exec_name, "/", 1) == 0); */ +/* } */ /* ** \brief Check if executable path is valid @@ -60,13 +60,13 @@ char *exec_search_path(t_path path, char *path_var, char *exec_name) { char *exec_path; - if (exec_is_path(exec_name)) + if (ft_strchr(exec_name, '/') != NULL) // TODO test recursive link return (exec_name); - // try current first + // TODO if PATH contain empty path, consider current directory files as cmd if ((exec_path = ft_htget(path, exec_name)) == NULL) { - if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht - return (NULL); + if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht + return (NULL); // FIXME need to distiguish between malloc error and cmd not found error if ((exec_path = ft_htget(path, exec_name)) == NULL) return (NULL); } diff --git a/src/main.c b/src/main.c index 332bfee..3d910f8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/07/18 08:58:52 by charles ### ########.fr */ +/* Updated: 2020/07/19 15:48:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,11 +33,10 @@ void ast_print(int level, t_ast *ast); ** cmd variable preprocess ** PATH with no permission, link and other file system fun stuff ** escape lexer -** escape split preprocessing +** escape split preprocessing (escaped spaces) ** signal on whole line instead of single command ** parsing error ** env local to current minishell process -** exit */ char *g_basename = "minishell"; @@ -61,6 +60,16 @@ int main(int argc, char **argv, char **envp) else g_basename = last_slash + 1; + char *pwd_var; + + if ((pwd_var = env_search(env, "PWD")) == NULL) + { + char buf[PATH_MAX] = {0}; + if (!(getcwd(buf, PATH_MAX))) + return(1); + env_export(env, "PWD", buf); + } + if (argc == 3 && ft_strcmp(argv[1], "-l") == 0) { t_ftlst *lex_out = lexer(ft_strdup(argv[2])); diff --git a/src/ms_glob.c b/src/ms_glob.c index 7603286..19be81c 100644 --- a/src/ms_glob.c +++ b/src/ms_glob.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/05 11:44:07 by charles #+# #+# */ -/* Updated: 2020/07/15 12:12:02 by charles ### ########.fr */ +/* Updated: 2020/07/19 15:21:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,10 @@ static int glob_iter_subdir( size_t i; ft_strcat3(ft_strcpy(subdir_name, dirname), "/", entry->d_name); + errno = 0; chdir(subdir_name); + if (errno == EACCES) + return (0); subdir_matches = glob_matches(subdir_pattern); chdir(dirname); if (subdir_matches == NULL) @@ -86,9 +89,10 @@ static int glob_iter( subdir_pattern[-1] = '/'; return (0); } + /* printf("%s %s\n", dirname, entry->d_name); */ if (subdir_pattern != NULL) { - if (entry->d_type != DT_DIR) + if (entry->d_type != DT_DIR && entry->d_type != DT_LNK) { subdir_pattern[-1] = '/'; return (0); @@ -185,7 +189,7 @@ char *ms_glob(char *pattern) } /* -** \brief Wrapper around `ms_glob` which free `pattern` for convinience +** \brief Wrapper around `ms_glob` which free `pattern` for convenience */ char *ms_globf(char *pattern) diff --git a/src/utils.c b/src/utils.c index e2dd765..3a27290 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/07/16 09:16:57 by charles ### ########.fr */ +/* Updated: 2020/07/19 14:53:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,9 @@ int utils_directory_iter( DIR *dir; struct dirent *entry; + errno = 0; if ((dir = opendir(dirname)) == NULL) // add fail safe - return (0); + return (errno == EACCES ? -2 : -1); while ((entry = readdir(dir)) != NULL) if (f(dirname, entry, param) == -1) { -- cgit