From 5bdde9b3dae8f7c3a086f56b220d7a711740a775 Mon Sep 17 00:00:00 2001 From: PrStein Date: Mon, 11 Apr 2022 21:55:33 +0200 Subject: [PATCH 1/3] cd and unset --- srcs/built_in/export.c | 20 +++++++++++--------- srcs/built_in/export2.c | 5 ++++- srcs/built_in/unset.c | 17 +++++++++-------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/srcs/built_in/export.c b/srcs/built_in/export.c index 3f9c4c5..e12dead 100644 --- a/srcs/built_in/export.c +++ b/srcs/built_in/export.c @@ -17,16 +17,16 @@ 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) @@ -66,12 +66,14 @@ 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); unset = ft_substr(dest, 0, find_len(dest, 0, '=')); printf("%s\n", unset); 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/unset.c b/srcs/built_in/unset.c index 5066ba8..8f41bb5 100644 --- a/srcs/built_in/unset.c +++ b/srcs/built_in/unset.c @@ -58,21 +58,22 @@ 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++; + // 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) From c2f08b31e36f556a6cb4a5aa4e71555bf1c2b3c0 Mon Sep 17 00:00:00 2001 From: PrStein Date: Tue, 12 Apr 2022 01:14:10 +0200 Subject: [PATCH 2/3] segfault cd after unset PWD after changed fint_it --- srcs/built_in/cd.c | 13 ++++++--- srcs/built_in/export.c | 53 ++++++++++++------------------------ srcs/built_in/init_builtin.c | 16 +++++------ srcs/built_in/unset.c | 1 - 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/srcs/built_in/cd.c b/srcs/built_in/cd.c index 9195914..ba82ae0 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 { diff --git a/srcs/built_in/export.c b/srcs/built_in/export.c index e12dead..b44e7a0 100644 --- a/srcs/built_in/export.c +++ b/srcs/built_in/export.c @@ -29,36 +29,26 @@ int check_variable(char *variable) 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; @@ -74,16 +64,15 @@ void ft_export_variable(t_s_cmd *cmd, char *variable) // 1 return ; } - 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++; @@ -91,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/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 8f41bb5..9bb5122 100644 --- a/srcs/built_in/unset.c +++ b/srcs/built_in/unset.c @@ -69,7 +69,6 @@ void find_variable(char *variable, t_s_cmd *cmd) // 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); From bdccd9f1e4cda51e20f40b7bde9aa8ba3f02cd9d Mon Sep 17 00:00:00 2001 From: PrStein Date: Tue, 12 Apr 2022 23:10:00 +0200 Subject: [PATCH 3/3] unset --- includes/minishell.h | 3 ++- srcs/built_in/cd.c | 17 ---------------- srcs/built_in/choose_builtin.c | 2 +- srcs/built_in/env.c | 37 ++++++++++++++++++++++++++-------- srcs/built_in/export.c | 4 ++-- srcs/built_in/unset.c | 8 ++++++++ 6 files changed, 42 insertions(+), 29 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 018d18d..11ddfe3 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -99,9 +99,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 ba82ae0..c65363a 100644 --- a/srcs/built_in/cd.c +++ b/srcs/built_in/cd.c @@ -200,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 b44e7a0..a8a01ad 100644 --- a/srcs/built_in/export.c +++ b/srcs/built_in/export.c @@ -58,9 +58,9 @@ void ft_export_variable(t_s_cmd *cmd, char *variable) i = 0; if (check_variable(variable) == 1) { - ft_putstr_fd("Minishell: export: ", 2); + ft_putstr_fd("Minishell: export: `", 2); ft_putstr_fd(variable, 2); - ft_putstr_fd(": not a valid identifier\n", 2); + ft_putstr_fd("': not a valid identifier\n", 2); // 1 return ; } diff --git a/srcs/built_in/unset.c b/srcs/built_in/unset.c index 9bb5122..e7dec68 100644 --- a/srcs/built_in/unset.c +++ b/srcs/built_in/unset.c @@ -65,6 +65,14 @@ void find_variable(char *variable, t_s_cmd *cmd) i = find_it(cmd->big_cmd->env, variable); // str = ft_strjoin(variable, "="); j = 0; + 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))