+ readline() replace gnl() in here_doc
+ "int error" in struct "t_cmd"
This commit is contained in:
LuckyLaszlo
2021-11-16 22:30:20 +01:00
17 changed files with 378 additions and 208 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);
}

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

@@ -0,0 +1,50 @@
#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], '='))
{
ft_free_2d_arr(var);
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);
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
*/

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);
}

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

@@ -0,0 +1,23 @@
#include "minishell.h"
int builtin_unset(int argc, char *argv[], t_all *c)
{
int position;
(void)argc;
(void)c;
position = getenv_position(c->envp, argv[1]);
if (position == -1)
return (0);
while (c->envp[position])
{
write(1, "1", 1);
free(c->envp[position]);
c->envp[position] = ft_strdup(c->envp[position + 1]);
position++;
}
write(1, "2", 1);
free(c->envp[position]);
return (0);
}