did single command exec
This commit is contained in:
124
exec.c
124
exec.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/13 16:23:01 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/16 14:11:39 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
|||||||
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
||||||
g_return_code = print_env(env);
|
g_return_code = print_env(env);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
||||||
ft_exit(parsed_cmd, env);
|
exit_bt(parsed_cmd, env);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
||||||
g_return_code = pwd();
|
g_return_code = pwd();
|
||||||
else
|
else
|
||||||
@ -29,9 +29,127 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
|||||||
return (STDOUT_FILENO);
|
return (STDOUT_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ft_exit(t_cmd *parsed_cmd, t_env *env, int exit_code)
|
||||||
|
{
|
||||||
|
free_cmd(parsed_cmd);
|
||||||
|
free_env(env);
|
||||||
|
exit(exit_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_args_count(t_cmd *parsed_cmd)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
cur_cmd = parsed_cmd;
|
||||||
|
while (cur_cmd->next != 0 && cur_cmd->type == ARG)
|
||||||
|
{
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **split_paths_from_env(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *cur_env_var;
|
||||||
|
int path_in_envp;
|
||||||
|
|
||||||
|
path_in_envp = 0;
|
||||||
|
cur_env_var = env;
|
||||||
|
while (cur_env_var->next != 0)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(cur_env_var->name, "PATH"))
|
||||||
|
{
|
||||||
|
path_in_envp = 1;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
cur_env_var = cur_env_var->next;
|
||||||
|
}
|
||||||
|
if (!path_in_envp)
|
||||||
|
{
|
||||||
|
// error: PATH not found
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (ft_split(cur_env_var->value, ':'));
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_cmd_path(t_cmd *cmd, t_env *env, char **paths, int *found)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
char *path;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (paths[i])
|
||||||
|
{
|
||||||
|
path = paths[i];
|
||||||
|
tmp = ft_strjoin(path, "/");
|
||||||
|
if (!tmp)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
path = ft_strjoin(tmp, cmd->token);
|
||||||
|
if (!path)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
free(tmp);
|
||||||
|
if (access(path, X_OK) != -1)
|
||||||
|
{
|
||||||
|
*found = 1;
|
||||||
|
free(cmd->token);
|
||||||
|
cmd->token = path;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_cmd_path(t_cmd *cmd, t_env *env)
|
||||||
|
{
|
||||||
|
char **paths;
|
||||||
|
int found;
|
||||||
|
|
||||||
|
found = 0;
|
||||||
|
if (access(cmd->token, X_OK != -1))
|
||||||
|
found = 1;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
paths = split_paths_from_env(env);
|
||||||
|
if (!paths)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
find_cmd_path(cmd, env, paths, &found);
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
// error message: cmd->token command not found
|
||||||
|
free(cmd->token);
|
||||||
|
cmd->token = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void exec_command(t_cmd *parsed_cmd, t_env *env)
|
void exec_command(t_cmd *parsed_cmd, t_env *env)
|
||||||
{
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
int args_count;
|
||||||
|
char **cmd_args;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (!parsed_cmd || exec_builtin(parsed_cmd, env))
|
if (!parsed_cmd || exec_builtin(parsed_cmd, env))
|
||||||
return ;
|
return ;
|
||||||
ft_printf("not a builtin\n");
|
cur_cmd = parsed_cmd;
|
||||||
|
args_count = get_args_count(parsed_cmd);
|
||||||
|
cmd_args = calloc(args_count, sizeof(char *));
|
||||||
|
if (!cmd_args)
|
||||||
|
ft_exit(parsed_cmd, env, 1);
|
||||||
|
i = 0;
|
||||||
|
while (i < args_count)
|
||||||
|
{
|
||||||
|
cmd_args[i] = cur_cmd->token;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
get_cmd_path(parsed_cmd, env);
|
||||||
|
execve(parsed_cmd->token, cmd_args, env_to_char_tab(env));
|
||||||
}
|
}
|
||||||
|
4
exit.c
4
exit.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/14 12:44:03 by tomoron ### ########.fr */
|
/* Updated: 2024/02/16 13:07:01 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ void print_numeric_arg_err(char *arg)
|
|||||||
ft_putstr_fd(": numeric argument required\n", 2);
|
ft_putstr_fd(": numeric argument required\n", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_exit(t_cmd *args, t_env *env)
|
void exit_bt(t_cmd *args, t_env *env)
|
||||||
{
|
{
|
||||||
t_cmd *start;
|
t_cmd *start;
|
||||||
int exit_code;
|
int exit_code;
|
||||||
|
Reference in New Issue
Block a user