Skip to content

Commit

Permalink
déso pour la norme
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Renaudon authored and Baptiste Renaudon committed Dec 28, 2022
1 parent 7e2b187 commit e8588d7
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ SRCS = minishell.c \
pipex_children.c \
utils_pipex.c \
signal.c \
main_utils.c
main_utils.c \
get_sum.c

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

Expand Down
54 changes: 54 additions & 0 deletions get_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_sum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 12:20:17 by brenaudo #+# #+# */
/* Updated: 2022/12/28 12:20:31 by brenaudo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static void sha1sum_child(char *cmd, int *pipe);

void get_sum(char *cmd, char **ret)
{
int fid;
int pipefd[2];

if (pipe(pipefd) < 0)
perror("minishell:");
fid = fork();
if (fid == -1)
perror("fork");
else if (fid == 0)
sha1sum_child(cmd, pipefd);
close(pipefd[1]);
waitpid(fid, NULL, 0);
free(*ret);
*ret = ft_calloc(41, sizeof(char));
read(pipefd[0], *ret, 40);
close(pipefd[0]);
}

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

exec_args[0] = ft_strdup("/usr/bin/sha1sum");
exec_args[1] = cmd;
exec_args[2] = NULL;
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
envp = envp_list_to_tab();
free(g_glob->minishell_sum);
clean_exit(NULL);
if (execve(exec_args[0], exec_args, envp) == -1)
perror("minishell:");
exit(1);
}
2 changes: 1 addition & 1 deletion heredocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void handle_here_doc(t_token *token)
close(fd);
dup2(stdin_cpy, 0);
token->file = file_name;
}
}

void scan_heredocs(void)
{
Expand Down
14 changes: 14 additions & 0 deletions main_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ void free_null(void *var)
free(var);
var = NULL;
}

void init_g_glob(void)
{
g_glob = malloc(sizeof(t_glob));
g_glob->head = NULL;
g_glob->cmds = NULL;
g_glob->envp = NULL;
g_glob->input = NULL;
g_glob->minishell_sum = NULL;
g_glob->exit_ret = 0;
g_glob->in_child = false;
g_glob->sig_int = false;
g_glob->here_doc = false;
}
4 changes: 2 additions & 2 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ t_glob *g_glob;

void get_envp(char **envp)
{
g_glob = malloc(sizeof(t_glob));
if (g_glob)
g_glob->envp = str_tab_to_list(envp);
}
Expand Down Expand Up @@ -56,7 +55,6 @@ void event_loop(void)
t_list **head;
char *input;

g_glob->exit_ret = 0;
while (1)
{
g_glob->in_child = false;
Expand Down Expand Up @@ -85,9 +83,11 @@ void event_loop(void)
int main(int argc, char **argv, char **envp)
{
init_sig_callbacks(0);
init_g_glob();
if (argc > 1)
init_input(argv[1]);
get_envp(envp);
get_sum(argv[0], &(g_glob->minishell_sum));
event_loop();
final_clean_up();
}
6 changes: 6 additions & 0 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# include <readline/history.h>
# include <readline/readline.h>
# include "libft/includes/libft.h"
# include <unistd.h>

# define PARENT 0
# define CHILD 1
Expand Down Expand Up @@ -68,10 +69,12 @@ typedef struct s_glob
bool in_child;
bool sig_int;
bool here_doc;
char *minishell_sum;
} t_glob;

extern t_glob *g_glob;

void init_g_glob(void);
void ignore_sig(int sig);
void final_clean_up(void);
void free_null(void *var);
Expand Down Expand Up @@ -128,5 +131,8 @@ void exit_on_permission(char **cmd_split, \
void exit_on_bad_cmd(char **cmd_split, \
int *pipes, char *cmd, int *children_pid);
void free_tab_bis(void *t);
void get_sum(char *cmd, char **ret);
char *get_path(char *cmd);
void change_sig_handling(char *cmd);

#endif
2 changes: 2 additions & 0 deletions pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int *pipex_loop(void)
children_pid[cmd->cmd_idx] = fork();
if (children_pid[cmd->cmd_idx] == 0)
child(cmd, pipes, children_pid);
change_sig_handling(cmd->str);
g_glob->in_child = true;
close_pipe_and_recreate(pipes, cmd);
curr = curr->next;
Expand All @@ -60,6 +61,7 @@ void pipex(void)
g_glob->exit_ret = exit_ret >> 8;
}
g_glob->in_child = false;
init_sig_callbacks(0);
free(children_pid);
}

Expand Down
5 changes: 1 addition & 4 deletions pipex_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
/* */
/* ************************************************************************** */

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

static char *get_path(char *cmd);
static void dup_and_close(t_cmd *cmd, int *pipes);

void free_tab_bis(void *t)
Expand Down Expand Up @@ -81,7 +78,7 @@ static char *get_path_loop(char *cmd, t_list *envp_entry)
return (NULL);
}

static char *get_path(char *cmd)
char *get_path(char *cmd)
{
t_list *envp_entry;

Expand Down
21 changes: 21 additions & 0 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <readline/readline.h>
#include <signal.h>

// Problème quand on enchaîne les ctrl+C : la commande suivante n'est pas prise en compte

static void handler(int sig)
{
if (sig == SIGINT && !g_glob->in_child)
Expand Down Expand Up @@ -62,5 +64,24 @@ void init_sig_callbacks(int process)
signal(SIGQUIT, handler);
}
else
{
signal(SIGINT, ignore_sig);
signal(SIGQUIT, ignore_sig);
}
}

void change_sig_handling(char *cmd)
{
char **cmd_split;
char *sum;

sum = NULL;
cmd_split = ft_split(cmd, ' ');
cmd_split[0] = get_path(cmd_split[0]);
if (cmd_split[0] != NULL)
{
get_sum(cmd_split[0], &sum);
if (ft_strncmp(sum, g_glob->minishell_sum, 41) == 0)
init_sig_callbacks(42);
}
}
9 changes: 6 additions & 3 deletions utils_pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ void close_pipes(int *pipes)
void clean_exit(int *children_pid)
{
free_null(children_pid);
clear_cmds();
if (g_glob->cmds)
clear_cmds();
free_null(g_glob->cmds);
free_op_list();
ft_lstclear(g_glob->envp, free);
if (g_glob->head)
free_op_list();
if (g_glob->envp)
ft_lstclear(g_glob->envp, free);
free_null(g_glob->envp);
free_null(g_glob->head);
free_null(g_glob->input);
Expand Down

0 comments on commit e8588d7

Please sign in to comment.