This commit is contained in:
Etienne Rey-bethbeder 2023-04-18 13:30:17 +02:00
parent 3e7dcd1ba9
commit 62e887cfbd
8 changed files with 39 additions and 22 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:41:30 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:41:30 by cchauvet #+# #+# */
/* Updated: 2023/04/05 12:16:50 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 12:57:36 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,10 +16,10 @@
int echo(int fd, char **strs); int echo(int fd, char **strs);
int pwd(int fd); int pwd(int fd);
char *get_pwd(int fd); char *get_pwd(void);
int print_env(t_list **env, int fd); int print_env(t_list **env, int fd);
int ft_export(t_list **env, char **args, int fd); int ft_export(t_list **env, char **args, int fd);
int move_folder(char **args, int fd); int move_folder(char **args, t_list **env, int fd);
int unset(t_list **env, char **args, int fd); int unset(t_list **env, char **args, int fd);
int ft_exit(char **args, int err); int ft_exit(char **args, int err);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:41:42 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:41:42 by cchauvet #+# #+# */
/* Updated: 2023/03/31 20:07:36 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 12:57:25 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,6 @@
# include "../env/env.h" # include "../env/env.h"
# include "../utils/utils.h" # include "../utils/utils.h"
char *get_pwd(int fd); char *get_pwd(void);
#endif #endif

View File

@ -6,31 +6,37 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */ /* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
/* Updated: 2023/03/31 15:02:43 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 13:22:22 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "./builtins_private.h" #include "./builtins_private.h"
int make_move(char *path, int fd) int make_move(char *path, t_list **env)
{ {
char *join; char *join;
char *old;
join = ft_strjoin("/", path); join = ft_strjoin("/", path);
join = ft_strfjoin(get_pwd(fd), join); join = ft_strfjoin(get_pwd(), join);
old = get_pwd();
if (chdir(join) == 0) if (chdir(join) == 0)
{ {
set_value_by_key("OLDPWD", old, env);
set_value_by_key("PWD", get_pwd(), env);
free(join); free(join);
return (0); return (0);
} }
free(old);
free(join); free(join);
write(2, "cd: No such file or directory\n", 30); write(2, "cd: No such file or directory\n", 30);
return (1); return (1);
} }
int move_folder(char **args, int fd) int move_folder(char **args, t_list **env)
{ {
char *path; char *path;
char *old;
if (args[0] == NULL || args[1] != NULL) if (args[0] == NULL || args[1] != NULL)
{ {
@ -40,11 +46,17 @@ int move_folder(char **args, int fd)
path = args[0]; path = args[0];
if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0) if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
{ {
old = get_pwd();
if (chdir(path) == 0) if (chdir(path) == 0)
{
set_value_by_key("OLDPWD", old, env);
set_value_by_key("PWD", get_pwd(), env);
return (0); return (0);
}
free(old);
write(2, "cd: No such file or directory\n", 30); write(2, "cd: No such file or directory\n", 30);
return (1); return (1);
} }
else else
return (make_move(path, fd)); return (make_move(path, env));
} }

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ /* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */
/* Updated: 2023/03/09 20:00:19 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 12:59:25 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,13 +20,13 @@ int pwd(int fd)
ft_putendl_fd(path, fd); ft_putendl_fd(path, fd);
else else
{ {
ft_putendl_fd("Error getcwd", fd); ft_putendl_fd("Error getcwd", 2);
return (1); return (1);
} }
return (0); return (0);
} }
char *get_pwd(int fd) char *get_pwd(void)
{ {
char *str; char *str;
@ -35,7 +35,7 @@ char *get_pwd(int fd)
return (str); return (str);
else else
{ {
ft_putendl_fd("Error getcwd", fd); ft_putendl_fd("Error getcwd", 2);
return (NULL); return (NULL);
} }
} }

11
env/env1.c vendored
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/03/30 13:07:15 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 12:47:08 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,7 +34,11 @@ void add_sort(t_list **head, t_env *var)
} }
} }
if (current->next == NULL) if (current->next == NULL)
{
current->next = ft_calloc(1, sizeof(t_list)); current->next = ft_calloc(1, sizeof(t_list));
if (!current->next)
return ;
}
} }
char *get_value_by_key(char *key, t_list **head) char *get_value_by_key(char *key, t_list **head)
@ -61,9 +65,7 @@ int set_value_by_key(char *key, char *value, t_list **head)
if (ft_strcmp(((t_env *)current->content)->key, key) == 0) if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
{ {
free(((t_env *)current->content)->value); free(((t_env *)current->content)->value);
free(((t_env *)current->content)->key);
((t_env *)current->content)->value = value; ((t_env *)current->content)->value = value;
((t_env *)current->content)->key = key;
return (0); return (0);
} }
current = current->next; current = current->next;
@ -97,7 +99,10 @@ t_list **init_env(char **env)
return (NULL); return (NULL);
*head = ft_calloc(1, sizeof(t_list)); *head = ft_calloc(1, sizeof(t_list));
if (*head == NULL) if (*head == NULL)
{
free(head);
return (NULL); return (NULL);
}
i = -1; i = -1;
while (env[++i]) while (env[++i])
{ {

2
env/env2.c vendored
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */ /* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */
/* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 13:26:31 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */ /* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/04/14 17:17:16 by cchauvet ### ########.fr */ /* Updated: 2023/04/18 13:00:08 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
else if (ft_strcmp(cmd->executable, "export") == 0) else if (ft_strcmp(cmd->executable, "export") == 0)
return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]); return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]);
else if (ft_strcmp(cmd->executable, "cd") == 0) else if (ft_strcmp(cmd->executable, "cd") == 0)
return_code = (move_folder(cmd->args + 1, cmd->fd_out[0])); return_code = (move_folder(cmd->args + 1, data->env, cmd->fd_out[0]));
else if (ft_strcmp(cmd->executable, "unset") == 0) else if (ft_strcmp(cmd->executable, "unset") == 0)
return_code = (unset(data->env, cmd->args, cmd->fd_out[0])); return_code = (unset(data->env, cmd->args, cmd->fd_out[0]));
else if (ft_strcmp(cmd->executable, "echo") == 0) else if (ft_strcmp(cmd->executable, "echo") == 0)

4
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/04/14 16:10:20 by erey-bet ### ########.fr */ /* Updated: 2023/04/18 12:59:43 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,7 @@ static char *ft_get_user_input(void)
char *prompt; char *prompt;
char *pwd; char *pwd;
pwd = get_pwd(2); pwd = get_pwd();
if (pwd == NULL) if (pwd == NULL)
return (NULL); return (NULL);
prompt = ft_strmerger(2, pwd, "$ "); prompt = ft_strmerger(2, pwd, "$ ");