can't figure out why it returns wrong value

This commit is contained in:
mdev9
2024-03-24 10:00:09 +01:00
parent bedf72b553
commit a58366f827
8 changed files with 28 additions and 194 deletions

View File

@ -6,11 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
/* Updated: 2024/03/23 19:04:17 by tomoron ### ########.fr */
/* Updated: 2024/03/24 09:59:16 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <sys/wait.h>
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
{
@ -43,7 +44,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
void exec_command(t_msh *msh, int i, int cmd_count)
{
g_return_code = 0;
//g_return_code = 0;
msh->fds[i] = ft_calloc(2, sizeof(int *));
if (!msh->fds[i])
ft_exit(msh, 1);
@ -85,9 +86,15 @@ void exec_commands(t_msh *msh)
while (i < cmd_count)
{
waitpid(msh->pids[i], &status, 0);
g_return_code = WEXITSTATUS(status);
//if (/*!g_return_code*/i == cmd_count - 1)
//{
//ft_printf_fd(2, "old: %d, new: %d\n", g_return_code, WEXITSTATUS(status));
//ft_printf_fd(2, "replace value\n");
//g_return_code = WEXITSTATUS(status);
//}
i++;
}
g_return_code = WEXITSTATUS(status);
i = 0;
while (i < cmd_count)
{

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/03/23 19:44:47 by tomoron ### ########.fr */
/* Updated: 2024/03/24 08:49:17 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -77,13 +77,13 @@ void delete_from_env(t_msh *msh, char *name)
tmp_env = msh->env;
prev = 0;
while(tmp_env)
while (tmp_env)
{
if(!strcmp(name, tmp_env->name))
if (!strcmp(name, tmp_env->name))
{
free(tmp_env->name);
free(tmp_env->value);
if(!prev)
if (!prev)
msh->env = tmp_env->next;
else
prev->next = tmp_env->next;
@ -100,9 +100,9 @@ int ft_unset(t_msh *msh)
t_cmd *cmd;
cmd = msh->cmds;
if(cmd)
if (cmd)
cmd = cmd->next;
while(cmd && cmd->type == ARG)
while (cmd && cmd->type == ARG)
{
delete_from_env(msh, cmd->token);
cmd = cmd->next;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/03/05 19:19:33 by marde-vr ### ########.fr */
/* Updated: 2024/03/24 09:56:01 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,10 +35,11 @@ void open_input_file(t_msh *msh, t_cmd **cur_cmd)
{
if (msh->in_fd != 0)
close(msh->in_fd);
msh->in_fd = open((*cur_cmd)->next->token, O_RDONLY | O_CREAT);
msh->in_fd = open((*cur_cmd)->next->token, O_RDONLY);
if (msh->in_fd == -1 && !g_return_code)
{
ft_printf_fd(2, "minishell: %s: ", (*cur_cmd)->next->token);
//ft_printf_fd(2, "\nset return_code to 1\n");
perror("");
g_return_code = 1;
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/03/23 16:37:25 by tomoron ### ########.fr */
/* Updated: 2024/03/24 09:53:43 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -66,6 +66,7 @@ typedef struct s_msh
enum e_token_type out_type;
int in_fd;
int out_fd;
int locked_return_code;
} t_msh;
extern int g_return_code;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/03/23 19:23:50 by marde-vr ### ########.fr */
/* Updated: 2024/03/24 08:50:11 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -54,7 +54,7 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
else if (**cmd == '~' && !*in_quote && !*in_dquote)
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|| (**cmd != '\'' && **cmd != '"'))
|| (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd;
(*cmd)++;
}

176
srcs/pids
View File

@ -1,176 +0,0 @@
exit.c: cur_cmd = msh->cmds->next;
exit.c: if(msh->fds)
exit.c: cmd_count = get_cmd_count(msh->cmds);
exit.c: free(msh->fds[cmd_count - 1]);
here_doc.c: write(msh->in_fd, line, ft_strlen(line));
here_doc.c: msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
here_doc.c: if (msh->in_fd == -1)
here_doc.c: close(msh->in_fd);
here_doc.c: msh->in_fd = open(here_doc_file, O_RDWR, 0644);
here_doc.c: if (msh->in_fd == -1)
here_doc.c: close(msh->in_fd);
here_doc.c: msh->in_fd = open(here_doc_file, O_RDWR, 0644);
here_doc.c: if (msh->in_fd == -1)
unalias.c: alias = msh->aliases;
unalias.c: if (alias->next && msh->cmds->next
unalias.c: && !ft_strcmp(alias->next->name, msh->cmds->next->token))
unalias.c: if (msh->cmds->next && !ft_strcmp(msh->cmds->next->token, "-a"))
unalias.c: free_alias(msh->aliases);
unalias.c: msh->aliases = 0;
unalias.c: if (msh->cmds->next && msh->cmds->next->type == ARG)
unalias.c: msh->cmds->next->token);
pipe.c: if (msh->fds[i - 1][0] > 2)
pipe.c: close(msh->fds[i - 1][0]);
pipe.c: if (msh->fds[i - 1][1] > 2)
pipe.c: close(msh->fds[i - 1][1]);
pipe.c: if (msh->fds[i][0] > 2)
pipe.c: close(msh->fds[i][0]);
pipe.c: if (msh->fds[i][1] > 2)
pipe.c: close(msh->fds[i][1]);
pipe.c: if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
pipe.c: || !ft_strcmp(msh->cmds->token, "alias")
pipe.c: || !ft_strcmp(msh->cmds->token, "unalias")
pipe.c: || !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh)))
pipe.c: free(msh->fds[i]);
pipe.c: msh->fds[i] = 0;
pipe.c: if (msh->cmds->token)
pipe.c: env = env_to_char_tab(msh->env);
pipe.c: execve(msh->cmds->token, cmd_args, env);
pipe.c: if ((msh->in_type != ARG && msh->in_type != PIPE)
pipe.c: || (msh->in_type == PIPE && i > 0))
pipe.c: if (msh->out_type == PIPE || msh->out_type == RED_O
pipe.c: || msh->out_type == RED_O_APP)
pipe.c: free(msh->fds[i]);
pipe.c: msh->fds[i] = 0;
pipe.c: if (msh->fds[i - 1][0] > 2)
pipe.c: close(msh->fds[i - 1][0]);
pipe.c: if (msh->fds[i - 1][1] > 2)
pipe.c: close(msh->fds[i - 1][1]);
pipe.c: if (msh->fds[i][0] > 2)
pipe.c: close(msh->fds[i][0]);
pipe.c: if (msh->fds[i][1] > 2)
pipe.c: close(msh->fds[i][1]);
pipe.c: if (msh->in_fd > 2)
pipe.c: close(msh->in_fd);
pipe.c: if (msh->out_fd > 2)
pipe.c: close(msh->out_fd);
exec.c: if (pipe(msh->fds[i]) == -1)
exec.c: msh->pids[i] = pid;
exec.c: msh->fds[i] = ft_calloc(2, sizeof(int *));
exec.c: if (!msh->fds[i])
exec.c: get_in_type(msh, msh->cmds);
exec.c: get_out_type(msh, msh->cmds);
exec.c: get_out_type(msh, msh->cmds);
exec.c: get_in_type(msh, msh->cmds);
exec.c: if (!cmd_is_builtin(msh, msh->cmds->token))
exec.c: if (!msh->cmds)
exec.c: cmd_count = get_cmd_count(msh->cmds);
exec.c: msh->fds = ft_calloc(cmd_count, sizeof(int **));
exec.c: msh->pids = ft_calloc(cmd_count, sizeof(int *));
exec.c: if (!msh->pids || !msh->fds)
exec.c: waitpid(msh->pids[i], &status, 0);
exec.c: free(msh->fds[i]);
exec.c: msh->fds[i] = 0;
exec.c: free(msh->fds);
exec.c: msh->fds = 0;
exec.c: free(msh->pids);
export.c: cmd = msh->cmds;
export.c: print_env_declare(msh->env);
export.c: msh->env = env_add_back(msh->env, name, value);
export.c: msh->env = tmp_env->next;
export.c: cmd = msh->cmds;
export.c: tmp_env = msh->env;
commands.c: args_count = get_args_count(msh->cmds);
commands.c: if (!cmd_args || !msh->fds)
commands.c: cur_cmd = msh->cmds;
commands.c: cur_cmd = msh->cmds;
commands.c: msh->in_type = cmd_tmp->type;
commands.c: msh->cmds = cur_cmd;
commands.c: msh->in_type = cur_cmd->type;
commands.c: msh->cmds = cur_cmd;
commands.c: msh->in_type = msh->cmds->type;
minishellrc.c: msh->cmds = parse_command(line, msh->env);
minishellrc.c: free_cmd(msh->cmds);
minishellrc.c: home = ft_get_env(msh->env, "HOME");
minishellrc.c: free(msh->env);
input_redirections.c: if (msh->in_type != PIPE)
input_redirections.c: if (dup2(msh->in_fd, 0) < 0)
input_redirections.c: close(msh->in_fd);
input_redirections.c: if (dup2(msh->fds[i - 1][0], 0) < 0)
input_redirections.c: if (msh->in_fd != 0)
input_redirections.c: close(msh->in_fd);
input_redirections.c: msh->in_fd = open((*cur_cmd)->next->token, O_RDONLY | O_CREAT);
input_redirections.c: if (msh->in_fd == -1 && !g_return_code)
input_redirections.c: msh->in_type = cur_cmd->type;
input_redirections.c: cur_cmd = msh->cmds;
path.c: path = ft_strjoin(tmp, msh->cmds->token);
path.c: free(msh->cmds->token);
path.c: msh->cmds->token = path;
path.c: paths = split_paths_from_env(msh->env);
path.c: if (ft_strchr(msh->cmds->token, '/'))
path.c: ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);
path.c: free(msh->cmds->token);
path.c: msh->cmds->token = 0;
main.c: msh->aliases = alias_add_back(msh->aliases, ft_strdup("ls"), ft_strdup("ls --color=auto"));
main.c: while (msh->env && commands)
main.c: prompt = get_prompt(msh->env);
main.c: msh->cmds = parse_command(commands, msh->env);
main.c: msh->cmds = handle_alias(msh);
main.c: free_cmd(msh->cmds);
handle_alias.c: if (!msh->cmds)
handle_alias.c: if (msh->cmds->type == ARG)
handle_alias.c: alias_command = get_alias(msh->aliases, msh->cmds->token);
handle_alias.c: return (msh->cmds);
handle_alias.c: res = parse_command(alias_command, msh->env);
handle_alias.c: tmp->next = msh->cmds->next;
handle_alias.c: if(msh->cmds)
handle_alias.c: free(msh->cmds->token);
handle_alias.c: free(msh->cmds);
alias.c: if (!msh->cmds->next || msh->cmds->next->type != ARG)
alias.c: print_aliases(msh->aliases);
alias.c: name = get_alias_name(msh->cmds->next);
alias.c: if (ft_strchr(msh->cmds->next->token, '='))
alias.c: value = get_alias_value(msh->cmds->next);
alias.c: msh->aliases = alias_add_back(msh->aliases, name, value);
alias.c: if (get_alias(msh->aliases, name))
alias.c: ft_printf("alias %s=%s\n", name, get_alias(msh->aliases, name));
builtins.c: cd(msh->cmds);
builtins.c: if (!(msh->in_type == PIPE || msh->out_type == PIPE))
builtins.c: if (!msh->cmds->token)
builtins.c: if (!ft_strcmp(msh->cmds->token, "echo"))
builtins.c: g_return_code = echo(msh->cmds->next);
builtins.c: else if (!ft_strcmp(msh->cmds->token, "ret"))
builtins.c: g_return_code = ft_atoi(msh->cmds->next->token);
builtins.c: else if (!ft_strcmp(msh->cmds->token, "env"))
builtins.c: g_return_code = print_env(msh->env);
builtins.c: else if (!ft_strcmp(msh->cmds->token, "exit"))
builtins.c: else if (!ft_strcmp(msh->cmds->token, "pwd"))
builtins.c: else if (!ft_strcmp(msh->cmds->token, "cd"))
builtins.c: g_return_code = cd(msh->cmds);
builtins.c: else if (!ft_strcmp(msh->cmds->token, "alias"))
builtins.c: else if (!ft_strcmp(msh->cmds->token, "unalias"))
utils.c: free_env(msh->env);
utils.c: free_alias(msh->aliases);
utils.c: free(msh->pids);
utils.c: free(msh->fds);
utils.c: free_cmd(msh->cmds);
utils.c: fd = open(msh->cmds->token, O_DIRECTORY);
utils.c: ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->cmds->token);
utils.c: if (access(msh->cmds->token, X_OK) != -1)
utils.c: ft_printf_fd(2, "minishell: %s: ", msh->cmds->token);
utils.c: if (access(msh->cmds->token, F_OK) != -1)
output_redirections.c: if (msh->out_type != PIPE)
output_redirections.c: if (dup2(msh->out_fd, 1) < 0)
output_redirections.c: if (dup2(msh->fds[i][1], 1) < 0)
output_redirections.c: msh->out_type = (*cur_cmd)->type;
output_redirections.c: if (msh->out_type == RED_O)
output_redirections.c: msh->out_fd = open((*cur_cmd)->next->token,
output_redirections.c: if (msh->out_type == RED_O_APP)
output_redirections.c: msh->out_fd = open((*cur_cmd)->next->token,
output_redirections.c: if (msh->out_fd == -1)
output_redirections.c: msh->out_type = ARG;
output_redirections.c: msh->out_fd = 0;
output_redirections.c: if (cmds->type && msh->cmds == cmds)
output_redirections.c: while (msh->cmds->type != ARG && msh->cmds->next->next)
output_redirections.c: msh->cmds = msh->cmds->next->next;
output_redirections.c: msh->out_type = ARG;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/03/23 19:25:38 by marde-vr ### ########.fr */
/* Updated: 2024/03/24 09:09:25 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,6 +44,7 @@ void execute_command(t_msh *msh, char **cmd_args, int i)
}
free(cmd_args);
//ft_exit(msh, 1);
//ft_printf_fd(2, "exiting with code: %d\n", g_return_code);
ft_exit(msh, g_return_code);
}
if (msh->cmds->token)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/03/23 19:21:13 by marde-vr ### ########.fr */
/* Updated: 2024/03/24 08:56:03 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */