diff --git a/includes/minishell.h b/includes/minishell.h index 2564fa4..f375db2 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -104,9 +104,10 @@ int find_len(char *input, int i, char c); void lone_export(t_s_cmd *cmd); void find_variable(char *variable, t_s_cmd *cmd); int find_it(char **str, char *s); +int check_variable(char *variable); //real builtin -void print_env(char **tab); +void print_env(t_cmd *cmd); void ft_env(t_s_cmd *cmd, char **env); void ft_exit(t_s_cmd *cmd); void ft_export(t_s_cmd *cmd); diff --git a/srcs/built_in/cd.c b/srcs/built_in/cd.c index 9195914..c65363a 100644 --- a/srcs/built_in/cd.c +++ b/srcs/built_in/cd.c @@ -111,6 +111,8 @@ void change_path(t_s_cmd *cmd) i++; } + if (tab) + free_double(tab); } void reboot_pwd(t_s_cmd *cmd, int i) @@ -145,7 +147,7 @@ void open_directory(t_s_cmd *cmd) char **str; int j; - if (find_it(cmd->big_cmd->env, "PWD")) + if (find_it(cmd->big_cmd->env, "PWD") != -1) { str = ft_split(cmd->big_cmd->env[find_it(cmd->big_cmd->env, "PWD")], '/'); j = double_size(str); @@ -167,9 +169,9 @@ void open_directory(t_s_cmd *cmd) return ; } char *p = ft_substr(cmd->big_cmd->env[find_it(cmd->big_cmd->env, "HOME")], 5, ft_strlen(cmd->big_cmd->env[find_it(cmd->big_cmd->env, "HOME")])); - printf("%s\n", p); + // printf("%s\n", p); if (chdir(p) == 0) - if (find_it(cmd->big_cmd->env, "PWD")) + if (find_it(cmd->big_cmd->env, "PWD") != -1) { cmd->big_cmd->env[find_it(cmd->big_cmd->env, "OLDPWD")] = ft_strjoin("OLD", cmd->big_cmd->env[find_it(cmd->big_cmd->env, "PWD")]); cmd->big_cmd->env[find_it(cmd->big_cmd->env, "PWD")] = ft_strjoin("PWD=", p); @@ -182,8 +184,11 @@ void open_directory(t_s_cmd *cmd) reboot_pwd(cmd, j); if (chdir(cmd->args[1]) == 0) { - if (find_it(cmd->big_cmd->env, "PWD")) + if (find_it(cmd->big_cmd->env, "PWD") != -1) + { change_path(cmd); + + } } else { @@ -195,20 +200,3 @@ void open_directory(t_s_cmd *cmd) } } -void ft_pwd(t_s_cmd *cmd) -{ - int i; - char p[1024]; - char *str; - - i = 1; - str = getcwd(p, sizeof(p)); - if (!str) - { - cmd->big_cmd->err_var = 1; - ft_putstr_fd("Minishell: pwd: Not found\n", 2); - } - else - ft_putendl_fd(p, 1); - -} \ No newline at end of file diff --git a/srcs/built_in/choose_builtin.c b/srcs/built_in/choose_builtin.c index f65a58d..9d4b872 100644 --- a/srcs/built_in/choose_builtin.c +++ b/srcs/built_in/choose_builtin.c @@ -32,7 +32,7 @@ int is_builtin(char *cmd) void call_builtin(t_cmd *cmd) { if (!ft_strcmp(cmd->current_s_cmd->cmd, "env")) - print_env(cmd->env); + print_env(cmd); if (!ft_strcmp(cmd->current_s_cmd->cmd, "export")) ft_export(cmd->current_s_cmd); if (!ft_strcmp(cmd->current_s_cmd->cmd, "unset")) diff --git a/srcs/built_in/env.c b/srcs/built_in/env.c index f734260..2b210ee 100644 --- a/srcs/built_in/env.c +++ b/srcs/built_in/env.c @@ -12,21 +12,42 @@ #include "../../includes/minishell.h" -void print_env(char **tab) +void print_env(t_cmd *cmd) { int i; i = 0; - if (tab) + if (cmd->current_s_cmd->nb_args > 1) { - while (tab[i]) + ft_putstr_fd("Minishell: env: '", 2); + ft_putstr_fd(cmd->current_s_cmd->args[1], 2); + ft_putstr_fd("': No such file or directory\n", 2); + // 127 + } + else if (cmd->env) + { + while (cmd->env[i]) { - if (ft_strchr(tab[i], '=')) - { - ft_putstr_fd(tab[i], 1); - ft_putstr_fd("\n", 1); - } + if (ft_strchr(cmd->env[i], '=')) + ft_putendl_fd(cmd->env[i], 1); i++; } } +} + +void ft_pwd(t_s_cmd *cmd) +{ + int i; + char p[1024]; + char *str; + + i = 1; + str = getcwd(p, sizeof(p)); + if (!str) + { + cmd->big_cmd->err_var = 1; + ft_putstr_fd("Minishell: pwd: Not found\n", 2); + } + else + ft_putendl_fd(p, 1); } \ No newline at end of file diff --git a/srcs/built_in/export.c b/srcs/built_in/export.c index 3f9c4c5..a8a01ad 100644 --- a/srcs/built_in/export.c +++ b/srcs/built_in/export.c @@ -17,48 +17,38 @@ int check_variable(char *variable) int i; i = 0; - if (!ft_isalpha(variable[i])) - return(0); + if (!ft_isalpha(variable[i]) && variable[i] != '_') + return(1); i++; - while(variable[i] != '=') + while(variable[i] && variable[i] != '=') { - if(!ft_isalnum(variable[i])) - return(0); + if(!ft_isalnum(variable[i]) && variable[i] != '_') + return(1); i++; } - return(1); + return(0); } -// char *define_double_quotes(char *value) +// char *check_value(char *value) // { // int i; // char *dest; -// -// i = find_len(value, 0, '=') + 2; + +// i = find_len(value, 0, '=') + 1; // if (value[i] == '"') -// dest = ft_substr() +// { +// printf("wait\n"); +// dest = ft_strdup(" "); +// // dest = define_double_quotes(value); +// } +// else{ +// dest = ft_substr(value, 0, find_len(value, 0, ' ')); +// // printf("brrrrrrr------\n"); + +// } +// return (dest); // } -char *check_value(char *value) -{ - int i; - char *dest; - - i = find_len(value, 0, '=') + 1; - if (value[i] == '"') - { - printf("wait\n"); - dest = ft_strdup(" "); - // dest = define_double_quotes(value); - } - else{ - dest = ft_substr(value, 0, find_len(value, 0, ' ')); - // printf("brrrrrrr------\n"); - - } - return (dest); -} - void ft_export_variable(t_s_cmd *cmd, char *variable) { char *dest; @@ -66,22 +56,23 @@ void ft_export_variable(t_s_cmd *cmd, char *variable) int i; i = 0; - if (check_variable(variable) == 0) + if (check_variable(variable) == 1) { - printf("Voir bash\n"); + ft_putstr_fd("Minishell: export: `", 2); + ft_putstr_fd(variable, 2); + ft_putstr_fd("': not a valid identifier\n", 2); + // 1 return ; } - // printf("jojo\n"); - dest = check_value(variable); + // dest = check_value(variable); + dest = ft_strdup(variable); unset = ft_substr(dest, 0, find_len(dest, 0, '=')); - printf("%s\n", unset); while (cmd->big_cmd->env[i] != NULL) { if (ft_strncmp(unset, cmd->big_cmd->env[i], ft_strlen(unset)) == 0) { free(cmd->big_cmd->env[i]); cmd->big_cmd->env[i] = dest; - // find_variable(unset, cmd); return ; } i++; @@ -89,14 +80,8 @@ void ft_export_variable(t_s_cmd *cmd, char *variable) register_env(cmd, dest); if (dest) free(dest); - // if (unset) - // free(unset); - // if (check_variable(v_v[0]) == 1) - // { - // tmp = ft_strdup(v_v[1]); - // free(v_v[1]); - // v_v[1] = check_value(tmp); - // } + if (unset) + free(unset); } void ft_export(t_s_cmd *cmd) diff --git a/srcs/built_in/export2.c b/srcs/built_in/export2.c index e494de0..ffc5cc4 100644 --- a/srcs/built_in/export2.c +++ b/srcs/built_in/export2.c @@ -54,7 +54,10 @@ void print_export(char *tmp) str1 = ft_substr(tmp, 0, find_len(tmp, i, '=')); i = find_len(tmp, 0, '=') + 1; str2 = ft_substr(tmp, i, find_len(tmp, i, '\0')); - printf("declare -x %s=\"%s\"\n", str1, str2); + printf("declare -x %s", str1); + if (next_space(str2, 0) != '\0') + printf("=\"%s\"", str2); + printf("\n"); free(str1); free(str2); diff --git a/srcs/built_in/init_builtin.c b/srcs/built_in/init_builtin.c index 36de6c5..c19a462 100644 --- a/srcs/built_in/init_builtin.c +++ b/srcs/built_in/init_builtin.c @@ -108,15 +108,15 @@ void ft_env(t_s_cmd *cmd, char **env) cmd->big_cmd->env[i] = NULL; } -int find_pwd(t_s_cmd *cmd) -{ - int i; +// int find_pwd(t_s_cmd *cmd) +// { +// int i; - i = 0; - while (cmd->big_cmd->env[i] && ft_strncmp(cmd->big_cmd->env[i], "PWD=", 4) != 0) - i++; - return (i); -} +// i = 0; +// while (cmd->big_cmd->env[i] && ft_strncmp(cmd->big_cmd->env[i], "PWD=", 4) != 0) +// i++; +// return (i); +// } void init_s_cmd(t_s_cmd *cmd, char **env) { diff --git a/srcs/built_in/unset.c b/srcs/built_in/unset.c index 5066ba8..e7dec68 100644 --- a/srcs/built_in/unset.c +++ b/srcs/built_in/unset.c @@ -58,21 +58,29 @@ void unset_variable(t_s_cmd *cmd, int i) void find_variable(char *variable, t_s_cmd *cmd) { - char *str; + // char *str; int i; int j; - i = 0; - str = ft_strjoin(variable, "="); + i = find_it(cmd->big_cmd->env, variable); + // str = ft_strjoin(variable, "="); j = 0; - while (str[j]) - j++; - while (cmd->big_cmd->env[i] && !(ft_strncmp(cmd->big_cmd->env[i], str, j) == 0)) - i++; + if (check_variable(variable) == 1) + { + ft_putstr_fd("Minishell: unset: `", 2); + ft_putstr_fd(variable, 2); + ft_putstr_fd("': not a valid identifier\n", 2); + // 2 + return ; + } + // while (str[j]) + // j++; + // while (cmd->big_cmd->env[i] && !(ft_strncmp(cmd->big_cmd->env[i], str, j) == 0)) + // i++; if (i == tab_len(cmd->big_cmd->env)) return ; unset_variable(cmd, i); - free(str); + // free(str); } void ft_unset(t_s_cmd *cmd)