variable extern environ fonction avec getenv et export ok

This commit is contained in:
hugogogo
2021-11-25 19:45:41 +01:00
parent 80410e6d81
commit ef3e91be13
10 changed files with 225 additions and 52 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 05:01:26 by lperrey #+# #+# */
/* Updated: 2021/10/10 07:37:29 by lperrey ### ########.fr */
/* Updated: 2021/11/25 19:01:13 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,11 @@
int builtin_env(int argc, char *argv[], t_all *c) // WIP
{
extern char **environ;
(void)argc;
(void)argv;
ft_putendl_arr_fd(c->envp, 1);
//ft_putendl_arr_fd(c->envp, 1);
ft_putendl_arr_fd(environ, 1);
return (0);
}

View File

@@ -8,43 +8,25 @@ int getenv_position(char **envp, char *name)
i = 0;
while (envp[i] && ft_strncmp(envp[i], name, ft_strlen(name)))
i++;
if (!envp[i])
return (-1);
return (i);
}
int builtin_export(int argc, char *argv[], t_all *c)
{
char **var;
int position;
extern char **environ;
char **var;
int env_position;
(void)argc;
var = ft_split(argv[1], '=');
position = getenv_position(c->envp, var[0]);
if (position == -1 || !ft_strchr(argv[1], '='))
{
ft_free_2d_arr(var);
(void)c;
if (!ft_strchr(argv[1], '='))
return (0);
}
c->envp[position] = ft_strjoin(var[0], "=");
if (var[1]) // parce que ft_strjoin return NULL si var[1] est null, pourquoi ?
c->envp[position] = ft_strjoinfree_s1(c->envp[position], var[1]);
ft_free_2d_arr(var);
var = ft_split(argv[1], '=');
env_position = getenv_position(environ, var[0]);
if (environ[env_position] == '\0')
environ = ft_resize_2d_arr(environ, 1);
environ[env_position] = ft_strdup(argv[1]);
// free var
return (0);
}
/*
** comportement de bash :
** 1. modifier une variable dans bash :
** > ca ne la modifie pas dans zsh
** > ca ne la garde pas apres exit de bash
** 2. ajouter une variable dans bash :
** > ca ne la modifie pas dans zsh
** > ca ne la garde pas apres exit de bash
** 3. ajouter une variable avec erreur :
** > 'export VARIABLE' n'exporte rien
** > 'export VARIABLE=' exporte une variable vide
** 4. ordre d'insertion d'une nouvelle variable :
** > aucune idee
*/

View File

@@ -4,20 +4,26 @@
int builtin_unset(int argc, char *argv[], t_all *c)
{
int position;
extern char **environ;
(void)argc;
(void)c;
position = getenv_position(c->envp, argv[1]);
//position = getenv_position(c->envp, argv[1]);
position = getenv_position(environ, argv[1]);
if (position == -1)
return (0);
while (c->envp[position])
// while (c->envp[position])
while (environ[position])
{
write(1, "1", 1);
free(c->envp[position]);
c->envp[position] = ft_strdup(c->envp[position + 1]);
//free(c->envp[position]);
free(environ[position]);
//c->envp[position] = ft_strdup(c->envp[position + 1]);
environ[position] = ft_strdup(environ[position + 1]);
position++;
}
write(1, "2", 1);
free(c->envp[position]);
//free(c->envp[position]);
free(environ[position]);
return (0);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/18 23:05:26 by hulamy ### ########.fr */
/* Updated: 2021/11/25 18:59:02 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,8 @@
int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
{
extern char **environ;
cmd->pid = fork();
if (cmd->pid == -1)
perror("fork()");
@@ -30,7 +32,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
close_pipeline_fd(c->cmd_arr);
if (cmd->builtin_func)
free_exit(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
else if (execve(cmd->path, cmd->argv, c->envp) == -1)
//else if (execve(cmd->path, cmd->argv, c->envp) == -1)
else if (execve(cmd->path, cmd->argv, environ) == -1)
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
}
return (EXIT_SUCCESS);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
/* Updated: 2021/11/18 03:09:46 by lperrey ### ########.fr */
/* Updated: 2021/11/25 16:36:39 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,7 @@ int free_exit(t_all *c, int exit_status)
free(c->prompt_base);
free(c->prompt);
ft_lstclear((t_list **)&c->token_list, free);
ft_free_2d_arr(c->envp);
// ft_free_2d_arr(c->envp);
ft_free_2d_arr(c->path);
free_pipeline(&c->cmd_arr);
//if (c->termios_changed)

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/11/18 21:56:06 by hulamy ### ########.fr */
/* Updated: 2021/11/25 18:57:59 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,15 +15,18 @@
static char *init_prompt_base(void);
static char *init_prompt(char *prompt_base);
int init(t_all *c, char *envp[])
//int init(t_all *c, char *envp[])
int init(t_all *c, char **environ)
{
(void)environ;
g_all = c;
ft_bzero(c, sizeof (*c));
c->envp = ft_dup_2d_arr(envp, (t_dup_f)ft_strdup); // TEST WIP
if (!c->envp)
return (ft_reti_perror(0, "ft_dup_2d_char_arr(envp) error"));
// c->envp = ft_dup_2d_arr(envp, (t_dup_f)ft_strdup); // TEST WIP
// if (!c->envp)
// return (ft_reti_perror(0, "ft_dup_2d_char_arr(envp) error"));
//print_matrix(c->envp, "\n --- \n"); // TEST WIP
c->path = retrieve_path(c->envp); // No return check. Its intended. PATH is optional
//c->path = retrieve_path(c->envp); // No return check. Its intended. PATH is optional
c->path = retrieve_path(environ); // No return check. Its intended. PATH is optional
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() error"));

View File

@@ -6,21 +6,24 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/11/08 04:05:41 by lperrey ### ########.fr */
/* Updated: 2021/11/25 16:30:24 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int main(int argc, char *argv[], char *envp[])
//int main(int argc, char *argv[], char *envp[])
int main(int argc, char *argv[])
{
t_all c;
extern char **environ;
(void)argc;
(void)argv;
if (!init(&c, envp))
//if (!init(&c, envp))
if (!init(&c, environ))
free_exit(&c, EXIT_FAILURE);
putenv("VAR=W1 W2 W3"); // TEMP TEST
//putenv("VAR=W1 W2 W3"); // TEMP TEST
//if (isatty(STDIN_FILENO))
shell_loop(&c);
//else