fix: several builtins *

With the fork, the builtins didn't have access to the parent process values.
This commit is contained in:
mcolonna 2024-07-01 14:33:38 +02:00
parent c637b5f382
commit 7d38a40717
4 changed files with 46 additions and 37 deletions

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/28 15:26:29 by mcolonna ### ########.fr */
/* Updated: 2024/07/01 14:10:47 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,22 +15,26 @@
# include "include.h"
// for the 'std' parameter in all these functions:
// - std[0] is stdin
// - std[1] is stdout
// Check if the command is a builtin.
bool is_builtin(const char *str);
// Execute the builtin command
int exec_builtin(t_env *env, t_call call);
int exec_builtin(t_env *env, t_call call, int std[2]);
// Execute echo builtin
int echo_builtin(t_env *env, t_call call);
int echo_builtin(t_env *env, t_call call, int std[2]);
// Execute cd builtin
int cd_builtin(t_env *env, t_call call);
int cd_builtin(t_env *env, t_call call, int std[2]);
// Execute export builtin
int export_builtin(t_env *env, t_call call);
int export_builtin(t_env *env, t_call call, int std[2]);
// Execute exit builtin
int exit_builtin(t_env *env, t_call call);
int exit_builtin(t_env *env, t_call call, int std[2]);
#endif

View file

@ -6,16 +6,17 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/07/01 12:35:09 by jschaft ### ########.fr */
/* Updated: 2024/07/01 14:20:51 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
static int unset_builtin(t_env *env, t_call call)
static int unset_builtin(t_env *env, t_call call, int std[2])
{
int i;
(void)std;
i = 0;
while (call.argv[++i])
variables_unset(env->variables, call.argv[i]);
@ -39,23 +40,24 @@ bool is_builtin(const char *str)
// Execute the builtin pwd for the current directory.
// Return 0 for the sake of returning something for exec_builtin
static int pwd_builtin(t_env *env, t_call call)
static int pwd_builtin(t_env *env, t_call call, int std[2])
{
char *str;
(void)env;
(void)std;
if (call.argv[1])
{
print_line(fatal_error, 2, "too many arguments");
return (1);
}
str = getcwd(NULL, 0);
printf("%s\n", str);
print_line(fatal_error, std[1], str);
free(str);
return (0);
}
static int env_builtin(t_env *env, t_call call)
static int env_builtin(t_env *env, t_call call, int std[2])
{
int i;
const t_memclass mc = mem_subclass(fatal_error, env->mc_global);
@ -69,7 +71,7 @@ static int env_builtin(t_env *env, t_call call)
i = 0;
while (envp[i] != NULL)
{
printf("%s\n", envp[i]);
print_line(fatal_error, std[1], envp[i]);
i++;
}
mem_freeall(mc);
@ -79,21 +81,21 @@ static int env_builtin(t_env *env, t_call call)
// Execute the specified builtin.
// The builtin must exist.
// Return the error value returned by the builtin.
int exec_builtin(t_env *env, t_call call)
int exec_builtin(t_env *env, t_call call, int std[2])
{
if (ft_strncmp(call.program, "cd", 3) == 0)
exit (cd_builtin(env, call));
return (cd_builtin(env, call, std));
if (ft_strncmp(call.program, "exit", 5) == 0)
exit (exit_builtin(env, call));
return (exit_builtin(env, call, std));
if (ft_strncmp(call.program, "export", 7) == 0)
exit (export_builtin(env, call));
return (export_builtin(env, call, std));
if (ft_strncmp(call.program, "unset", 6) == 0)
exit (unset_builtin(env, call));
return (unset_builtin(env, call, std));
if (ft_strncmp(call.program, "pwd", 4) == 0)
exit (pwd_builtin(env, call));
return (pwd_builtin(env, call, std));
if (ft_strncmp(call.program, "echo", 5) == 0)
exit (echo_builtin(env, call));
return (echo_builtin(env, call, std));
if (ft_strncmp(call.program, "env", 4) == 0)
exit (env_builtin(env, call));
exit (1);
return (env_builtin(env, call, std));
return (1);
}

View file

@ -6,13 +6,13 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/06/28 15:30:55 by mcolonna ### ########.fr */
/* Updated: 2024/07/01 14:18:19 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
int export_builtin(t_env *env, t_call call)
int export_builtin(t_env *env, t_call call, int std[2])
{
int i;
char *name;
@ -20,6 +20,7 @@ int export_builtin(t_env *env, t_call call)
char **splitted;
const t_memclass mc = mem_subclass(fatal_error, env->mc_global);
(void)std;
i = 0;
while (call.argv[++i])
{
@ -40,10 +41,11 @@ int export_builtin(t_env *env, t_call call)
return (0);
}
int exit_builtin(t_env *env, t_call call)
int exit_builtin(t_env *env, t_call call, int std[2])
{
int r;
(void)std;
if (call.argv[1] && call.argv[2])
{
print_line(fatal_error, 2, "exit: too many arguments");
@ -63,7 +65,7 @@ int exit_builtin(t_env *env, t_call call)
return (r);
}
int echo_builtin(t_env *env, t_call call)
int echo_builtin(t_env *env, t_call call, int std[2])
{
int i;
bool newline;
@ -78,13 +80,13 @@ int echo_builtin(t_env *env, t_call call)
}
while (call.argv[i] != NULL)
{
printf("%s", call.argv[i]);
print_str(fatal_error, std[1], call.argv[i]);
if (call.argv[i + 1] != NULL)
printf(" ");
print_str(fatal_error, std[1], " ");
i++;
}
if (newline)
printf("\n");
print_str(fatal_error, std[1], "\n");
return (0);
}
@ -95,11 +97,12 @@ static int go_home(t_env *env)
// Execute the builtin cd for the specified path.
// Return 0 if success, errno if error.
int cd_builtin(t_env *env, t_call call)
int cd_builtin(t_env *env, t_call call, int std[2])
{
int r;
int argc;
(void)std;
argc = 0;
while (call.argv[argc])
argc++;

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/07/01 12:36:03 by jschaft ### ########.fr */
/* Updated: 2024/07/01 14:32:09 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,7 +35,7 @@ static void close_all_pipes(t_exec_command_global *global)
// Execute a program with specific stdin and stdout:
// - inout[0] is stdin
// - inout[0] is stdout
// - inout[1] is stdout
// If call.program doesn't have any '/' in it, find the program in $PATH.
// If the program wasn't to found in $PATH, or the fork didn't work,
// write the error and return the error status.
@ -48,9 +48,11 @@ static int execute_call(
program_path = search_path(global->mc, global->env->variables,
call.program);
if (!program_path)
if (!program_path && !is_builtin(call.program))
return (minishell_error(str_join(fatal_error, global->mc,
"command not found: ", call.program)), 127);
if (is_builtin(call.program))
return (exec_builtin(global->env, call, (int *)inout));
pid = fork();
if (pid < 0)
return (minishell_error("errno"), errno);
@ -60,10 +62,7 @@ static int execute_call(
if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0)
return (minishell_error("errno"), errno);
close_all_pipes(global);
if (is_builtin(call.program))
exec_builtin(global->env, call);
else
execve(program_path, call.argv, envp);
execve(program_path, call.argv, envp);
return (minishell_error("errno"), errno);
}
return (0);
@ -115,6 +114,7 @@ int execute_command(t_env *env, t_command command)
int r;
int wstatus;
wstatus = 0;
r = 0;
global.mc = env->mc_command;
global.nb_calls = 0;
@ -132,7 +132,7 @@ int execute_command(t_env *env, t_command command)
;
if (errno != ECHILD)
return (minishell_error("errno"), errno);
if (!r)
if (WEXITSTATUS(wstatus))
r = WEXITSTATUS(wstatus);
return (r);
}