Skip to content

Commit

Permalink
done, norme à faire et leaks à détect
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Renaudon authored and Baptiste Renaudon committed Jan 5, 2023
1 parent 331a637 commit 25a5959
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SRCS = add_cmd.c \
cd.c \
dollar.c \
echo.c \
env.c \
errors.c \
exit.c \
export.c \
Expand Down
1 change: 1 addition & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
tab[0] = echo;
tab[1] = cd;
tab[2] = export;
tab[3] = env;
tab[4] = unset;
tab[5] = exit_child;
tab[6] = pwd;
Expand Down
41 changes: 41 additions & 0 deletions env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 10:48:10 by brenaudo #+# #+# */
/* Updated: 2023/01/05 10:48:12 by brenaudo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static void env_no_arg(int fd_out)
{
int i;
char **envp;

envp = envp_list_to_tab();
i = 0;
while (envp[i])
{
if (ft_strncmp(envp[i], "_=", 2))
{
ft_putstr_fd(envp[i], fd_out);
ft_putchar_fd('\n', fd_out);
}
i++;
}
free_tab(envp);
}

void env(char *cmd, int fd_out)
{
(void)cmd;
env_no_arg(fd_out);
close(fd_out);
clean_exit(NULL);
exit(0);
}
32 changes: 26 additions & 6 deletions export.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static void export_no_arg(int fd_out)
}
i++;
}
free_tab(envp);
}

void export(char *cmd, int fd_out)
Expand All @@ -54,13 +55,26 @@ void export(char *cmd, int fd_out)
exit(0);
}

/*TODO : verif si identifier valable (je crois que que lettres possibles
ou un truc comme ça)
*/

static bool is_valid_identifier(char *identifier)
{
(void)identifier;
int i;
char *err;

i = 1;
if (!ft_isalpha(identifier[0]) && identifier[0] != '_')
return (false);
while (identifier[i])
{
if (!ft_isalnum(identifier[i]) && identifier[i] != '_')
{
err = ft_strjoin("minishell: cd: ", identifier);
err = ft_strjoinf(err, ": not a valid identifier\n");
ft_putstr_fd(err, 2);
free(err);
return (false);
}
i++;
}
return (true);
}

Expand All @@ -76,7 +90,7 @@ static void handle_export_add(char *var)
if (is_valid_identifier(var_split[0]))
{
while (env_cpy && ft_strncmp(env_entry_split[0], var_split[0], \
ft_strlen(longest_str(env_entry_split[0], var_split[0])) + 1))
ft_strlen(var_split[0]) + 1))
{
env_cpy = env_cpy->next;
if (env_cpy)
Expand All @@ -92,9 +106,15 @@ static void handle_export_add(char *var)
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 ;
}

bool export_parent(void)
Expand Down
1 change: 1 addition & 0 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void clear_cmds(void);
void close_pipes(int *pipes);
char **cut_on_first(char *str, char sep);
void echo(char *cmd, int fd_out);
void env(char *cmd, int fd_out);
char **envp_list_to_tab(void);
void eof_limiter_not_found(char *here_doc_entry, char *limiter);
void exit_child(char *cmd, int fd_out);
Expand Down
7 changes: 5 additions & 2 deletions unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ bool unset_parent(void)
ft_strncmp(env_cpy->next->content, name, ft_strlen(name)))
env_cpy = env_cpy->next;
tmp = env_cpy->next;
env_cpy->next = tmp->next;
ft_lstdelone(tmp, free);
if (tmp != NULL)
{
env_cpy->next = tmp->next;
ft_lstdelone(tmp, free);
}
free_tab(cmd_split);
g_glob->exit_ret = 0;
return (true);
Expand Down
17 changes: 0 additions & 17 deletions utils_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,3 @@ char **cut_on_first(char *str, char sep)
ret[1] = ft_substr(str, i + 1, ft_strlen(str) - i);
return (ret);
}

char *longest_str(char *s1, char *s2)
{
int i;
int j;

i = 0;
j = 0;
while (s1 && s1[i])
i++;
while (s2 && s2[j])
j++;
if (i > j)
return (s1);
else
return (s2);
}

0 comments on commit 25a5959

Please sign in to comment.