Skip to content

Commit

Permalink
exit looks good lol
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 2, 2023
1 parent b594612 commit 09e0364
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 65 deletions.
9 changes: 7 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/29 14:05:48 by zhabri ### ########.fr #
# Updated: 2023/01/02 15:00:09 by zhabri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -49,7 +49,12 @@ SRCS = minishell.c \
main_utils.c \
get_sum.c \
utils_heredocs.c \
utils_signal.c
utils_signal.c \
pwd.c \
exit.c \
utils_exit.c \
ft_atoll.c \
ft_split_sep.c

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

Expand Down
105 changes: 63 additions & 42 deletions exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,88 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/30 11:56:08 by zhabri #+# #+# */
/* Updated: 2022/12/30 12:40:23 by zhabri ### ########.fr */
/* Updated: 2023/01/02 15:02:43 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft/includes/libft.h"
#include "minishell.h"

bool str_is_numeric(char *str)
bool exit_parent_arg(char **cmd_split)
{
int i;
long long ret;

i = 0;
while (str[i])
if (!ft_isdigit(str[i++]))
return (false);
return (true);
if (cmd_split[1])
{
if (check_exit_error(cmd_split))
return (true);
clean_exit(NULL);
if (!is_valid_exit_arg(cmd_split[1]))
{
free_tab(cmd_split);
exit(2);
}
ret = ft_atoll(cmd_split[1]);
free_tab(cmd_split);
exit(ret);
}
return (false);
}

bool overflows_llong(char *str)
bool exit_parent(void)
{
bool is_neg;
int len;
char **cmd_split;

is_neg = false;
if (str[0] == '-' || str[0] == '+')
if (ft_lstsize(*g_glob->cmds) == 1)
{
if (str[0] == '-')
is_neg = true;
str++;
cmd_split = ft_split_sep(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (!ft_strncmp(cmd_split[0], "exit", 5))
{
ft_putstr_fd("exit\n", 2);
if (exit_parent_arg(cmd_split))
return (true);
else
{
clean_exit(NULL);
free_tab(cmd_split);
exit(0);
}
}
}
len = ft_strlen(str);
if (len > 19)
return (true);
else if (len < 19)
return (false);
if (!is_neg && ft_strncmp(str, "9223372036854775807", 19) > 0)
return (true);
if (is_neg && ft_strncmp(str, "9223372036854775808", 19) > 0)
return (true);
return (false);
}

bool exit_parent(void)
bool exit_child_arg(char **cmd_split)
{
t_cmd *cmd;
char **cmd_split;
long long ret;

if (ft_lstsize(g_glob->cmds) == 1)
if (cmd_split[1])
{
cmd = ((t_cmd *)(*g_glob->cmds)->content);
cmd_split = ft_split(cmd->str, ' ');
if (!ft_strncmp(cmd_split[0], "exit", 5))
// TODO if cmd_split[1] exists it needs to be:
// 1- numeric
// 2- lower than LLMAX higer than LLMIN
// cmd_split[2] is NULL
// Always print exit in stderr
// Always change g_glob->ret according to the following:
// 1- If the numeric argument is OK change to it
// 2- If its not numeric change to 2 print error and exit
// 3- Change to 1 if too many print error dont exit
if (check_exit_error(cmd_split))
return (true);
if (!is_valid_exit_arg(cmd_split[1]))
{
free_tab(cmd_split);
g_glob->exit_ret = 2;
}
ret = ft_atoll(cmd_split[1]);
g_glob->exit_ret = ret;
}
free_tab(cmd_split);
return (false);
}

bool exit_child(char *cmd)
{
char **cmd_split;

cmd_split = ft_split_sep(cmd, " \t");
if (!ft_strncmp(cmd_split[0], "exit", 5))
{
g_glob->exit_ret = 0;
exit_child_arg(cmd_split);
return (true);
}
return (false);
}
// TODO: IF IN CHILD never exit but change g_glob->ret
2 changes: 1 addition & 1 deletion ft_atoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/22 10:25:53 by zhabri #+# #+# */
/* Updated: 2022/12/30 12:35:19 by zhabri ### ########.fr */
/* Updated: 2023/01/02 13:49:09 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
83 changes: 83 additions & 0 deletions ft_split_sep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_sep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:29 by zhabri #+# #+# */
/* Updated: 2023/01/02 14:59:34 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static bool is_in_str(char *sep, char c)
{
int i;

i = 0;
while (sep[i])
if (sep[i++] == c)
return (true);
return (false);
}

static int tab_len(char *str, char *sep)
{
int i;
int n;

i = 0;
n = 0;
while (str[i])
{
if (!is_in_str(sep, str[i]))
if ((!is_in_str(sep, str[i + 1]))
|| !str[i + 1])
n++;
i++;
}
return (n);
}

static char **split_loop(char **tab, char const *s, char *sep)
{
size_t i;
size_t j;
char *tmp;

i = 0;
j = 0;
while (i <= ft_strlen(s))
{
if (!s[i] || (is_in_str(sep, s[i])))
{
if (j < i)
{
tmp = ft_substr(s, j, i - j);
if (!tmp)
{
free_tab(tab);
return (NULL);
}
add_to_tab(tab, tmp);
}
j = i + 1;
}
i++;
}
return (tab);
}

char **ft_split_sep(char const *s, char *sep)
{
char **tab;

if (!s)
return (NULL);
tab = ft_calloc(tab_len((char *)s, sep) + 1, sizeof(char *));
if (!tab)
return (NULL);
return (split_loop(tab, s, sep));
}
11 changes: 10 additions & 1 deletion 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/30 12:36:35 by zhabri ### ########.fr */
/* Updated: 2023/01/02 15:04:44 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -75,6 +75,15 @@ typedef struct s_glob

extern t_glob *g_glob;

bool last_cmd_is_exit(void);
char **ft_split_sep(char const *s, char *sep);
bool exit_child(char *cmd);
bool exit_parent(void);
bool check_exit_error(char **cmd_split);
void print_exit_num_error(char *cmd);
bool is_valid_exit_arg(char *cmd);
bool overflows_llong(char *str);
bool str_is_numeric(char *str);
long long ft_atoll(const char *nptr);
void handle_sigquit(void);
void handle_sigint(void);
Expand Down
38 changes: 27 additions & 11 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/29 13:58:53 by zhabri ### ########.fr */
/* Updated: 2023/01/02 15:27:42 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,10 +17,21 @@
static void create_pipes(int *pipes);
static void close_pipe_and_recreate(int *pipes, t_cmd *cmd);

static void pipex_loop_exit(int *children_pid, t_list *curr, int *pipes)
{
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, ((t_cmd *)curr->content));
}

int *pipex_loop(void)
{
int pipes[4];
int *children_pid;
bool exit_ret;
t_list *curr;

curr = *g_glob->cmds;
Expand All @@ -32,14 +43,13 @@ int *pipex_loop(void)
{
if (curr->content)
{
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, ((t_cmd *)curr->content));
curr = curr->next;
exit_ret = exit_child(((t_cmd *)curr->content)->str);
if (!exit_ret)
pipex_loop_exit(children_pid, curr, pipes);
else
children_pid[((t_cmd *)curr->content)->cmd_idx] = -1;
}
curr = curr->next;
}
close_pipes(pipes);
return (children_pid);
Expand All @@ -52,12 +62,18 @@ void pipex(void)
int *children_pid;

i = 0;
if (exit_parent())
return ;
children_pid = pipex_loop();
while (children_pid[i] != 0)
{
waitpid(children_pid[i++], &exit_ret, 0);
if (!g_glob->sig_int && !g_glob->sig_quit)
g_glob->exit_ret = exit_ret >> 8;
if (children_pid[i] != -1)
{
waitpid(children_pid[i], &exit_ret, 0);
if (!g_glob->sig_int && !g_glob->sig_quit && !last_cmd_is_exit())
g_glob->exit_ret = exit_ret >> 8;
}
i++;
}
g_glob->in_child = false;
init_sig_callbacks(0);
Expand Down
4 changes: 2 additions & 2 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/29 13:41:05 by zhabri ### ########.fr */
/* Updated: 2023/01/02 15:01:06 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,7 +36,7 @@ void child(t_cmd *cmd, int *pipes, int *children_pid)

if (cmd->fd_in != -1 && cmd->fd_out != -1 && cmd->str[0] != '\0')
{
cmd_split = ft_split(cmd->str, ' ');
cmd_split = ft_split_sep(cmd->str, " \t");
cmd_split[0] = get_path(cmd_split[0]);
exit_on_bad_cmd(cmd_split, pipes, cmd->str, children_pid);
exit_on_permission(cmd_split, pipes, children_pid);
Expand Down
6 changes: 3 additions & 3 deletions pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 11:16:11 by brenaudo #+# #+# */
/* Updated: 2023/01/02 11:16:27 by brenaudo ### ########.fr */
/* Updated: 2023/01/02 13:33:21 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.c"
#include "minishell.h"

void pwd(void)
{
int size;
char *path;
char *ret;

size = 16;
path = NULL;
ret = NULL;
Expand Down
Loading

0 comments on commit 09e0364

Please sign in to comment.