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

@@ -1,3 +1,11 @@
## ENVP vs ENVIRON
- main.c
- free.c
- init.c
- minishell_structs.h
- export.c
## TESTS SIGNAUX WITH HERE-DOCS :
**bash**
@@ -172,6 +180,8 @@ test
- [send signal to process and childs](https://linuxconfig.org/how-to-propagate-a-signal-to-child-processes-from-a-bash-script)
- commande utile pour tester les process parents et enfants : while true ; do ps -o pid,pgid,cmd -C bash ; sleep 0.1 ; echo "\n" ; done
- [exit code status](https://tldp.org/LDP/abs/html/exitcodes.html)
- [`char **s` vs `char *s[]`](https://stackoverflow.com/questions/46830654/what-is-the-difference-between-extern-char-environ-and-extern-char-environ)
- [extern and global](https://stackoverflow.com/questions/2652545/extern-and-global-in-c)
```text

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
/* Updated: 2021/11/16 21:38:28 by lperrey ### ########.fr */
/* Updated: 2021/11/25 16:33:56 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,7 +52,7 @@ typedef struct s_cmd
typedef struct s_all
{
t_cmd **cmd_arr;
char **envp;
// char **envp;
char **path;
char *prompt_base;
char *prompt;

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

163
test_environ.c Normal file
View File

@@ -0,0 +1,163 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
# define COL_GREEN "\001\e[0;32m\002"
# define COL_BROWN "\001\e[0;33m\002"
# define COL_PURPL "\001\e[0;35m\002"
# define _END "\001\e[0m\002"
size_t ft_strlen(const char *s);
char *ft_strdup(const char *s);
size_t ft_2d_arrlen(void *ptr);
void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *));
void print_matrix(char **matrix, char *sep);
void *ft_resize_2d_arr(void *ptr, size_t add_nbr);
typedef void *(*t_dup_f)(void *);
int main(int argc, char *argv[], char *envp[])
{
extern char **environ;
int env_len;
char *print;
char *env_var;
(void)argc;
(void)argv;
print = COL_GREEN"\n[original <environ>] :\n"_END;
write(1, print, strlen(print));
env_len = ft_2d_arrlen(environ);
printf("\nenviron_len: %i\nenviron :\n", env_len);
print_matrix(environ, "\n");
print = COL_GREEN"\n[getenv] :\n"_END;
write(1, print, strlen(print));
env_var = getenv("TESTT");
if (env_var)
write(1, COL_PURPL"FOUND\n"_END, 21);
else
write(1, COL_PURPL"NOT FOUND\n"_END, 25);
print = COL_BROWN"\n[resize <environ>]...\n"_END;
write(1, print, strlen(print));
environ = ft_resize_2d_arr(environ, 1);
print = COL_BROWN"\n[add to <environ>]...\n"_END;
write(1, print, strlen(print));
environ[env_len] = "TESTT=testt";
print = COL_GREEN"\n[modified <environ>] :\n"_END;
write(1, print, strlen(print));
env_len = ft_2d_arrlen(environ);
printf("\nenviron_len: %i\nenviron :\n", env_len);
print_matrix(environ, "\n");
print = COL_GREEN"\n[getenv] :\n"_END;
write(1, print, strlen(print));
env_var = getenv("TESTT");
if (env_var)
write(1, COL_PURPL"FOUND\n"_END, 21);
else
write(1, COL_PURPL"NOT FOUND\n"_END, 25);
printf("\n%s\n", env_var);
return (0);
}
void *ft_resize_2d_arr(void *ptr, size_t add_nbr)
{
unsigned int i;
char **arr;
char **new_arr;
new_arr = malloc((ft_2d_arrlen(ptr) + add_nbr + 1) * sizeof (void *));
arr = (char **)ptr;
i = 0;
while (arr[i])
{
new_arr[i] = arr[i];
i++;
}
// free(arr);
return (new_arr);
}
void print_matrix(char **matrix, char *sep)
{
int i;
i = 0;
while (matrix[i])
{
printf("%s", matrix[i]);
if (matrix[i + 1])
printf("%s", sep);
fflush(stdout);
i++;
}
write(1, "\n", 1);
}
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (s[len] != '\0')
len++;
return (len);
}
char *ft_strdup(const char *s)
{
unsigned int i;
char *dup;
dup = malloc(ft_strlen(s) + 1);
if (!dup)
return (NULL);
i = 0;
while (s[i])
{
dup[i] = s[i];
i++;
}
dup[i] = '\0';
return (dup);
}
size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen()
{
size_t len;
char **arr;
arr = (char **)ptr;
len = 0;
while (arr[len] != NULL)
len++;
return (len);
}
void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *))
{
unsigned int i;
char **arr;
char **new_arr;
new_arr = malloc((ft_2d_arrlen(ptr) + 1) * sizeof (void *));
if (!new_arr)
return (NULL);
arr = (char **)ptr;
i = 0;
while (arr[i])
{
new_arr[i] = dup_func(arr[i]);
i++;
}
new_arr[i] = NULL;
return (new_arr);
}