aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-19 16:07:08 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-19 16:07:08 +0200
commited45800a6ad9d4cfbd73832d53e4e26bb7645054 (patch)
treed4691054bba3a7f022eaef0c33763e8322477f7f /src
parent6e97753fa7307cfc0a2ea426edaac6c683e916f7 (diff)
downloadminishell-ed45800a6ad9d4cfbd73832d53e4e26bb7645054.tar.gz
minishell-ed45800a6ad9d4cfbd73832d53e4e26bb7645054.tar.bz2
minishell-ed45800a6ad9d4cfbd73832d53e4e26bb7645054.zip
Fixing cd error messages, Fixing glob on links
Diffstat (limited to 'src')
-rw-r--r--src/builtin/cd.c39
-rw-r--r--src/error.c5
-rw-r--r--src/eval/cmd.c17
-rw-r--r--src/eval/exec.c22
-rw-r--r--src/main.c15
-rw-r--r--src/ms_glob.c10
-rw-r--r--src/utils.c5
7 files changed, 83 insertions, 30 deletions
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 <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{