Merge branch 'main' of github.com:gazhonsepaskwa/mmoat
This commit is contained in:
@@ -16,6 +16,8 @@ void builtin_exit(char *arg, bool depth);
|
|||||||
void builtin_pwd(char *arg);
|
void builtin_pwd(char *arg);
|
||||||
void builtin_env(char *str, char **envp);
|
void builtin_env(char *str, char **envp);
|
||||||
void builtin_unset(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
|
//UTILS
|
||||||
int count_char(char *str);
|
int count_char(char *str);
|
||||||
|
|||||||
@@ -1,6 +1,65 @@
|
|||||||
#include "../../includes/builtins.h"
|
#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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ void builtin_env(char *str, char **envp)
|
|||||||
(void)str;
|
(void)str;
|
||||||
while (envp[i])
|
while (envp[i])
|
||||||
{
|
{
|
||||||
if (envp[i][0])
|
if (envp[i][0] && ft_strchr(envp[i], '=') != NULL)
|
||||||
printf("%s\n", envp[i]);
|
printf("%s\n", envp[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ void bash_exit(int code)
|
|||||||
exit(code);
|
exit(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bash_exit_error(char *arg)
|
void bash_exit_errornum(char *arg)
|
||||||
{
|
{
|
||||||
ft_putendl_fd("exit", 2);
|
ft_putendl_fd("exit", 2);
|
||||||
ft_put_s_fd("minishell: exit: ", 2);
|
ft_put_s_fd("minishell: exit: ", 2);
|
||||||
@@ -44,6 +44,39 @@ static char *get_arg(char *str)
|
|||||||
return (&str[i]);
|
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)
|
void builtin_exit(char *str, bool depth)
|
||||||
{
|
{
|
||||||
char *arg;
|
char *arg;
|
||||||
@@ -51,6 +84,11 @@ void builtin_exit(char *str, bool depth)
|
|||||||
arg = get_arg(str);
|
arg = get_arg(str);
|
||||||
if (depth == true)
|
if (depth == true)
|
||||||
{
|
{
|
||||||
|
if (count_arg(str) >= 2)
|
||||||
|
{
|
||||||
|
ft_putendl_fd("minishell: exit: too many arguments", 2);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
if (!ft_isnumeric(arg))
|
if (!ft_isnumeric(arg))
|
||||||
{
|
{
|
||||||
ft_put_s_fd("minishell: exit: ", 2);
|
ft_put_s_fd("minishell: exit: ", 2);
|
||||||
@@ -60,8 +98,10 @@ void builtin_exit(char *str, bool depth)
|
|||||||
}
|
}
|
||||||
exit(ft_atoi(arg));
|
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));
|
bash_exit(ft_atoi(arg));
|
||||||
else
|
else
|
||||||
bash_exit_error(arg);
|
bash_exit_errornum(arg);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,89 @@
|
|||||||
#include "../../includes/builtins.h"
|
#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);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ int main(int ac, char **av, char **envp)
|
|||||||
builtin_env(input, envp);
|
builtin_env(input, envp);
|
||||||
if (ft_strncmp(input, "unset", 5) == 0)
|
if (ft_strncmp(input, "unset", 5) == 0)
|
||||||
builtin_unset(input, envp);
|
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);
|
free(input);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
Reference in New Issue
Block a user