mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-05-10 22:58:45 +02:00
「✏️」 norm: Normed everything and leak erased from the surface of the earth and what the fuck am i doing at this hour coding i shoud be sleeping 1:30am wtf god damn please give me a bed and fuck you yosyo 🖕
This commit is contained in:
@ -6,29 +6,14 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/07/06 18:12:25 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/07/10 01:18:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "builtins.h"
|
||||
#include "error_msg.h"
|
||||
|
||||
int is_in_builtins(char *cmd)
|
||||
{
|
||||
int i;
|
||||
static const char *builtins[] = {"exit", "cd", "unset", "export", "echo",
|
||||
"pwd", "env", NULL};
|
||||
|
||||
i = 0;
|
||||
while (builtins[i])
|
||||
{
|
||||
if (!ft_strcmp(cmd, builtins[i]))
|
||||
return (i + 1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
#include "execution.h"
|
||||
|
||||
void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
|
||||
{
|
||||
@ -51,157 +36,111 @@ void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
|
||||
ft_env(env_t);
|
||||
if (i == 1)
|
||||
exit(ft_atoi(argv[1]));
|
||||
if (i > 4)
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
execve(cmd, argv, env);
|
||||
}
|
||||
|
||||
char *get_cmd_local_path(char *cmd, t_env *env)
|
||||
void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
|
||||
{
|
||||
char *path;
|
||||
|
||||
path = env_get_value("PWD", env);
|
||||
if (!path)
|
||||
return (NULL);
|
||||
path = ft_strjoin_free_s1(path, "/");
|
||||
if (!path)
|
||||
return (NULL);
|
||||
path = ft_strjoin_free_s1(path, cmd);
|
||||
return (path);
|
||||
}
|
||||
|
||||
int switch_cmd_path(t_cmd *cmd, t_env *env)
|
||||
{
|
||||
if (is_in_builtins(cmd->cmd))
|
||||
return (0);
|
||||
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
|
||||
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
|
||||
else if (cmd->cmd[0] != '/')
|
||||
cmd->cmd = get_path(env_get_value("PATH", env), cmd->cmd);
|
||||
if (!(cmd->cmd))
|
||||
return (-1);
|
||||
return (0);
|
||||
exec.status = dup2(cmd->infile, STDIN_FILENO);
|
||||
if (cmd->infile != STDIN_FILENO)
|
||||
close(cmd->infile);
|
||||
if (exec.status == -1)
|
||||
exit(-1);
|
||||
exec.status = dup2(cmd->outfile, STDOUT_FILENO);
|
||||
if (cmd->outfile != STDOUT_FILENO)
|
||||
close(cmd->outfile);
|
||||
if (exec.pipe_fd[0] != -1)
|
||||
close(exec.pipe_fd[0]);
|
||||
if (exec.pipe_fd[0] != -1)
|
||||
close(exec.pipe_fd[1]);
|
||||
if (exec.status != -1)
|
||||
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
|
||||
{
|
||||
t_exec exec;
|
||||
int fork_pid;
|
||||
int status;
|
||||
char *input;
|
||||
|
||||
input = ft_strdup(cmd->cmd);
|
||||
status = switch_cmd_path(cmd, env_t);
|
||||
if (status == -1 || !input || (access(cmd->cmd, X_OK) && !is_in_builtins(cmd->cmd)))
|
||||
exec.pipe_fd[0] = pipe_fd[0];
|
||||
exec.pipe_fd[1] = pipe_fd[1];
|
||||
exec.status = switch_cmd_path(cmd, env_t);
|
||||
if (exec.status == -1 || !input || (access(cmd->cmd, X_OK) \
|
||||
&& !is_in_builtins(cmd->cmd)))
|
||||
{
|
||||
printf("minishell : command not found: %s\n", input);
|
||||
free(input);
|
||||
return (-1);
|
||||
}
|
||||
if (is_in_builtins(cmd->cmd) && is_in_builtins(cmd->cmd) < 5)
|
||||
free(input);
|
||||
if (is_in_builtins(cmd->cmd) > 0)
|
||||
{
|
||||
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
|
||||
return (0);
|
||||
}
|
||||
fork_pid = fork();
|
||||
if (!fork_pid)
|
||||
{
|
||||
status = dup2(cmd->infile, STDIN_FILENO);
|
||||
if (cmd->infile != STDIN_FILENO)
|
||||
close(cmd->infile);
|
||||
if (status == -1)
|
||||
exit(-1);
|
||||
status = dup2(cmd->outfile, STDOUT_FILENO);
|
||||
if (cmd->outfile != STDOUT_FILENO)
|
||||
close(cmd->outfile);
|
||||
if (pipe_fd[0] != -1)
|
||||
close(pipe_fd[0]);
|
||||
if (pipe_fd[0] != -1)
|
||||
close(pipe_fd[1]);
|
||||
if (status != -1)
|
||||
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
|
||||
exit(-1);
|
||||
}
|
||||
__fork_single_cmd(cmd, env, env_t, exec);
|
||||
return (fork_pid);
|
||||
}
|
||||
|
||||
void print_return_value(int return_code)
|
||||
t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
|
||||
{
|
||||
int code;
|
||||
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
|
||||
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
|
||||
"Floating point exception", "Killed", "User defined signal 1",
|
||||
"Segmentation fault", "User defined signal 2", 0,
|
||||
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
|
||||
"Stopped", "Stopped", 0, "CPU time limit exceeded",
|
||||
"File size limit exceeded", "Virtual time expired",
|
||||
"Profiling timer expired", "I/O possible", "Power failure",
|
||||
"Bad system call"};
|
||||
|
||||
if (!WIFEXITED(return_code))
|
||||
{
|
||||
if (WIFSIGNALED(return_code))
|
||||
{
|
||||
code = WTERMSIG(return_code);
|
||||
if (!sigmsg[code])
|
||||
return ;
|
||||
if (WCOREDUMP(return_code))
|
||||
printf("minishell : %s %s\n", sigmsg[code], ERROR_COREDUMP);
|
||||
else
|
||||
printf("minishell : %s\n", sigmsg[code]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int exec_split_cmd(t_list *list_cmd, t_env *env)
|
||||
{
|
||||
char **env_array;
|
||||
int status;
|
||||
int return_code;
|
||||
int i;
|
||||
int pipe_fd[2];
|
||||
|
||||
env_array = env_get(env);
|
||||
pipe_fd[0] = -1;
|
||||
pipe_fd[1] = -1;
|
||||
if (!env_array)
|
||||
return (-1);
|
||||
i = 1;
|
||||
while (list_cmd->next)
|
||||
{
|
||||
status = pipe(pipe_fd);
|
||||
if (status)
|
||||
return (-1);
|
||||
exec.status = pipe(exec.pipe_fd);
|
||||
if (exec.status)
|
||||
return ((t_exec){exec.env_array, -1, exec.i, \
|
||||
{exec.pipe_fd[0], exec.pipe_fd[1]}});
|
||||
if (((t_cmd *)(list_cmd->content))->outfile == STDOUT_FILENO)
|
||||
((t_cmd *)(list_cmd->content))->outfile = pipe_fd[1];
|
||||
((t_cmd *)(list_cmd->content))->outfile = exec.pipe_fd[1];
|
||||
if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO)
|
||||
((t_cmd *)(list_cmd->next->content))->infile = pipe_fd[0];
|
||||
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd);
|
||||
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->outfile);
|
||||
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->infile);
|
||||
if (status != -1)
|
||||
i++;
|
||||
((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0];
|
||||
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
|
||||
env, exec.pipe_fd);
|
||||
__close(list_cmd->content);
|
||||
if (exec.status != -1)
|
||||
exec.i++;
|
||||
list_cmd = list_cmd->next;
|
||||
}
|
||||
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd);
|
||||
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
|
||||
env, exec.pipe_fd);
|
||||
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->outfile);
|
||||
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->infile);
|
||||
ft_free("a", &env_array);
|
||||
if (status == -1)
|
||||
i--;
|
||||
if (i < 1)
|
||||
return (exec);
|
||||
}
|
||||
|
||||
int exec_split_cmd(t_list *list_cmd, t_env *env)
|
||||
{
|
||||
t_exec exec;
|
||||
int return_code;
|
||||
|
||||
exec.env_array = env_get(env);
|
||||
if (!exec.env_array)
|
||||
return (-1);
|
||||
return_code = 0;
|
||||
exec.pipe_fd[0] = -1;
|
||||
exec.pipe_fd[1] = -1;
|
||||
exec.i = 1;
|
||||
exec = exec_pipe(exec, list_cmd, env);
|
||||
if (exec.env_array)
|
||||
ft_free("a", &exec.env_array);
|
||||
if (exec.status == -1)
|
||||
exec.i--;
|
||||
if (exec.i < 1)
|
||||
return (0);
|
||||
if (status != 0)
|
||||
waitpid(status, &return_code, 0);
|
||||
while (i - 1)
|
||||
{
|
||||
waitpid(-1, NULL, 0);
|
||||
i--;
|
||||
}
|
||||
if (exec.status != 0)
|
||||
waitpid(exec.status, &return_code, 0);
|
||||
if (!return_code)
|
||||
return_code = 0;
|
||||
__wait(exec.i);
|
||||
print_return_value(return_code);
|
||||
return (0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user