Skip to content

Commit

Permalink
corrected august bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 10, 2023
1 parent c6b7d5a commit 2d0e31a
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 81 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: zhabri <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/04 08:44:16 by zhabri #+# #+# #
# Updated: 2023/01/09 13:26:38 by zhabri ### ########.fr #
# Updated: 2023/01/10 11:24:53 by zhabri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -63,7 +63,8 @@ SRCS = add_cmd.c \
utils_pipex.c \
utils_pipex_bis.c \
utils_signal.c \
utils_ter.c
utils_table.c \
utils_ter.c

OBJS = $(SRCS:.c=.o)

Expand Down
6 changes: 5 additions & 1 deletion add_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 14:30:28 by zhabri #+# #+# */
/* Updated: 2022/12/20 14:45:43 by zhabri ### ########.fr */
/* Updated: 2023/01/10 12:52:58 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,6 +23,10 @@ void add_cmd(t_token *token, t_list **cmds, bool *pb)
node = ((t_cmd *)ft_lstlast(*cmds)->content);
if (node->str)
free(node->str);
if (node->fd_in > 2)
close(node->fd_in);
if (node->fd_out > 2)
close(node->fd_out);
node->str = ft_strdup(token->arg);
node->fd_in = -3;
node->fd_out = 1;
Expand Down
11 changes: 4 additions & 7 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 10:49:26 by zhabri #+# #+# */
/* Updated: 2023/01/09 14:21:21 by zhabri ### ########.fr */
/* Updated: 2023/01/10 13:01:15 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -47,8 +47,8 @@ void init_builtin_tab(t_builtin *tab[7])

void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
{
int fd_out;
int i;
int fd_out;
t_builtin *tab[7];

init_builtin_tab(tab);
Expand All @@ -68,9 +68,6 @@ void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
free(children_pid);
if (cmd->fd_in != -1 && cmd->fd_out != -1)
tab[built_in](cmd->str, fd_out);
else
{
clean_exit(NULL);
exit(1);
}
clean_exit(NULL);
exit(1);
}
6 changes: 4 additions & 2 deletions exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/30 11:56:08 by zhabri #+# #+# */
/* Updated: 2023/01/09 13:30:38 by zhabri ### ########.fr */
/* Updated: 2023/01/10 10:50:58 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,6 +36,7 @@ bool exit_parent_arg(char **cmd_split)

bool exit_parent(void)
{
int ret;
char **cmd_split;

if (ft_lstsize(*g_glob->cmds) == 1)
Expand All @@ -52,8 +53,9 @@ bool exit_parent(void)
}
else
{
ret = g_glob->exit_ret;
clean_and_free(cmd_split);
exit(g_glob->exit_ret);
exit(ret);
}
}
free_tab(cmd_split);
Expand Down
43 changes: 34 additions & 9 deletions get_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 12:20:17 by brenaudo #+# #+# */
/* Updated: 2023/01/05 14:30:31 by zhabri ### ########.fr */
/* Updated: 2023/01/10 13:00:04 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft/includes/libft.h"
#include "minishell.h"
#include <unistd.h>

static void sha1sum_child(char *cmd, int *pipefd, int *pipes);
static void sha1sum_child(char **cmd, int *pipefd, int *pipes, \
int *children_pid);

void get_sum(char *cmd, char **ret, int *pipes)
void get_sum(char **cmd, char **ret, int *pipes, int *children_pid)
{
int fid;
int pipefd[2];
Expand All @@ -26,7 +28,7 @@ void get_sum(char *cmd, char **ret, int *pipes)
if (fid == -1)
perror("fork");
else if (fid == 0)
sha1sum_child(cmd, pipefd, pipes);
sha1sum_child(cmd, pipefd, pipes, children_pid);
close(pipefd[1]);
waitpid(fid, NULL, 0);
free(*ret);
Expand All @@ -46,25 +48,48 @@ bool is_dir(char *cmd)
return (true);
}

static void sha1sum_child(char *cmd, int *pipefd, int *pipes)
static void free_cmd_tab(char **cmd)
{
if (cmd[0])
free_tab(cmd);
else
free_tab_bis(cmd);
}

static void sha1sum_free(char **cmd, char **envp, char *exec1, char *exec2)
{
free_cmd_tab(cmd);
free(exec1);
free(exec2);
free_tab(envp);
close(1);
}

static void sha1sum_child(char **cmd, int *pipefd, int *pipes, \
int *children_pid)
{
int i;
char *exec_args[3];
char **envp;

i = -1;
if (children_pid != NULL)
free(children_pid);
while (pipes && ++i < 4)
close(pipes[i]);
exec_args[0] = ft_strdup("/usr/bin/sha1sum");
exec_args[1] = cmd;
exec_args[1] = ft_strdup(cmd[0]);
exec_args[2] = NULL;
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
envp = envp_list_to_tab();
clean_exit(NULL);
if (!is_dir(cmd) && !access(cmd, X_OK) \
&& execve(exec_args[0], exec_args, envp) == -1)
perror("minishell");
if (!is_dir(cmd[0]) && !access(cmd[0], X_OK))
{
free_cmd_tab(cmd);
execve(exec_args[0], exec_args, envp);
}
sha1sum_free(cmd, envp, exec_args[0], exec_args[1]);
exit(1);
}
2 changes: 1 addition & 1 deletion heredocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/20 12:33:11 by zhabri #+# #+# */
/* Updated: 2022/12/29 13:55:25 by zhabri ### ########.fr */
/* Updated: 2023/01/10 11:33:19 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion main_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/23 10:40:36 by zhabri #+# #+# */
/* Updated: 2023/01/06 11:44:51 by zhabri ### ########.fr */
/* Updated: 2023/01/10 11:08:49 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
8 changes: 6 additions & 2 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 11:49:37 by zhabri #+# #+# */
/* Updated: 2023/01/09 11:05:43 by zhabri ### ########.fr */
/* Updated: 2023/01/10 12:53:16 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -81,11 +81,15 @@ void event_loop(void)

int main(int argc, char **argv, char **envp)
{
char **t;

(void)argc;
init_sig_callbacks(0);
init_g_glob();
get_envp(envp);
get_sum(argv[0], &(g_glob->minishell_sum), NULL);
t = tab_dup(argv, argc);
get_sum(t, &(g_glob->minishell_sum), NULL, NULL);
free_tab(t);
event_loop();
final_clean_up();
}
7 changes: 4 additions & 3 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 20:30:45 by zhabri #+# #+# */
/* Updated: 2023/01/09 13:26:18 by zhabri ### ########.fr */
/* Updated: 2023/01/10 11:24:36 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -85,7 +85,7 @@ void call_builtin(int built_in, t_cmd *cmd, int *pipes, \
int *children_pid);
void cd(char *cmd, int fd_out);
bool cd_parent(void);
void change_sig_handling(char *cmd, int *pipes);
void change_sig_handling(char *cmd, int *pipes, int *children_pid);
bool check_exit_error(char **cmd_split);
void child(t_cmd *cmd, int *pipes, int *children_pid);
void clean_and_free(char **cmd_split);
Expand Down Expand Up @@ -139,7 +139,7 @@ t_list *get_env_node(char *var);
char *get_first(void);
char *get_home_content(void);
char *get_limiter(t_token *token);
void get_sum(char *cmd, char **ret, int *pipes);
void get_sum(char **cmd, char **ret, int *pipes, int *children_pid);
void get_ops(const char *input, t_list **head);
char *get_path(char *cmd, bool shaone);
void handle_sigint(void);
Expand Down Expand Up @@ -181,6 +181,7 @@ void split_cmds(void);
int str_cmp_sort(char *s1, char *s2);
bool str_is_numeric(char *str);
t_list **str_tab_to_list(char **tab);
char **tab_dup(char **tab, int tab_len);
void unlink_heredocs(void);
void unset(char *cmd, int fd_out);
bool unset_parent(void);
Expand Down
31 changes: 5 additions & 26 deletions pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 12:02:33 by brenaudo #+# #+# */
/* Updated: 2023/01/09 15:54:11 by zhabri ### ########.fr */
/* Updated: 2023/01/10 11:18:33 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,6 @@ static void close_pipe_and_recreate(int *pipes, t_cmd *cmd);

static void pipex_loop_core(int *children_pid, t_list *curr, int *pipes)
{
// int ret;
int built_in;

built_in = builtin(((t_cmd *)curr->content)->str);
Expand All @@ -33,16 +32,7 @@ static void pipex_loop_core(int *children_pid, t_list *curr, int *pipes)
children_pid);
}
if (built_in == -1)
change_sig_handling(((t_cmd *)curr->content)->str, pipes);
// else
// {
// waitpid(children_pid[((t_cmd *)curr->content)->cmd_idx], &ret, 0);
// printf("children_pid is %d, ret is %d\n", children_pid[((t_cmd *)curr->content)->cmd_idx], ret >> 8);
// if (ret >> 8 == 0)
// children_pid[((t_cmd *)curr->content)->cmd_idx] = -256;
// else
// children_pid[((t_cmd *)curr->content)->cmd_idx] = -1 * (ret >> 8);
// }
change_sig_handling(((t_cmd *)curr->content)->str, pipes, children_pid);
g_glob->in_child = true;
close_pipe_and_recreate(pipes, ((t_cmd *)curr->content));
}
Expand Down Expand Up @@ -78,20 +68,9 @@ void pipex(void)
children_pid = pipex_loop();
while (children_pid[i] != 0)
{
// printf("children_pid: %d\n", children_pid[i]);
// if (children_pid[i] < 0 && !g_glob->sig_int && !g_glob->sig_quit \
// && children_pid[i] != -256)
// g_glob->exit_ret = children_pid[i] * (-1);
// else if (children_pid[i] < 0 && !g_glob->sig_int && !g_glob->sig_quit \
// && children_pid[i] == -256)
// g_glob->exit_ret = 0;
// else if (children_pid[i] > 0)
// {
waitpid(children_pid[i], &exit_ret, 0);
// printf("ret is %d\n", exit_ret >> 8);
if (!g_glob->sig_int && !g_glob->sig_quit)
g_glob->exit_ret = exit_ret >> 8;
// }
waitpid(children_pid[i], &exit_ret, 0);
if (!g_glob->sig_int && !g_glob->sig_quit)
g_glob->exit_ret = exit_ret >> 8;
i++;
}
g_glob->in_child = false;
Expand Down
Loading

0 comments on commit 2d0e31a

Please sign in to comment.