Skip to content

Commit

Permalink
norme and stuff lol
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 5, 2023
1 parent 25a5959 commit 68de128
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 54 deletions.
17 changes: 11 additions & 6 deletions 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/03 13:16:55 by zhabri ### ########.fr */
/* Updated: 2023/01/05 11:32:50 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,19 +34,24 @@ int builtin(char *cmd)
return (-1);
}

void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
void init_builtin_tab(t_builtin *tab[7])
{
int fd_out;
int i;
t_builtin *tab[7];

tab[0] = echo;
tab[1] = cd;
tab[2] = export;
tab[3] = env;
tab[4] = unset;
tab[5] = exit_child;
tab[6] = pwd;
}

void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
{
int fd_out;
int i;
t_builtin *tab[7];

init_builtin_tab(tab);
if (cmd->fd_out != -3)
fd_out = cmd->fd_out;
else
Expand Down
41 changes: 25 additions & 16 deletions cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 12:40:50 by zhabri #+# #+# */
/* Updated: 2023/01/03 13:25:40 by zhabri ### ########.fr */
/* Updated: 2023/01/05 11:57:24 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static t_list *get_env_node(char *var)
t_list *get_env_node(char *var)
{
t_list *envp_entry;

envp_entry = *g_glob->envp;
while (ft_strncmp((char *)envp_entry->content, var, ft_strlen(var)))
while (envp_entry \
&& ft_strncmp((char *)envp_entry->content, var, ft_strlen(var)))
envp_entry = envp_entry->next;
return (envp_entry);
}
Expand Down Expand Up @@ -78,10 +79,29 @@ void cd(char *cmd, int fd_out)
exit(0);
}

bool cd_parent_arg(char **cmd_split)
{
DIR *dir;

dir = opendir(cmd_split[1]);
if (cmd_split[2] || !dir)
{
if (cmd_split[2])
ft_putstr_fd("minishell: cd: too many arguments\n", 2);
if (!dir)
print_error_dir_cd(cmd_split[1]);
free_tab(cmd_split);
g_glob->exit_ret = 1;
return (true);
}
closedir(dir);
ft_chdir(cmd_split[1], 16);
return (false);
}

bool cd_parent(void)
{
char **cmd_split;
DIR *dir;

if (ft_lstsize(*g_glob->cmds) == 1)
{
Expand All @@ -91,19 +111,8 @@ bool cd_parent(void)
{
if (cmd_split[1])
{
dir = opendir(cmd_split[1]);
if (cmd_split[2] || !dir)
{
if (cmd_split[2])
ft_putstr_fd("minishell: cd: too many arguments\n", 2);
if (!dir)
print_error_dir_cd(cmd_split[1]);
free_tab(cmd_split);
g_glob->exit_ret = 1;
if (cd_parent_arg(cmd_split))
return (true);
}
closedir(dir);
ft_chdir(cmd_split[1], 16);
}
else
ft_chdir(get_env_node("HOME=")->content + 5, 16);
Expand Down
13 changes: 5 additions & 8 deletions env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 10:48:10 by brenaudo #+# #+# */
/* Updated: 2023/01/05 10:48:12 by brenaudo ### ########.fr */
/* Updated: 2023/01/05 11:34:52 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,19 +21,16 @@ static void env_no_arg(int fd_out)
i = 0;
while (envp[i])
{
if (ft_strncmp(envp[i], "_=", 2))
{
ft_putstr_fd(envp[i], fd_out);
ft_putchar_fd('\n', fd_out);
}
ft_putstr_fd(envp[i], fd_out);
ft_putchar_fd('\n', fd_out);
i++;
}
free_tab(envp);
free_tab(envp);
}

void env(char *cmd, int fd_out)
{
(void)cmd;
(void)cmd;
env_no_arg(fd_out);
close(fd_out);
clean_exit(NULL);
Expand Down
30 changes: 8 additions & 22 deletions export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:56:54 by brenaudo #+# #+# */
/* Updated: 2023/01/04 14:56:56 by brenaudo ### ########.fr */
/* Updated: 2023/01/05 11:59:22 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

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

static void export_no_arg(int fd_out)
Expand All @@ -20,11 +21,9 @@ static void export_no_arg(int fd_out)

envp = envp_list_to_tab();
sort_ascii(envp);
i = 0;
envp_entry_split = NULL;
while (envp[i])
i = -1;
while (envp[++i])
{
free_tab(envp_entry_split);
envp_entry_split = cut_on_first(envp[i], '=');
if (envp_entry_split && ft_strncmp(envp_entry_split[0], "_", 2))
{
Expand All @@ -38,7 +37,7 @@ static void export_no_arg(int fd_out)
}
ft_putchar_fd('\n', fd_out);
}
i++;
free_tab(envp_entry_split);
}
free_tab(envp);
}
Expand Down Expand Up @@ -81,38 +80,24 @@ static bool is_valid_identifier(char *identifier)
static void handle_export_add(char *var)
{
char **var_split;
char **env_entry_split;
t_list *env_cpy;

var_split = cut_on_first(var, '=');
env_cpy = *g_glob->envp;
env_entry_split = cut_on_first(env_cpy->content, '=');
if (is_valid_identifier(var_split[0]))
{
while (env_cpy && ft_strncmp(env_entry_split[0], var_split[0], \
ft_strlen(var_split[0]) + 1))
{
env_cpy = env_cpy->next;
if (env_cpy)
{
free_tab(env_entry_split);
env_entry_split = cut_on_first(env_cpy->content, '=');
}
}
env_cpy = get_env_node(var_split[0]);
if (env_cpy == NULL)
ft_lstadd_back(g_glob->envp, ft_lstnew(var));
ft_lstadd_back(g_glob->envp, ft_lstnew(ft_strdup(var)));
else if (var_split[1][0])
{
free(env_cpy->content);
env_cpy->content = ft_strdup(var);
}
free_tab(var_split);
free_tab(env_entry_split);
g_glob->exit_ret = 0;
return ;
}
free_tab(var_split);
free_tab(env_entry_split);
g_glob->exit_ret = 1;
return ;
}
Expand All @@ -137,6 +122,7 @@ bool export_parent(void)
handle_export_add(cmd_split[i]);
}
g_glob->exit_ret = 0;
free_tab(cmd_split);
return (true);
}
free_tab(cmd_split);
Expand Down
4 changes: 2 additions & 2 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: 2023/01/03 13:25:18 by zhabri ### ########.fr */
/* Updated: 2023/01/05 11:48:27 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -128,10 +128,10 @@ char *ft_strtrimf(char *s1, char const *set);
void get_after_op(void);
void get_args(void);
void get_cmd(void);
t_list *get_env_node(char *var);
char *get_first(void);
char *get_limiter(t_token *token);
void get_sum(char *cmd, char **ret, int *pipes);
int get_longest_str(char *s1, char *s2);
void get_ops(const char *input, t_list **head);
char *get_path(char *cmd);
void handle_sigint(void);
Expand Down

0 comments on commit 68de128

Please sign in to comment.