[FIX] fixed the cd builtin

This commit is contained in:
joris schaft 2024-06-28 11:24:29 +02:00
parent a0f6ecb7a6
commit d72d88bded
5 changed files with 64 additions and 29 deletions

View file

@ -5,7 +5,7 @@ SRCS = src/
# include directory
INCLUDES = include/ libtf/ libft/
# .c files in src/ without the extension
CODE = main ask_command error path parse_command exec_command builtin echo_builtin\
CODE = main ask_command error path parse_command exec_command builtin builtin2\
signals cool_readline
# directories to 'make'
LIBRARIES = libtf libft

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/25 13:31:08 by jschaft ### ########.fr */
/* Updated: 2024/06/27 14:36:14 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,5 +24,6 @@ int exec_builtin(t_call call, char *const envp[]);
// Execute echo builtin
int echo_builtin(const char **argv);
int cd_builtin(char *const *path, char *const envp[]);
#endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/06/25 13:58:48 by jschaft ### ########.fr */
/* Updated: 2024/06/27 13:52:00 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,21 +26,6 @@ bool is_builtin(const char *str)
|| ft_strncmp(str, "unset", 6) == 0);
}
// Execute the builtin cd for the specified path.
// Return 0 if success, errno if error.
static int cd_builtin(const char *path)
{
int r;
r = chdir(path);
if (r)
{
perror("cd");
return (errno);
}
return (0);
}
//Execute the builtin pwd for the current directory.
// Return 0 for the sake of returning something for exec_builtin
static int pwd_builtin(void)
@ -79,7 +64,7 @@ void exit_builtin(void)
int exec_builtin(t_call call, char *const envp[])
{
if (ft_strncmp(call.program, "cd", 3) == 0)
return (cd_builtin(call.argv[1]));
return (cd_builtin(call.argv, envp));
if (ft_strncmp(call.program, "exit", 5) == 0)
exit_builtin();
/* if (ft_strncmp(call.program, "export", 7) == 0)

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo_builtin.c :+: :+: :+: */
/* builtin2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/06/25 13:55:12 by jschaft ### ########.fr */
/* Updated: 2024/06/27 14:35:22 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,13 +19,13 @@ int echo_builtin(const char **argv)
i = 1;
opt = 0;
if (argv[1] != NULL)
if (argv[1] != NULL)
{
if (!ft_strncmp(argv[1], "-n", 3))
{
{
opt = 1;
i++;
}
i++;
}
}
while (argv[i] != NULL)
{
@ -34,7 +34,52 @@ int echo_builtin(const char **argv)
printf(" ");
i++;
}
if (opt == 0)
printf("\n");
if (opt == 0)
printf("\n");
return (0);
}
static int go_home(char *const envp[])
{
char **path;
int r;
int i;
i = 0;
while (ft_strncmp(envp[i], "HOME=", 5) != 0)
i++;
path = ft_split(envp[i], '=');
r = chdir(path[1]);
free(path[0]);
free(path[1]);
free(path);
return (r);
}
// Execute the builtin cd for the specified path.
// Return 0 if success, errno if error.
int cd_builtin(char *const *path, char *const envp[])
{
int r;
int i;
i = 0;
while (path[i] != NULL)
i++;
i--;
if (i == 1)
r = chdir(path[1]);
if (i == 0)
r = go_home(envp);
if (i != 0 && i != 1)
{
printf("cd: Too many arguments\n");
return (0);
}
if (r)
{
perror("cd");
return (errno);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
/* Updated: 2024/06/25 14:08:55 by jschaft ### ########.fr */
/* Updated: 2024/06/27 14:09:18 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,8 +78,12 @@ int main(const int argc, const char *argv[], char *const envp[])
t_env env;
t_list variables;
(void)argc;
(void)argv;
if (argc != 1)
{
printf("Error: No argument needed\n");
return (0);
}
env.exit = false;
env.mc_global = mem_newclass(fatal_error);
set_global_mc(env.mc_global);