fix: $PATH checked from variables *

also fix variables assignment
This commit is contained in:
mcolonna 2024-06-25 13:10:01 +02:00
parent 1091ddf32f
commit ef11eb0c8c
7 changed files with 24 additions and 24 deletions

View file

@ -1,5 +1,6 @@
- finding commands
- built-in commands
- find with given path only if there is a '/'
- variables
- uses $PATH in env.variables instead of envp
- builtins:

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 14:01:41 by mcolonna ### ########.fr */
/* Updated: 2024/06/21 16:22:20 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,4 +50,7 @@ typedef struct s_pipes
// If error, return a t_command wth the value .error = true.
t_command parse_command(t_env *env, const char *command);
// TODO reorganize
const char *variables_get(t_list *variables, const char *name);
#endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:00:10 by mcolonna ### ########.fr */
/* Updated: 2024/06/21 16:24:15 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,15 +17,11 @@
///// PATH /////
// Get the PATH values.
// Return a list of strings ended by NULL.
const char **get_path(const t_memclass mc, char *const envp[]);
// Search the program in $PATH.
// Returns the path of the program to use, or NULL if there is none.
// If there is several possibilities, it returns the one from the first path
// given in $PATH.
const char *search_path(
const t_memclass mc, const char **path, const char *prog);
const t_memclass mc, t_list *variables, const char *prog);
#endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/06/21 15:57:28 by mcolonna ### ########.fr */
/* Updated: 2024/06/24 16:16:31 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@ typedef struct s_exec_command_global
t_memclass mc;
int nb_calls;
t_pipes *pipes;
t_env *env;
} t_exec_command_global;
static void close_all_pipes(t_exec_command_global *global)
@ -50,11 +51,11 @@ static int execute_call(
t_exec_command_global *global, t_call call, const int inout[2],
char *const envp[])
{
const char **path = get_path(global->mc, envp);
pid_t pid;
const char *program_path;
program_path = search_path(global->mc, path, call.program);
program_path = search_path(global->mc, global->env->variables,
call.program);
if (!program_path)
return (minishell_error(str_join(fatal_error, global->mc,
"command not found: ", str_join(fatal_error, global->mc,
@ -121,6 +122,7 @@ int execute_command(t_env *env, t_command command)
r = 0;
global.mc = env->mc_command;
global.nb_calls = 0;
global.env = env;
while (command.calls[global.nb_calls].program != NULL)
global.nb_calls++;
if (global.nb_calls == 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/21 15:48:48 by mcolonna ### ########.fr */
/* Updated: 2024/06/24 17:30:19 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */
/* Updated: 2024/06/21 15:44:51 by mcolonna ### ########.fr */
/* Updated: 2024/06/25 13:06:57 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,7 +31,7 @@ static t_variable *variables_find(t_list *variables, const char *name)
el = el->next;
}
r = mem_alloc(fatal_error, variables->mc, sizeof(t_variable));
r->name = name;
r->name = str_dup(fatal_error, variables->mc, name);
r->value = "";
list_add(fatal_error, variables, r);
return (r);
@ -40,7 +40,8 @@ static t_variable *variables_find(t_list *variables, const char *name)
// Set a variable to a new value.
void variables_set(t_list *variables, const t_variable var)
{
variables_find(variables, var.name)->value = var.value;
variables_find(variables, var.name)->value =
str_dup(fatal_error, variables->mc, var.value);
}
// Get the value of a variable from its name.
@ -152,7 +153,6 @@ static void read_variable_value(t_parsing_args *args, const char **dest)
const char *tmp;
name = NULL;
tmp = NULL;
if (stream_read(&args->stream) == '?')
{
stream_pop(&args->stream);

View file

@ -6,29 +6,26 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/06/07 11:40:04 by jschaft ### ########.fr */
/* Updated: 2024/06/21 16:23:54 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
const char **get_path(const t_memclass mc, char *const envp[])
static const char **get_path(const t_memclass mc, t_list *variables)
{
int i;
const char **re;
char *path;
const char *path;
i = 0;
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
i++;
path = envp[i] + 5;
path = variables_get(variables, "PATH");
re = (const char **)str_split(fatal_error, mc, path, ":");
return (re);
}
const char *search_path(
const t_memclass mc, const char **path, const char *prog)
const t_memclass mc, t_list *variables, const char *prog)
{
const char **path = get_path(mc, variables);
const char *prog2 = str_join(fatal_error, mc, "/", prog);
int i;
const char *r;
@ -48,5 +45,6 @@ const char *search_path(
i++;
}
mem_free((void *)prog2);
mem_free(path);
return (NULL);
}