feature: added env, setenv & unsetenv builtins
added -i option & command execution to env added setenv & unsetenv builtins
This commit is contained in:
144
srcs/ms_exec.c
144
srcs/ms_exec.c
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
|
||||
/* Updated: 2019/09/27 16:52:50 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/10/10 14:05:31 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -36,26 +36,22 @@ char *check_path_slash(char *exec)
|
||||
int i;
|
||||
struct stat info;
|
||||
|
||||
if (exec && ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1)
|
||||
if ((i = access(exec, F_OK)) != 0)
|
||||
put_error(exec, "no such file or directory");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
if ((i = stat(exec, &info)) != 0)
|
||||
put_error(exec, "can't determine info");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
if (!S_ISREG(info.st_mode))
|
||||
{
|
||||
if ((i = access(exec, F_OK)) != 0)
|
||||
put_error(exec, "no such file or directory");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
if ((i = stat(exec, &info)) != 0)
|
||||
put_error(exec, "can't determine info");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
if (!S_ISREG(info.st_mode))
|
||||
{
|
||||
put_error(exec, "not an executable file");
|
||||
return (exec);
|
||||
}
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
put_error(exec, "permission denied");
|
||||
put_error(exec, "not an executable file");
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
put_error(exec, "permission denied");
|
||||
return (exec);
|
||||
}
|
||||
|
||||
char *check_path_dot(char *exec, t_env *env)
|
||||
@@ -64,31 +60,28 @@ char *check_path_dot(char *exec, t_env *env)
|
||||
char *tmp;
|
||||
char *ret;
|
||||
|
||||
if (exec && exec[0] == '.')
|
||||
{
|
||||
if ((path = lstgetelem("PWD", env)) == NULL
|
||||
if ((path = lstgetelem("PWD", env)) == NULL
|
||||
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
|
||||
{
|
||||
put_error(exec, "memory error");
|
||||
return (exec);
|
||||
}
|
||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
if (ret == NULL)
|
||||
return (exec);
|
||||
if (access(ret, F_OK) == 0)
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
return (ret);
|
||||
put_error(exec, "permission denied");
|
||||
ft_strdel(&ret);
|
||||
return (exec);
|
||||
}
|
||||
put_error(exec, "no such file or directory");
|
||||
ft_strdel(&ret);
|
||||
{
|
||||
put_error(exec, "memory error");
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
if (ret == NULL)
|
||||
return (exec);
|
||||
if (access(ret, F_OK) == 0)
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
return (ret);
|
||||
put_error(exec, "permission denied");
|
||||
ft_strdel(&ret);
|
||||
return (exec);
|
||||
}
|
||||
put_error(exec, "no such file or directory");
|
||||
ft_strdel(&ret);
|
||||
return (exec);
|
||||
}
|
||||
|
||||
char *check_path(char *exec, t_env *env)
|
||||
@@ -100,39 +93,56 @@ char *check_path(char *exec, t_env *env)
|
||||
char *ret;
|
||||
|
||||
ret = NULL;
|
||||
if ((ret = check_path_slash(exec)) != NULL
|
||||
|| (ret = check_path_dot(exec, env)) != NULL)
|
||||
return (ret);
|
||||
if ((path = lstgetelem("PATH", env)) == NULL
|
||||
|| path->val == NULL
|
||||
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (path_elems[i])
|
||||
if (exec)
|
||||
{
|
||||
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
if (tmp == NULL)
|
||||
break ;
|
||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||
if (ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1)
|
||||
{
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
break ;
|
||||
if ((ret = check_path_slash(exec)) != NULL)
|
||||
return (ret);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
ft_strdel(&tmp);
|
||||
if (access(ret, F_OK) == 0)
|
||||
if (exec[0] == '.')
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
if ((ret = check_path_dot(exec, env)) != NULL)
|
||||
return (ret);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
if ((path = lstgetelem("PATH", env)) == NULL
|
||||
|| path->val == NULL
|
||||
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
||||
{
|
||||
put_error(exec, "could not resolve path");
|
||||
return (NULL);
|
||||
}
|
||||
i = 0;
|
||||
while (path_elems[i])
|
||||
{
|
||||
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
if (tmp == NULL)
|
||||
break ;
|
||||
put_error(exec, "permission denied");
|
||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||
{
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
break ;
|
||||
}
|
||||
ft_strdel(&tmp);
|
||||
if (access(ret, F_OK) == 0)
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
break ;
|
||||
put_error(exec, "permission denied");
|
||||
}
|
||||
ft_strdel(&ret);
|
||||
i++;
|
||||
}
|
||||
ft_strdel(&ret);
|
||||
i++;
|
||||
if (path_elems[i] == NULL)
|
||||
put_error(exec, "command not found");
|
||||
ft_del_words_tables(&path_elems);
|
||||
}
|
||||
if (path_elems[i] == NULL)
|
||||
put_error(exec, "command not found");
|
||||
ft_del_words_tables(&path_elems);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user