diff --git a/builtins/cd.c b/builtins/cd.c index 92d4291..b621fcd 100644 --- a/builtins/cd.c +++ b/builtins/cd.c @@ -6,15 +6,30 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */ -/* Updated: 2023/02/28 13:37:18 by erey-bet ### ########.fr */ +/* Updated: 2023/03/09 20:13:05 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "./builtins_private.h" -int move_folder(char **args, int fd) +int make_move(char *path, int fd) { char *join; + + join = ft_strjoin("/", path); + join = ft_strfjoin(get_pwd(fd), join); + if (chdir(join) == 0) + { + free(join); + return (0); + } + free(join); + write(2, "No suck file or directory", 25); + return (1); +} + +int move_folder(char **args, int fd) +{ char *path; if (args[1] != NULL) @@ -31,16 +46,5 @@ int move_folder(char **args, int fd) return (1); } else - { - join = ft_strjoin("/", path); - join = ft_strfjoin(get_pwd(fd), join); - if (chdir(join) == 0) - { - free(join); - return (0); - } - free(join); - write(2, "No suck file or directory", 25); - return (1); - } + return (make_move(path, fd)); } diff --git a/builtins/env.c b/builtins/env.c index da00299..7d6786d 100644 --- a/builtins/env.c +++ b/builtins/env.c @@ -6,7 +6,7 @@ /* By: cchauvet next != NULL) { - ft_putstr_fd(((t_env *)(current->content))->key, fd); - ft_putstr_fd("=", fd); - ft_putstr_fd(((t_env *)(current->content))->value, fd); - write(fd, "\n", 1); + if (((t_env *)(current->content))->original) + { + ft_putstr_fd(((t_env *)(current->content))->key, fd); + ft_putstr_fd("=", fd); + ft_putstr_fd(((t_env *)(current->content))->value, fd); + write(fd, "\n", 1); + } current = current->next; } return (0); diff --git a/builtins/exit.c b/builtins/exit.c index a059e50..a2ce1b9 100644 --- a/builtins/exit.c +++ b/builtins/exit.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/24 10:17:59 by erey-bet #+# #+# */ -/* Updated: 2023/02/28 14:55:39 by erey-bet ### ########.fr */ +/* Updated: 2023/03/09 20:08:56 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,11 +31,11 @@ int ft_exit(char **args) if (args[0] == NULL) return (0); err = ft_atoi_check(args[0]); - if (err == 1) + if (err == 1) return (error(err, "numeric argument required", args[0])); if (args[1] != NULL) - return (error(-1, "too many arguments", NULL)); + return (error(1, "too many arguments", NULL)); if (err > 0) - return(error(err, "numeric argument required", args[0])); + return (error(err, "numeric argument required", args[0])); return ((ft_atoi(args[0]) % 256 + 256) % 256); } diff --git a/builtins/export.c b/builtins/export.c index 81db1ab..a72c855 100644 --- a/builtins/export.c +++ b/builtins/export.c @@ -6,7 +6,7 @@ /* By: cchauvet content))->key, fd); - ft_putstr_fd("=", fd); - write(fd, "\"", 1); - ft_putstr_fd(((t_env *)(current->content))->value, fd); - write(fd, "\"\n", 2); + if (((t_env *)(current->content))->value != NULL) + { + ft_putstr_fd("=", fd); + write(fd, "\"", 1); + ft_putstr_fd(((t_env *)(current->content))->value, fd); + write(fd, "\"\n", 2); + } + else + write(fd, "\n", 2); current = current->next; } } -int add_export(t_list **env, char *args, int fd, int *err) +int set_key_value_export(t_list **env, char *args, char **key, char **value) +{ + *key = ft_strndup(args, ft_strnchr(args, '=')); + if (*key == NULL) + return (1); + if (ft_strlen(*key) == 0) + return (1); + if (possible_key(*key) == 2) + { + (*key)[ft_strlen(*key) - 1] = '\0'; + if (get_value_by_key(*key, env) == NULL) + *value = ft_strdup(ft_strchr(args, '=') + 1); + else + *value = ft_strjoin(get_value_by_key(*key, env), + ft_strchr(args, '=') + 1); + } + else + *value = ft_strdup(ft_strchr(args, '=') + 1); + return (0); +} + +int add_export(t_list **env, char *args, int fd) { char *key; char *value; - key = args; - value = ""; if (ft_strchr(args, '=') != NULL) { - key = ft_strndup(args, ft_strnchr(args, '=')); - if (key == NULL) - return (1); - if (ft_strlen(key) == 0) - { - error(args, fd); - return (1); - } - value = ft_strchr(args, '=') + 1; + if (set_key_value_export(env, args, &key, &value)) + return (error(args, fd)); + } + else + { + key = ft_strdup(args); + value = get_value_by_key(key, env); + if (value != NULL) + value = ft_strdup(value); + if (possible_key(key) == 2) + return (error(key, fd)); } if (!possible_key(key)) - { - *err = error(key, fd); - return (1); - } - create_value_by_key_dup(key, value, env); + return (error(args, fd)); + create_value_by_key(key, value, env); return (0); } @@ -77,7 +100,7 @@ int ft_export(t_list **env, char **args, int fd) { i = -1; while (args[++i]) - if (add_export(env, args[i], fd, &err) == 1) + if (add_export(env, args[i], fd) == 1) err = 1; } return (err); diff --git a/builtins/pwd.c b/builtins/pwd.c index 3f3fd8f..b803d70 100644 --- a/builtins/pwd.c +++ b/builtins/pwd.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ -/* Updated: 2023/02/21 15:11:38 by cchauvet ### ########.fr */ +/* Updated: 2023/03/09 20:00:19 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,9 @@ int pwd(int fd) { char path[PATH_MAX]; - if (getcwd(path, sizeof(path)) != NULL) + if (getcwd(path, sizeof(path)) != NULL) ft_putendl_fd(path, fd); - else + else { ft_putendl_fd("Error getcwd", fd); return (1); @@ -31,9 +31,9 @@ char *get_pwd(int fd) char *str; str = ft_calloc(PATH_MAX, sizeof(char *)); - if (getcwd(str, PATH_MAX) != NULL) + if (getcwd(str, PATH_MAX) != NULL) return (str); - else + else { ft_putendl_fd("Error getcwd", fd); return (NULL); diff --git a/env/env.h b/env/env.h index c7da96e..c3777cb 100644 --- a/env/env.h +++ b/env/env.h @@ -1,5 +1,6 @@ #ifndef ENV_H # define ENV_H +# include # include "../libftx/libft/list.h" # include "../data/data.h" @@ -7,6 +8,7 @@ typedef struct s_env { char *key; char *value; + bool original; } t_env; char *ft_env_filler(t_data *data, const char *str); diff --git a/env/env1.c b/env/env1.c index 239df46..1c2649a 100644 --- a/env/env1.c +++ b/env/env1.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ -/* Updated: 2023/02/23 13:34:41 by erey-bet ### ########.fr */ +/* Updated: 2023/03/09 19:01:05 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ char *get_value_by_key(char *key, t_list **head) return (((t_env *)current->content)->value); current = current->next; } - return (""); + return (NULL); } int set_value_by_key(char *key, char *value, t_list **head) @@ -80,6 +80,7 @@ int create_value_by_key(char *key, char *value, t_list **head) return (1); content->key = key; content->value = value; + content->original = 0; add_sort(head, content); return (0); } @@ -104,6 +105,7 @@ t_list **init_env(char **env) return (NULL); var->key = get_key(env[i]); var->value = get_value(env[i]); + var->original = 1; add_sort(head, var); } return (head); diff --git a/env/env2.c b/env/env2.c index 59ad638..2a3e5e9 100644 --- a/env/env2.c +++ b/env/env2.c @@ -1,11 +1,12 @@ +/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/02/17 17:22:01 by erey-bet #+# #+# */ -/* Updated: 2023/02/17 17:24:57 by erey-bet ### ########.fr */ +/* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */ +/* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/env/env3.c b/env/env3.c index 3635228..d6b2653 100644 --- a/env/env3.c +++ b/env/env3.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ -/* Updated: 2023/02/28 12:46:33 by erey-bet ### ########.fr */ +/* Updated: 2023/03/09 19:58:55 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,19 +43,22 @@ int create_value_by_key_dup(char *key, char *value, t_list **env) char *key_dup; char *value_dup; + if (set_value_by_key(key, value, env) == 0) + return (0); key_dup = ft_strdup(key); if (key_dup == NULL) - { - ft_eprintf("minishell: malloc failed\n"); return (1); - } - value_dup = ft_strdup(value); - if (value_dup == NULL) + if (value != NULL) { - free(key); - ft_eprintf("minishell: malloc failed\n"); - return (1); + value_dup = ft_strdup(value); + if (value_dup == NULL) + { + free(key); + return (1); + } } + else + value_dup = value; if (create_value_by_key(key_dup, value_dup, env)) return (1); return (0); @@ -89,9 +92,13 @@ int possible_key(char *key) i = -1; if (ft_isdigit(key[i + 1])) - return (0); - while (key[++i]) + return (0); + while (key[++i + 1]) if (!ft_isalnum(key[i]) && key[i] != '_') return (0); + if (key[i] == '+') + return (2); + else if (!ft_isalnum(key[i]) && key[i] != '_') + return (0); return (1); } diff --git a/minishell.h b/minishell.h index 8cc4505..aed8af2 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet