Skip to content

Commit

Permalink
Merge pull request #26 from Hugothms/hugo
Browse files Browse the repository at this point in the history
Final
  • Loading branch information
Hugothms authored Dec 16, 2020
2 parents 26e516a + e23894b commit 02216a6
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 113 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ minishell
# Makefile
mini_tester/**
minishell-tester/**
mini_tester
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vmoreau <vmoreau@student.42.fr> +#+ +:+ +#+ #
# By: hthomas <hthomas@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/15 20:30:49 by hthomas #+# #+# #
# Updated: 2020/12/07 18:07:07 by vmoreau ### ########.fr #
# Updated: 2020/12/15 22:18:39 by hthomas ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -22,6 +22,7 @@ SRCS += srcs/main.c
#---------------Commands---------------#

SRCS += srcs/commands/ft_cd.c \
srcs/commands/ft_cd2.c \
srcs/commands/ft_echo.c \
srcs/commands/ft_env.c \
srcs/commands/ft_exit.c \
Expand Down Expand Up @@ -95,7 +96,7 @@ fclean: clean echoFCLEAN

re: fclean all

.PHONY: clean fclean
.PHONY: all re clean fclean
.SILENT:

###########################COLOR
Expand Down
17 changes: 11 additions & 6 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: hthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/04 09:33:37 by hthomas #+# #+# */
/* Updated: 2020/12/07 19:20:30 by hthomas ### ########.fr */
/* Updated: 2020/12/15 22:20:29 by hthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -107,6 +107,11 @@ t_glob g_glob;
*/
char *ft_echo(t_list_cmd *args);
char *ft_cd(t_list_cmd *args, t_list *env);
char *ft_cd2(t_list_cmd *args, t_list *env,
struct stat *stats);
char *error_cd(char *arg, int err_status);
int test_cd_home(t_list_cmd *args, t_list *env,
struct stat *stats);
char *ft_pwd(void);
char *ft_export(t_list_cmd *args, t_list *env);
void add_quote(char **tri);
Expand All @@ -115,7 +120,7 @@ int have_equal(char *str);
void print_exp_err(char **key, char **value);
char *ft_unset(t_list_cmd *args, t_list *env);
char *ft_env(t_list *env);
char *ft_exit(t_list_cmd *args, t_list *env);
char *ft_exit(t_list_line *lst_line, t_list *env);
void clear_env_lst(t_list *env);

/*
Expand Down Expand Up @@ -161,14 +166,14 @@ void delete_empty_elements(t_list_cmd **cmd);
void parse_error(char *input, t_list_line *lst_line);
int print_synt_err(void);
void print_prompt(void);
void not_found(char *cmd);
void not_found(t_list_line *lst_line, t_list *env);

/*
** exec_line.c
*/
void exec_line(t_list_line *lst_line, t_list *env);
t_list_cmd *reparse_var_env(t_list_cmd *cmd);
char *exec_cmd(t_list_cmd *cmd, t_list *env);
char *exec_cmd(t_list_line *lst_line, t_list *env);
int make_and_exec_cmd(t_list_line *lst_line, t_list *env);
t_list_cmd *split_add_back(t_list_cmd *cmd,
void (*del)(t_list_cmd *), t_list_cmd *to_del);
Expand All @@ -187,7 +192,7 @@ void set_env(char **envp, t_list **env);
** pipe.c
*/
int create_pipe(t_list_line **lst_line, t_list \
*env, int fd_inold);
*env, int fd_inold, int *nb_wait);

/*
** redirection.c
Expand All @@ -197,7 +202,7 @@ int redirections(t_list_line *lst_line);
/*
** searche_command.c
*/
int search_command(t_list_cmd *cmd, t_list *env);
int search_command(t_list_line *lst_line, t_list *env);

/*
** List
Expand Down
18 changes: 7 additions & 11 deletions srcs/commands/ft_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: hthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/22 16:16:24 by vmoreau #+# #+# */
/* Updated: 2020/12/07 18:57:59 by hthomas ### ########.fr */
/* Updated: 2020/12/15 22:20:37 by hthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,7 @@ static void modif_oldpwd_pwd(t_list *env)
modif_var_env(env, "OLDPWD", &find_var_env(env, "PWD=")[4]);
if (!pwd)
{
ft_putstr_fd("No such file or directory\n", STDERR);
free(pwd);
pwd = ft_strdup(&find_var_env(env, "PWD=")[4]);
pwd = ft_strjoin_free(pwd, "/..");
Expand All @@ -30,7 +31,7 @@ static void modif_oldpwd_pwd(t_list *env)
free(pwd);
}

static char *error_cd(char *arg, int err_status)
char *error_cd(char *arg, int err_status)
{
char *ret;

Expand Down Expand Up @@ -75,7 +76,7 @@ static char *cd_oldpwd(t_list *env, t_list_cmd *arg, struct stat *stats)
return (ret);
}

static int test_cd_home(t_list_cmd *args, t_list *env, struct stat *stats)
int test_cd_home(t_list_cmd *args, t_list *env, struct stat *stats)
{
if (stat(&find_var_env(env, "HOME=")[5], stats) != 0)
return (1);
Expand All @@ -88,18 +89,13 @@ static int test_cd_home(t_list_cmd *args, t_list *env, struct stat *stats)
char *ft_cd(t_list_cmd *args, t_list *env)
{
struct stat stats;
char *ret;

g_glob.exit = 0;
if ((!args || !args->str))
{
if (chdir(&find_var_env(env, "HOME=")[5]))
if (find_var_env(env, "HOME=") == NULL)
return (error_cd("« HOME »", 3));
else if (find_var_env(env, "HOME=")[5] == '\0')
return (ft_strdup(""));
else if (test_cd_home(args, env, &stats))
return (error_cd(&find_var_env(env, "HOME=")[5],
test_cd_home(args, env, &stats)));
if ((ret = ft_cd2(args, env, &stats)))
return (ret);
}
else if (c_lst_size(args) > 1)
return (error_cd(args->str, 0));
Expand Down
28 changes: 28 additions & 0 deletions srcs/commands/ft_cd2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cd2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/15 22:17:31 by hthomas #+# #+# */
/* Updated: 2020/12/16 10:01:30 by hthomas ### ########.fr */
/* */
/* ************************************************************************** */

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

char *ft_cd2(t_list_cmd *args, t_list *env, struct stat *stats)
{
if (chdir(&find_var_env(env, "HOME=")[5]))
{
if (find_var_env(env, "HOME=") == NULL)
return (error_cd("« HOME »", 3));
else if (find_var_env(env, "HOME=")[5] == '\0')
return (ft_strdup(""));
else if (test_cd_home(args, env, stats))
return (error_cd(&find_var_env(env, "HOME=")[5],
test_cd_home(args, env, stats)));
}
return (NULL);
}
12 changes: 7 additions & 5 deletions srcs/commands/ft_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hthomas <hthomas@student.42.fr> +#+ +:+ +#+ */
/* By: vmoreau <vmoreau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/22 16:16:34 by vmoreau #+# #+# */
/* Updated: 2020/12/07 19:10:11 by hthomas ### ########.fr */
/* Updated: 2020/12/10 16:35:05 by vmoreau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -64,18 +64,20 @@ static int check_args(t_list_cmd *args)
return (ret);
}

char *ft_exit(t_list_cmd *args, t_list *env)
char *ft_exit(t_list_line *lst_line, t_list *env)
{
int err;

err = check_args(args);
err = check_args(lst_line->cmd->next);
if (err >= 0)
{
clear_env_lst(env);
free(g_glob.path);
if (!err)
if (args && (err = ft_atoi_strict(args->str)))
if (lst_line->cmd->next &&
(err = ft_atoi_strict(lst_line->cmd->next->str)))
g_glob.exit = err;
l_lst_clear(lst_line);
exit(g_glob.exit);
}
return (NULL);
Expand Down
4 changes: 3 additions & 1 deletion srcs/commands/ft_export2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: hthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/22 16:16:36 by vmoreau #+# #+# */
/* Updated: 2020/12/07 19:05:39 by hthomas ### ########.fr */
/* Updated: 2020/12/15 21:48:32 by hthomas ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,10 +20,12 @@ int have_equal(char *str)
while (str[i])
{
if (str[i] == '=')
{
if (i == 0)
return (1);
else
return (i);
}
i++;
}
return (0);
Expand Down
7 changes: 3 additions & 4 deletions srcs/commands/ft_unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
/* By: hthomas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/22 16:16:41 by vmoreau #+# #+# */
/* Updated: 2020/12/07 19:02:23 by hthomas ### ########.fr */
/* Updated: 2020/12/15 21:49:42 by hthomas ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include <stdio.h>

static char **catch_arg(t_list_cmd *args)
{
Expand Down Expand Up @@ -44,7 +43,7 @@ static void lst_del_next_node(t_list *env, char *var)
tmp = env->next->next;
free(env->next->content);
free(env->next);
env->next == NULL;
env->next = NULL;
env->next = tmp;
break ;
}
Expand All @@ -69,7 +68,7 @@ char *ft_unset(t_list_cmd *args, t_list *env)
else
{
g_glob.exit = 1;
ft_putstr_fd("minishell: export: « ", STDERR);
ft_putstr_fd("minishell: unset: « ", STDERR);
ft_putstr_fd(var[i], STDERR);
ft_putstr_fd(" » : not valid identifier\n", STDERR);
}
Expand Down
10 changes: 6 additions & 4 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hthomas <hthomas@student.42.fr> +#+ +:+ +#+ */
/* By: vmoreau <vmoreau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/15 19:21:43 by hthomas #+# #+# */
/* Updated: 2020/12/07 19:20:36 by hthomas ### ########.fr */
/* Updated: 2020/12/11 16:32:54 by vmoreau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,21 +38,23 @@ static void sighandler(int signum)
{
kill(g_glob.pid, signum);
ft_putstr_fd("\n", STDERR);
g_glob.exit = 130;
g_glob.pid = 0;
}
else if (g_glob.pid && signum == SIGQUIT)
{
kill(g_glob.pid, signum);
ft_putstr_fd("Quit (core dumped)\n", STDERR);
g_glob.exit = 131;
g_glob.pid = 0;
}
else
{
ft_putstr_fd("\n", STDERR);
print_prompt();
}
if (signum == SIGINT)
g_glob.exit = 130;
if (signum == SIGQUIT)
g_glob.exit = 131;
}

static void init_main(t_list **env, char **envp)
Expand Down
Loading

0 comments on commit 02216a6

Please sign in to comment.