[ADD] cd builtin

This commit is contained in:
joris schaft 2024-06-03 14:32:45 +02:00
parent b103e605c3
commit d3fdd3bbf6
5 changed files with 68 additions and 5 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
CODE = main ask_command error path parse_command exec_command builtin
# directories to 'make'
LIBRARIES = libtf libft
# .a files to include

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/05/31 13:45:38 by mcolonna ### ########.fr */
/* Updated: 2024/06/03 13:52:11 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -97,6 +97,14 @@ const char **get_path(const t_memclass mc, char *const envp[]);
const char *search_path(
const t_memclass mc, const char **path, const char *prog);
///// BUILTIN /////
// Check if the command is a builtin, return 1 if yes, -1 if no
int is_builtin(const char *str);
// Execute the builtin command
int exec_builtin(t_call call);
///// MAIN /////
extern t_memclass g_mc;

52
src/builtin.c Normal file
View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/06/03 14:18:50 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
int is_builtin(const char *str)
{
if (ft_strncmp(str, "cd", 3) == 0)
return (1);
if (ft_strncmp(str, "exit", 5) == 0)
return (1);
if (ft_strncmp(str, "export", 7) == 0)
return (1);
if (ft_strncmp(str, "unset", 6) == 0)
return (1);
return (-1);
}
static int cd_builtin(const char *path)
{
int r;
r = chdir(path);
if (r)
{
minishell_error("cd: unavailable path");
return (-1);
}
return (1);
}
int exec_builtin(t_call call)
{
if (ft_strncmp(call.program, "cd", 3) == 0)
return (cd_builtin((const char*)call.argv[1]));
/* if (ft_strncmp(call.program, "exit", 5) == 0)
exit_builtin();
if (ft_strncmp(call.program, "export", 7) == 0)
return (export_builtin(call.argv));
if (ft_strncmp(call.program, "unset", 6) == 0)
return (unset_builtin());*/
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/05/17 14:43:37 by mcolonna ### ########.fr */
/* Updated: 2024/06/03 14:01:02 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -102,7 +102,10 @@ static int exec_each_call(t_exec_command_global *global,
{
inout[0] = global->pipes[i].pipe[0];
inout[1] = global->pipes[i + 1].pipe[1];
r = execute_call(global, command.calls[i], inout, envp);
if (is_builtin(command.calls[i].program) == 1)
r = exec_builtin(command.calls[i]);
else
r = execute_call(global, command.calls[i], inout, envp);
if (r)
return (r);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/05/01 17:53:38 by mcolonna ### ########.fr */
/* Updated: 2024/06/03 11:39:59 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */