return value 0

This commit is contained in:
kinou-p 2022-04-07 19:15:04 +02:00
parent 01d80ba1cd
commit c11e2ebf1c
6 changed files with 82 additions and 93 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/05 18:22:48 by apommier ### ########.fr */
/* Updated: 2022/04/07 16:47:50 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,9 +52,9 @@ typedef struct s_simple {
typedef struct s_command {
int nb_s_cmd;
struct s_simple **s_cmds;
char *outfile;
char *infile;
char *err_file;
//char *outfile;
//char *infile;
int err_var;
struct s_simple *current_s_cmd;
char **path;
} t_cmd;
@ -94,6 +94,9 @@ int find_pwd(t_s_cmd *cmd);
void init_s_cmd(t_s_cmd *cmd, char **env);
int tab_len(char **tab);
int find_len(char *input, int i, char c);
//void lone_export(t_s_cmd *cmd);
//void find_variable(char *variable, t_s_cmd *cmd);
//real builtin
void ft_env(t_s_cmd *cmd, char **env);
void ft_exit(t_s_cmd *cmd);
@ -102,6 +105,7 @@ void ft_unset(t_s_cmd *cmd);
void ft_echo(t_s_cmd *cmd);
void ft_pwd(t_s_cmd *cmd);
void open_directory(t_s_cmd *cmd);//cd
//parse builtin
int is_builtin(char *cmd);
void call_builtin(t_cmd *cmd, char **env);

View File

@ -6,20 +6,32 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/04 21:12:38 by apommier ### ########.fr */
/* Updated: 2022/04/07 17:47:01 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
void crtl_c(int num)
{
printf("\n");
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
}
void sig_quit(int num)
{
ft_putstr_fd("\b \b\b \b", 1);
//printf("quit num= %d\n", num);
//exit(0);
}
void print_prompt(char **path)
{
char *input;
t_cmd *cmd;
int i;
int tmpout = dup(1);
int tmpin = dup(0);
input = 0;
i = 0;
@ -27,94 +39,40 @@ void print_prompt(char **path)
while (1)
{
i = 0;
/*ft_putstr_fd("\033[1;31m~$ \033[0m", 0);
input = get_next_line(0);
while (input[i] != '\n' && input[i])
i++;
if (input[i] == '\n')
input[i] = 0;*/
//input = ft_strjoin(readline("\033[1;31m~$ \033[0m"), 0);
input = readline("\033[1;31m~$ \033[0m");
//if (!ft_strlen(input))
// printf("\n");
add_history(input);
//l_replace_line(0, 1);
//rl_replace_line("\n", 1);
//_on_new_line();
//rl_redisplay();
if (!input)
exit_shell(cmd);
add_history(input);
if (!ft_strcmp("exit", input) && input)
{
//dprintf(2, "exit\n");
rl_clear_history();
//free(input);
exit_shell(cmd);
}
if (ft_strlen(input) && next_space(input, 0) && input)
{
//printf("hre\n");
//add_history(input);
cmd = set_cmd(input, path);
//free(input);
if (cmd)
{
cmd->err_var = 0;
execute(cmd, path);
dup2(tmpout, 1);
dup2(tmpin, 0);
//waitpid(-1, 0, 0);
//free_cmd(cmd);
cmd = 0;
}
//free_cmd(cmd);
//cmd = 0;
//rl_replace_line("test", 0);
}
//else
// dprintf(2, "no exec\n");
//rl_replace_line("\n", 1);
free(input);
}
}
void test(int num)
{
num = 0;
//ft_putstr_fd("\n\033[1;31m~$ \033[0m", 1);
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
//num = 0;
//if (num)
// printf("num = %d\n", num);
//ft_putstr_fd("\b\b ", 2);
//ft_putstr_fd("\n", 2);
//ft_putstr_fd("\033[1;31m~$ \033[0m", 1);
//red();
//normal();
//printf("CRTL-C ? try exit\n");
}
void just_exit(int num)
{
num = 0;
printf("crtl \\n");
exit(0);
}
int main(int ac, char **av, char **path)
{
av = 0;
if (ac > 1)
if (ac != 1)
{
printf("too much arguments\n");
return (0);
}
//ft_putstr_fd("\033[1;31m~$ \033[0m", 1);
printf("---MINISHELL START---\n");
signal(SIGINT, test);
signal(SIGQUIT, just_exit);
signal(SIGINT, crtl_c);
signal(SIGQUIT, sig_quit);
print_prompt(path);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/05 18:26:19 by apommier ### ########.fr */
/* Updated: 2022/04/07 19:08:54 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,34 +48,51 @@ int wait_exit(t_cmd *cmd)
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]);
}
if (WIFEXITED(status))
{
if (!cmd->err_var)
cmd->err_var = WEXITSTATUS(status);
//printf("code= %d\n", cmd->err_var);
}
else if (WIFSIGNALED(status))
{
cmd->err_var = WTERMSIG(status);
if (cmd->err_var == 2)
ft_putstr_fd("\b\b\b\b\b", 1);
}
i++;
}
}
return (ret);
}
void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
{
if (is_builtin(cmd->current_s_cmd->cmd))
cmd->current_s_cmd->child = fork();
if (cmd->current_s_cmd->child == 0)
{
call_builtin(cmd, env);
return ;
}
cmd->current_s_cmd->child = fork();
if (cmd->current_s_cmd->child == 0)
{
if (fdpipe)
close(fdpipe[0]);
dup2(cmd->current_s_cmd->fd[0], 0);
dup2(cmd->current_s_cmd->fd[1], 1);
close(cmd->current_s_cmd->fd[0]);
close(cmd->current_s_cmd->fd[1]);
if (is_builtin(cmd->current_s_cmd->cmd))
{
call_builtin(cmd, env);
exit(0);
}
if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
{
ft_putstr_fd("Minishell: command not found\n", 2);
exit(0);
}
if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env))
dprintf(2, "exec error\n");
ft_putstr_fd("Minishell: exec error\n", 2);
exit(0);
}
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
cmd->err_var = 127;
}
void execute(t_cmd *cmd, char **env)
@ -88,13 +105,14 @@ void execute(t_cmd *cmd, char **env)
int i;
i = 0;
fdpipe[1] = -1;
tmpin = dup(0);
tmpout= dup(1);
tmpout = dup(1);
if (cmd->current_s_cmd->infile)
{
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("open error\n");
printf("Minishell: open error\n");
}
else
fdin=dup(tmpin);
@ -105,7 +123,7 @@ void execute(t_cmd *cmd, char **env)
{
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("open error\n");
printf("Minishell: open error\n");
}
if (i == cmd->nb_s_cmd - 1)
{
@ -116,7 +134,10 @@ void execute(t_cmd *cmd, char **env)
fdout=dup(tmpout);
cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout;
exec_cmd(cmd, env, 0);
if (is_builtin(cmd->current_s_cmd->cmd))
call_builtin(cmd, env);
else
exec_cmd(cmd, env, 0);
}
else
{
@ -134,9 +155,11 @@ void execute(t_cmd *cmd, char **env)
exec_cmd(cmd, env, fdpipe);
close(cmd->current_s_cmd->fd[0]);
}
close(fdpipe[1]);
if (fdpipe[1] > 0)
close(fdpipe[1]);
i++;
cmd->current_s_cmd = cmd->s_cmds[i];
//printf("cmd->err_var= %d\n", cmd->err_var);
}
close_pipe(cmd);
wait_exit(cmd);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
/* Updated: 2022/03/11 17:05:20 by apommier ### ########.fr */
/* Updated: 2022/04/07 14:00:58 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
void exit_shell(t_cmd *cmd)
{
ft_putstr_fd("exit\n", 1);
cmd = 0;
exit(1);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/04/05 18:23:49 by apommier ### ########.fr */
/* Updated: 2022/04/07 17:26:11 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,13 +25,15 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index)
s_cmd = malloc(sizeof(t_s_cmd));
if (!s_cmd)
return (0);
//s_cmd->infile = 0;
//s_cmd->outfile = 0;
s_cmd->args = 0;
s_cmd->infile = 0;
s_cmd->outfile = 0;
s_cmd->args = 0;
//printf("before redirect\n");
line = set_redirection(s_cmd, line, index);//SET REDIRECTION
split_line = ft_split(line, ' ');
print_double_fd(split_line, 0);
//print_double_fd(split_line, 0);
if (!is_builtin(split_line[0]))
s_cmd->cmd = get_command(split_line, cmd->path);
else
@ -89,8 +91,9 @@ t_cmd *set_cmd(char *input, char **env)
return (0);
cmd->s_cmds[double_size(cmds)] = 0;
cmd->path = get_path(env);
cmd->outfile = 0;
cmd->infile = 0;
cmd->err_var = 0;
//cmd->outfile = 0;
//cmd->infile = 0;
cmd->nb_s_cmd = double_size(cmds);
cmd = split_cmd(cmd, cmds, env); //split each cmd into args in s_cmd
free(cmds);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/04/03 21:59:51 by apommier ### ########.fr */
/* Updated: 2022/04/06 16:18:01 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -253,7 +253,7 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
int i;
i = 0;
printf("enter redirection\n");
// printf("enter redirection\n");
while (line[i])
{
if(line[i] == '<')