Skip to content

Commit

Permalink
jsp frer 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Dec 29, 2022
1 parent e8588d7 commit 839360a
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 128 deletions.
6 changes: 4 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: 2022/12/26 16:37:02 by zhabri ### ########.fr #
# Updated: 2022/12/29 14:05:48 by zhabri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -47,7 +47,9 @@ SRCS = minishell.c \
utils_pipex.c \
signal.c \
main_utils.c \
get_sum.c
get_sum.c \
utils_heredocs.c \
utils_signal.c

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

Expand Down
5 changes: 2 additions & 3 deletions find_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/09 14:14:40 by zhabri #+# #+# */
/* Updated: 2022/12/27 14:46:35 by zhabri ### ########.fr */
/* Updated: 2022/12/29 13:46:24 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -75,8 +75,7 @@ void find_cmd_infile(t_token *token, t_cmd *node, bool *pb)
free(file);
if (!g_glob->sig_int)
*pb = ft_open_in(node, token);
else
*pb = false;
*pb = false;
}

void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
Expand Down
15 changes: 9 additions & 6 deletions get_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 12:20:17 by brenaudo #+# #+# */
/* Updated: 2022/12/28 12:20:31 by brenaudo ### ########.fr */
/* Updated: 2022/12/29 11:23:02 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

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

void get_sum(char *cmd, char **ret)
void get_sum(char *cmd, char **ret, int *pipes)
{
int fid;
int pipefd[2];
Expand All @@ -25,7 +25,7 @@ void get_sum(char *cmd, char **ret)
if (fid == -1)
perror("fork");
else if (fid == 0)
sha1sum_child(cmd, pipefd);
sha1sum_child(cmd, pipefd, pipes);
close(pipefd[1]);
waitpid(fid, NULL, 0);
free(*ret);
Expand All @@ -34,19 +34,22 @@ void get_sum(char *cmd, char **ret)
close(pipefd[0]);
}

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

i = -1;
while (pipes && ++i < 4)
close(pipes[i]);
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:");
Expand Down
75 changes: 22 additions & 53 deletions heredocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,38 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/20 12:33:11 by zhabri #+# #+# */
/* Updated: 2022/12/27 13:59:47 by zhabri ### ########.fr */
/* Updated: 2022/12/29 13:55:25 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

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

static char *name_generator(void)
static void finalize_heredoc(char *here_doc_entry, char *limiter, \
int fd, int stdin_cpy)
{
int i;
char *file_name;

i = 0;
file_name = NULL;
while (file_name == NULL)
file_name = malloc(10 * sizeof(char));
file_name[0] = '.';
file_name[9] = '\0';
while (++i < 9)
file_name[i] = 'a';
while (file_name == NULL || access(file_name, 0) == 0)
{
file_name[8]++;
i = 9;
while (--i > 1)
{
if (file_name[i] == 'z' + 1)
{
file_name[i - 1]++;
file_name[i] = 'a';
}
}
}
return (file_name);
if (!g_glob->sig_int)
eof_limiter_not_found(here_doc_entry, limiter);
close(fd);
free(limiter);
dup2(stdin_cpy, 0);
close(stdin_cpy);
}

static char *get_limiter(t_token *token)
static int init_heredoc(char **here_doc_entry, char **file_name, \
int *stdin_cpy, int *fd)
{
int i;
char *limiter;

i = 0;
while (ft_isprint_nospace(token->arg[i]))
*file_name = name_generator();
*stdin_cpy = dup(0);
*fd = open(*file_name, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (*fd == -1)
{
if (token->arg[i] == '"')
i += find_quote(token->arg + i + 1, '"');
else if (token->arg[i] == '\'')
i += find_quote(token->arg + i + 1, '\'');
i++;
free(*file_name);
return (1);
}
limiter = ft_substr(token->arg, 0, i);
return (remove_quotes(limiter));
*here_doc_entry = NULL;
return (0);
}

static void handle_here_doc(t_token *token)
Expand All @@ -69,16 +48,9 @@ static void handle_here_doc(t_token *token)
char *file_name;
char *limiter;

file_name = name_generator();
stdin_cpy = dup(0);
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
free(file_name);
return ;
}
here_doc_entry = NULL;
limiter = get_limiter(token);
if (init_heredoc(&here_doc_entry, &file_name, &stdin_cpy, &fd))
return ;
g_glob->here_doc = true;
while (!g_glob->sig_int)
{
Expand All @@ -90,10 +62,7 @@ static void handle_here_doc(t_token *token)
free(here_doc_entry);
}
g_glob->here_doc = false;
if (!g_glob->sig_int)
eof_limiter_not_found(here_doc_entry, limiter);
close(fd);
dup2(stdin_cpy, 0);
finalize_heredoc(here_doc_entry, limiter, fd, stdin_cpy);
token->file = file_name;
}

Expand Down
11 changes: 10 additions & 1 deletion main_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/23 10:40:36 by zhabri #+# #+# */
/* Updated: 2022/12/23 11:35:14 by zhabri ### ########.fr */
/* Updated: 2022/12/29 13:57:23 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void reset_g_glob(void)
{
g_glob->in_child = false;
g_glob->sig_int = false;
g_glob->sig_quit = false;
g_glob->here_doc = false;
}

void final_clean_up(void)
{
ft_lstclear(g_glob->envp, free);
free_null(g_glob->minishell_sum);
free_null(g_glob->envp);
free_null(g_glob);
rl_clear_history();
Expand Down
8 changes: 3 additions & 5 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: 2022/12/27 14:43:50 by zhabri ### ########.fr */
/* Updated: 2022/12/29 14:12:32 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,9 +57,7 @@ void event_loop(void)

while (1)
{
g_glob->in_child = false;
g_glob->sig_int = false;
g_glob->here_doc = false;
reset_g_glob();
input = readline("minishell> ");
if (input == NULL)
{
Expand Down Expand Up @@ -87,7 +85,7 @@ int main(int argc, char **argv, char **envp)
if (argc > 1)
init_input(argv[1]);
get_envp(envp);
get_sum(argv[0], &(g_glob->minishell_sum));
get_sum(argv[0], &(g_glob->minishell_sum), NULL);
event_loop();
final_clean_up();
}
12 changes: 9 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: 2022/12/27 13:06:46 by zhabri ### ########.fr */
/* Updated: 2022/12/29 14:04:35 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,12 +68,18 @@ typedef struct s_glob
unsigned char exit_ret;
bool in_child;
bool sig_int;
bool sig_quit;
bool here_doc;
char *minishell_sum;
} t_glob;

extern t_glob *g_glob;

void handle_sigquit(void);
void handle_sigint(void);
void reset_g_glob(void);
char *get_limiter(t_token *token);
char *name_generator(void);
void init_g_glob(void);
void ignore_sig(int sig);
void final_clean_up(void);
Expand Down Expand Up @@ -131,8 +137,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);
void get_sum(char *cmd, char **ret, int *pipes);
char *get_path(char *cmd);
void change_sig_handling(char *cmd);
void change_sig_handling(char *cmd, int *pipes);

#endif
17 changes: 8 additions & 9 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: 2022/12/26 16:24:04 by zhabri ### ########.fr */
/* Updated: 2022/12/29 13:58:53 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,6 @@ int *pipex_loop(void)
int pipes[4];
int *children_pid;
t_list *curr;
t_cmd *cmd;

curr = *g_glob->cmds;
children_pid = NULL;
Expand All @@ -33,13 +32,12 @@ int *pipex_loop(void)
{
if (curr->content)
{
cmd = ((t_cmd *)curr->content);
children_pid[cmd->cmd_idx] = fork();
if (children_pid[cmd->cmd_idx] == 0)
child(cmd, pipes, children_pid);
change_sig_handling(cmd->str);
children_pid[((t_cmd *)curr->content)->cmd_idx] = fork();
if (children_pid[((t_cmd *)curr->content)->cmd_idx] == 0)
child(((t_cmd *)curr->content), pipes, children_pid);
change_sig_handling(((t_cmd *)curr->content)->str, pipes);
g_glob->in_child = true;
close_pipe_and_recreate(pipes, cmd);
close_pipe_and_recreate(pipes, ((t_cmd *)curr->content));
curr = curr->next;
}
}
Expand All @@ -58,7 +56,8 @@ void pipex(void)
while (children_pid[i] != 0)
{
waitpid(children_pid[i++], &exit_ret, 0);
g_glob->exit_ret = exit_ret >> 8;
if (!g_glob->sig_int && !g_glob->sig_quit)
g_glob->exit_ret = exit_ret >> 8;
}
g_glob->in_child = false;
init_sig_callbacks(0);
Expand Down
8 changes: 5 additions & 3 deletions pipex_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/26 16:35:44 by zhabri #+# #+# */
/* Updated: 2022/12/26 16:54:59 by zhabri ### ########.fr */
/* Updated: 2022/12/29 13:41:05 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,11 +45,13 @@ void child(t_cmd *cmd, int *pipes, int *children_pid)
clean_exit(children_pid);
if (execve(cmd_split[0], cmd_split, envp) == -1)
perror("Error");
close_pipes(pipes);
exit(1);
}
else
clean_exit(children_pid);
close_pipes(pipes);
exit(1);
exit(0);
}

static char *get_path_loop(char *cmd, t_list *envp_entry)
Expand Down Expand Up @@ -85,7 +87,7 @@ char *get_path(char *cmd)
envp_entry = *g_glob->envp;
while (ft_strncmp((char *)envp_entry->content, "PATH=", 5))
envp_entry = envp_entry->next;
if (access(cmd, F_OK) == 0 && ft_strchr(cmd, '/'))
if (cmd && access(cmd, F_OK) == 0 && ft_strchr(cmd, '/'))
return (cmd);
return (get_path_loop(cmd, envp_entry));
}
Expand Down
Loading

0 comments on commit 839360a

Please sign in to comment.