ajout des builtins pwd cd et export

This commit is contained in:
hugogogo
2021-11-11 17:44:42 +01:00
parent d65a701186
commit 47ae67ed14
12 changed files with 127 additions and 147 deletions

10
srcs/builtins/cd.c Normal file
View File

@@ -0,0 +1,10 @@
#include "minishell.h"
int builtin_cd(int argc, char *argv[], t_all *c)
{
(void)argc;
(void)c;
chdir(argv[1]);
return (0);
}

47
srcs/builtins/export.c Normal file
View File

@@ -0,0 +1,47 @@
#include "minishell.h"
int getenv_position(char **envp, char *name)
{
int i;
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;
(void)argc;
var = ft_split(argv[1], '=');
position = getenv_position(c->envp, var[0]);
if (position == -1 || !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_strjoin(c->envp[position], var[1]);
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 :
**
**
*/

15
srcs/builtins/pwd.c Normal file
View File

@@ -0,0 +1,15 @@
#include "minishell.h"
int builtin_pwd(int argc, char *argv[], t_all *c)
{
char *pwd;
(void)argc;
(void)argv;
(void)c;
pwd = getcwd(NULL, 0);
write(1, pwd, ft_strlen(pwd));
write(1, "\n", 1);
return (0);
}

10
srcs/builtins/unset.c Normal file
View File

@@ -0,0 +1,10 @@
#include "minishell.h"
int builtin_unset(int argc, char *argv[], t_all *c)
{
(void)argc;
(void)argv;
(void)c;
return (0);
}