This commit is contained in:
Loic Deridder
2025-01-16 14:42:38 +01:00
parent 0bf69437bb
commit 583b3e034a
8 changed files with 199 additions and 18 deletions

View File

@@ -16,6 +16,8 @@ void builtin_exit(char *arg, bool depth);
void builtin_pwd(char *arg);
void builtin_env(char *str, char **envp);
void builtin_unset(char *str, char **envp);
char **builtin_cd(char **arg, char **envp);
char **builtin_export(char **arg, char **envp);
//UTILS
int count_char(char *str);

View File

@@ -35,7 +35,6 @@ size_t ft_strlcat(char *dst, const char *src, size_t dstsize);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_strcmp(const char *s1, const char *s2);
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
char *ft_substr(const char *s, unsigned int start, size_t len);
char *ft_strdup(const char *s1);

View File

@@ -1,11 +0,0 @@
#include "../../libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
while (*s1 && *s2 && (*s1 == *s2))
{
s1++;
s2++;
}
return ((unsigned char) *s1 - (unsigned char) *s2);
}

View File

@@ -1,6 +1,65 @@
#include "../../includes/builtins.h"
void builtin_cd(char *str)
char **update_oldpwd(char **envp)
{
(void)str;
char **new_envp;
int i;
i = 0;
while (envp[i])
i++;
new_envp = malloc(sizeof(char *) * i + 1);
if (!new_envp)
return (perror("malloc"), NULL);
new_envp[i] = NULL;
i = -1;
while (envp[++i])
{
if (ft_strnstr(envp[i], "OLDPWD=", 7) == NULL)
new_envp[i] = ft_strdup(envp[i]);
else
new_envp[i] = ft_strjoin("OLDPWD=", getcwd(NULL, 0));
}
return (new_envp);
}
char **update_pwd(char **envp)
{
char **new_envp;
int i;
i = 0;
while (envp[i])
i++;
new_envp = malloc(sizeof(char *) * i + 1);
if (!new_envp)
return (perror("malloc"), NULL);
new_envp[i] = NULL;
i = -1;
while (envp[++i])
{
if (ft_strnstr(envp[i], "PWD=", 4) == NULL)
new_envp[i] = ft_strdup(envp[i]);
else
new_envp[i] = ft_strjoin("PWD=", getcwd(NULL, 0));
}
return (new_envp);
}
char **builtin_cd(char **arg, char **envp)
{
/*
* OLDPWD = getcwd()
*/
envp = update_oldpwd(envp);
if (chdir(arg[1]) == -1)
{
perror("cd");
return (envp);
}
envp = update_pwd(envp);
/*
* PWD = getcwd()
*/
return (envp);
}

View File

@@ -8,7 +8,7 @@ void builtin_env(char *str, char **envp)
(void)str;
while (envp[i])
{
if (envp[i][0])
if (envp[i][0] && ft_strchr(envp[i], '=') != NULL)
printf("%s\n", envp[i]);
i++;
}

View File

@@ -23,7 +23,7 @@ void bash_exit(int code)
exit(code);
}
void bash_exit_error(char *arg)
void bash_exit_errornum(char *arg)
{
ft_putendl_fd("exit", 2);
ft_put_s_fd("minishell: exit: ", 2);
@@ -44,6 +44,39 @@ static char *get_arg(char *str)
return (&str[i]);
}
int is_sep(char c)
{
if ((c >= 9 && c <= 13) || c == ' ' || c == '\0')
return (1);
else
return (0);
}
int count_arg(char *str)
{
int count;
int i;
count = 0;
i = 0;
while (str[i] && str[i + 1])
{
if (!is_sep(str[i]) && is_sep(str[i + 1]))
count++;
i++;
}
return (count);
}
void bash_exiterrorcount(void)
{
ft_putendl_fd("exit", 2);
ft_putendl_fd("minishell: exit: too many arguments", 2);
/*
* SET EXIT CODE WITHOUT EXITING MINISHELL
*/
}
void builtin_exit(char *str, bool depth)
{
char *arg;
@@ -51,6 +84,11 @@ void builtin_exit(char *str, bool depth)
arg = get_arg(str);
if (depth == true)
{
if (count_arg(str) >= 2)
{
ft_putendl_fd("minishell: exit: too many arguments", 2);
exit (1);
}
if (!ft_isnumeric(arg))
{
ft_put_s_fd("minishell: exit: ", 2);
@@ -60,8 +98,10 @@ void builtin_exit(char *str, bool depth)
}
exit(ft_atoi(arg));
}
if (ft_isnumeric(arg))
if (count_arg(str) >= 2)
bash_exiterrorcount();
else if (ft_isnumeric(arg))
bash_exit(ft_atoi(arg));
else
bash_exit_error(arg);
bash_exit_errornum(arg);
}

View File

@@ -1 +1,89 @@
#include "../../includes/builtins.h"
void check_format(char **arg)
{
(void)arg;
}
void print_error(char *str, int *code)
{
ft_put_s_fd("minishell: export: ", 2);
ft_put_s_fd(str, 2);
ft_putendl_fd(": not a valid identifier", 2);
*code = 1;
}
void print_arr(char **envp)
{
int i;
char *equal;
(void)equal;
i = -1;
while (envp[++i])
{
ft_printf("declare -x %s\n", envp[i]);
}
}
void print_export(char **envp)
{
int i;
int j;
char *tmp;
int len;
i = 0;
len = 0;
while (envp[len])
len++;
while (i < len - 1)
{
j = 0;
while (j < len - i - 1)
{
if (ft_strncmp(envp[j], envp[j + 1], ft_strlen(envp[j])) > 0)
{
tmp = envp[j];
envp[j] = envp[j + 1];
envp[j + 1] = tmp;
}
j++;
}
i++;
}
print_arr(envp);
}
char **builtin_export(char **arg, char **envp)
{
char **new_envp;
int i;
// int code;
i =0;
while (envp[i])
i++;
new_envp = malloc(sizeof(char *) * i + 1);
new_envp[i] = NULL;
i = -1;
while (envp[++i])
new_envp[i] = ft_strdup(envp[i]);
// code = 0;
i = 0;
while (arg[i])
i++;
if (i == 1)
return(print_export(new_envp), envp);
return (envp);
// i = -1;
// while (arg[++i])
// {
// if (check_format(arg[i]))
// {
//
// }
// else
// print_error(arg[i], &code);
// }
}

View File

@@ -57,6 +57,10 @@ int main(int ac, char **av, char **envp)
builtin_env(input, envp);
if (ft_strncmp(input, "unset", 5) == 0)
builtin_unset(input, envp);
if (ft_strncmp(input, "cd", 2) == 0)
envp = builtin_cd(ft_split(input, ' '), envp);
if (ft_strncmp(input, "export", 6) == 0)
envp = builtin_export(ft_split(input, ' '), envp);
free(input);
}
return (0);