Skip to content

Commit

Permalink
fix various mistakes still sigpipe error to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 9, 2023
1 parent 217f0e6 commit 3642bd8
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 57 deletions.
3 changes: 2 additions & 1 deletion 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/06 10:39:22 by zhabri ### ########.fr #
# Updated: 2023/01/09 13:26:38 by zhabri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -61,6 +61,7 @@ SRCS = add_cmd.c \
utils_export_bis.c \
utils_heredocs.c \
utils_pipex.c \
utils_pipex_bis.c \
utils_signal.c \
utils_ter.c

Expand Down
5 changes: 4 additions & 1 deletion 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/06 11:55:35 by zhabri ### ########.fr */
/* Updated: 2023/01/09 14:21:21 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -69,5 +69,8 @@ void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
if (cmd->fd_in != -1 && cmd->fd_out != -1)
tab[built_in](cmd->str, fd_out);
else
{
clean_exit(NULL);
exit(1);
}
}
39 changes: 20 additions & 19 deletions cd.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 12:40:50 by zhabri #+# #+# */
/* Updated: 2023/01/05 14:47:16 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:17:52 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,25 +34,26 @@ static void ft_chdir(char *dir, int size)
t_list *pwd;
t_list *old_pwd;

pwd = get_env_node("PWD");
old_pwd = get_env_node("OLDPWD");
ret = NULL;
free_null(old_pwd->content);
tmp = ft_strdup(pwd->content + 4);
old_pwd->content = ft_strjoin("OLDPWD=", tmp);
free_and_null(&tmp);
chdir(dir);
while (ret == NULL)
if (dir)
{
size *= 2;
init_ft_chdir(&pwd, &old_pwd, &ret);
free_null(old_pwd->content);
tmp = ft_strdup(pwd->content + 4);
old_pwd->content = ft_strjoin("OLDPWD=", tmp);
free_and_null(&tmp);
while (tmp == NULL)
tmp = ft_calloc(size, sizeof(char));
ret = getcwd(tmp, size);
chdir(dir);
while (ret == NULL)
{
size *= 2;
free_and_null(&tmp);
while (tmp == NULL)
tmp = ft_calloc(size, sizeof(char));
ret = getcwd(tmp, size);
}
free_null(pwd->content);
pwd->content = ft_strjoin("PWD=", tmp);
free_null(tmp);
}
free_null(pwd->content);
pwd->content = ft_strjoin("PWD=", tmp);
free_null(tmp);
}

void cd(char *cmd, int fd_out)
Expand All @@ -78,7 +79,7 @@ void cd(char *cmd, int fd_out)
ft_chdir(cmd_split[1], 16);
}
else
ft_chdir(get_env_node("HOME")->content + 5, 16);
ft_chdir(get_home_content(), 16);
clean_and_free(cmd_split);
exit(0);
}
Expand Down Expand Up @@ -119,7 +120,7 @@ bool cd_parent(void)
return (true);
}
else
ft_chdir(get_env_node("HOME")->content + 5, 16);
ft_chdir(get_home_content(), 16);
free_tab(cmd_split);
g_glob->exit_ret = 0;
return (true);
Expand Down
4 changes: 2 additions & 2 deletions echo.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 11:24:58 by zhabri #+# #+# */
/* Updated: 2023/01/06 11:23:06 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:05:43 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,7 +39,7 @@ void echo(char *cmd, int fd_out)
i = 1;
cmd_split = ft_split_quotes(cmd, " \t");
newline = is_echo_with_newline(cmd_split[1]);
if (!newline)
while (!is_echo_with_newline(cmd_split[i]))
i++;
while (cmd_split[i] && cmd_split[i + 1])
{
Expand Down
5 changes: 3 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/05 14:47:53 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:30:38 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -85,6 +85,7 @@ void exit_child(char *cmd, int fd_out)
free_tab(cmd_split);
exit(ret);
}
ret = g_glob->exit_ret;
clean_and_free(cmd_split);
exit(g_glob->exit_ret);
exit(ret);
}
4 changes: 1 addition & 3 deletions export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:56:54 by brenaudo #+# #+# */
/* Updated: 2023/01/06 10:49:20 by zhabri ### ########.fr */
/* Updated: 2023/01/09 14:22:42 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -97,11 +97,9 @@ static void handle_export_add(char *var)
else if (var_split[1][0])
edit_var(plus_char, var, var_split, env_cpy);
free_and_set_exit_ret(var_split, 0);
g_glob->exit_ret = 0;
return ;
}
free_and_set_exit_ret(var_split, 1);
g_glob->exit_ret = 1;
}

bool export_parent(void)
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: 2023/01/06 10:58:59 by zhabri ### ########.fr */
/* Updated: 2023/01/09 14:23:41 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

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

void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
Expand Down
5 changes: 1 addition & 4 deletions find_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 19:39:54 by zhabri #+# #+# */
/* Updated: 2023/01/09 11:25:09 by zhabri ### ########.fr */
/* Updated: 2023/01/09 14:22:57 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -96,9 +96,6 @@ void get_ops(const char *input, t_list **head)
double_q = !double_q;
else if (input[i] == '\'' && !double_q)
single_q = !single_q;
// if (input[i] == '$' && input[i + 1] == '$')
// while (input[i + 1] == '$')
// i++;
else if (input[i] == '$' && !ft_isprint_nospace_nodollar(input[i + 1]))
i++;
if (input[i] && ((!single_q && !double_q) \
Expand Down
4 changes: 2 additions & 2 deletions ft_split_quotes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:29 by zhabri #+# #+# */
/* Updated: 2023/01/06 11:12:42 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:46:58 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -80,7 +80,7 @@ char **ft_split_quotes(char const *s, char *sep)

if (!s)
return (NULL);
tab = ft_calloc(tab_len((char *)s, sep) + 1, sizeof(char *));
tab = ft_calloc(tab_len((char *)s, sep) + 2, sizeof(char *));
if (!tab)
return (NULL);
return (split_loop(tab, s, sep));
Expand Down
4 changes: 2 additions & 2 deletions ft_split_sep.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:29 by zhabri #+# #+# */
/* Updated: 2023/01/02 14:59:34 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:48:15 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -78,6 +78,6 @@ char **ft_split_sep(char const *s, char *sep)
return (NULL);
tab = NULL;
while (tab == NULL)
tab = ft_calloc(tab_len((char *)s, sep) + 1, sizeof(char *));
tab = ft_calloc(tab_len((char *)s, sep) + 2, sizeof(char *));
return (split_loop(tab, s, sep));
}
6 changes: 5 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: 2023/01/06 10:48:51 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:26:18 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -103,6 +103,8 @@ void eof_limiter_not_found(char *here_doc_entry, char *limiter);
void exit_child(char *cmd, int fd_out);
void exit_on_bad_cmd(char **cmd_split, \
int *pipes, char *cmd, int *children_pid);
void exit_on_error(t_cmd *cmd, char **cmd_split, \
int *pipes, int *children_pid);
void exit_on_not_existing_file(char *cmd, char **cmd_split, \
int *pipes, int *children_pid);
void exit_on_permission(char **cmd_split, \
Expand Down Expand Up @@ -135,6 +137,7 @@ void get_args(void);
void get_cmd(void);
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_ops(const char *input, t_list **head);
Expand All @@ -144,6 +147,7 @@ void handle_sigquit(void);
void ignore_sig(int sig);
void init_children_pid(int **children_pid, int size);
t_cmd *init_cmd_token(int in, int out, char *str, bool reset);
void init_ft_chdir(t_list **pwd, t_list **old_pwd, char **ret);
void init_g_glob(void);
void init_new_var(bool plus_char, char *var, char **var_split);
void init_sig_callbacks(int process);
Expand Down
13 changes: 6 additions & 7 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: 2023/01/06 11:50:07 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:27:07 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,21 +40,21 @@ void child(t_cmd *cmd, int *pipes, int *children_pid)
cmd_split = ft_split_quotes(cmd->str, " \t");
cmd_split[0] = remove_quotes(cmd_split[0]);
cmd_split[0] = get_path(cmd_split[0], false);
exit_on_not_existing_file(cmd->str, cmd_split, pipes, children_pid);
exit_on_bad_cmd(cmd_split, pipes, cmd->str, children_pid);
exit_on_permission(cmd_split, pipes, children_pid);
exit_on_error(cmd, cmd_split, pipes, children_pid);
dup_and_close(cmd, pipes);
envp = envp_list_to_tab();
clean_exit(children_pid);
execve(cmd_split[0], cmd_split, envp);
close_pipes(pipes);
exit(g_glob->exit_ret);
}
else
clean_exit(children_pid);
close_pipes(pipes);
if (cmd->fd_in != -1 && cmd->fd_out != -1)
{
clean_exit(children_pid);
exit(1);
}
clean_exit(children_pid);
exit(0);
}

Expand Down Expand Up @@ -105,7 +105,6 @@ char *get_path(char *cmd, bool shaone)
}
return (cmd);
}

return (get_path_loop(cmd, envp_entry));
}

Expand Down
3 changes: 2 additions & 1 deletion unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 11:31:23 by brenaudo #+# #+# */
/* Updated: 2023/01/05 15:18:18 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:21:10 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,6 +37,7 @@ void unset_parent_arg(char **cmd_split, int index)
ft_lstdelone(tmp, free);
}
g_glob->exit_ret = 0;
free(name);
}

bool unset_parent(void)
Expand Down
23 changes: 22 additions & 1 deletion utils_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 13:54:10 by brenaudo #+# #+# */
/* Updated: 2023/01/04 13:54:14 by brenaudo ### ########.fr */
/* Updated: 2023/01/09 13:17:03 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

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

void init_ft_chdir(t_list **pwd, t_list **old_pwd, char **ret)
{
*pwd = get_env_node("PWD");
*old_pwd = get_env_node("OLDPWD");
*ret = NULL;
}

void free_and_null(char **tmp)
{
free_null(*tmp);
Expand All @@ -26,3 +34,16 @@ void print_error_dir_cd(char *dir)
perror(err);
free(err);
}

char *get_home_content(void)
{
t_list *env_node;

env_node = get_env_node("HOME");
if (env_node == NULL)
{
ft_putstr_fd("minishell: cd: HOME not set\n", 2);
return (NULL);
}
return (env_node->content + 5);
}
9 changes: 1 addition & 8 deletions utils_pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/22 11:02:00 by zhabri #+# #+# */
/* Updated: 2023/01/03 11:16:58 by zhabri ### ########.fr */
/* Updated: 2023/01/09 13:49:05 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -76,10 +76,3 @@ void close_pipes(int *pipes)
close(pipes[i]);
}
}

void init_children_pid(int **children_pid, int size)
{
*children_pid = NULL;
while (!*children_pid)
*children_pid = ft_calloc(size, sizeof(int));
}
Loading

0 comments on commit 3642bd8

Please sign in to comment.