This commit is contained in:
Loic Deridder
2025-01-17 14:40:19 +01:00
parent ba1a2933e4
commit 8c64e2c6f6
5 changed files with 65 additions and 10 deletions

View File

@@ -21,4 +21,8 @@ char **builtin_export(char **arg, char **envp);
//UTILS //UTILS
int count_char(char *str); int count_char(char *str);
int count_args(char **tab);
int extractenv(char *str, char **envp);
char *ft_getenv(char *str, char **envp);
#endif #endif

View File

@@ -3,6 +3,7 @@
# include <stdio.h> # include <stdio.h>
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h>
# include <unistd.h> # include <unistd.h>
# include <stdlib.h> # include <stdlib.h>

View File

@@ -1,5 +1,15 @@
#include "../../includes/builtins.h" #include "../../includes/builtins.h"
int count_args(char **tab)
{
int i;
i = 0;
while (tab[i])
i++;
return (i);
}
char **update_oldpwd(char **envp) char **update_oldpwd(char **envp)
{ {
char **new_envp; char **new_envp;
@@ -46,11 +56,50 @@ char **update_pwd(char **envp)
return (new_envp); return (new_envp);
} }
int check_path(char *str)
{
if (access(str, F_OK | X_OK) == -1)
{
ft_put_s_fd(str, 2);
ft_put_s_fd(": ", 2);
perror("cd");
return (0);
}
return (1);
}
// void expand_var(char *str, char **envp)
// {
// char *new_arg;
// char *tmp;
// int i;
// int j;
//
// i = -1;
// while (str[++i])
// {
// if (str[i] == '$')
// {
// j = 0;
// while (str[j] && str[j] != ' ')
// j++;
// if (j >= 1)
// }
// }
// }
char **builtin_cd(char **arg, char **envp) char **builtin_cd(char **arg, char **envp)
{ {
/* // char *path;
* OLDPWD = getcwd()
*/ if (count_args(arg) >= 3)
{
ft_putendl_fd("cd: too many arguments", 2);
return (envp);
}
// path = expand_var(arg[1], envp);
if (!check_path(arg[1]))
return (envp);
envp = update_oldpwd(envp); envp = update_oldpwd(envp);
if (chdir(arg[1]) == -1) if (chdir(arg[1]) == -1)
{ {
@@ -58,8 +107,5 @@ char **builtin_cd(char **arg, char **envp)
return (envp); return (envp);
} }
envp = update_pwd(envp); envp = update_pwd(envp);
/*
* PWD = getcwd()
*/
return (envp); return (envp);
} }

View File

@@ -41,7 +41,7 @@ char *ft_getenv(char *str, char **envp)
return (&envp[j][len]); return (&envp[j][len]);
} }
static int extractenv(char *str, char **envp) int extractenv(char *str, char **envp)
{ {
int i; int i;
char *var; char *var;

View File

@@ -4,6 +4,7 @@ char *powerline(void)
{ {
char *pwd; char *pwd;
char *tilt; char *tilt;
char *input;
pwd = getcwd(NULL, 0); pwd = getcwd(NULL, 0);
if (ft_strncmp(pwd, "/home/", 6) == 0) if (ft_strncmp(pwd, "/home/", 6) == 0)
@@ -19,7 +20,10 @@ char *powerline(void)
----------------------------------%s", POW5, RESET); ----------------------------------%s", POW5, RESET);
printf("\n%s  MMOAT %s%s%s%s%s %s%s%s ", printf("\n%s  MMOAT %s%s%s%s%s %s%s%s ",
POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET); POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET);
return (readline("")); input = readline("");
if (ft_strlen(input) > 0)
add_history(input);
return (input);
} }
char **ft_setnewenv(void) char **ft_setnewenv(void)
@@ -58,9 +62,9 @@ int main(int ac, char **av, char **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) if (ft_strncmp(input, "cd", 2) == 0)
envp = builtin_cd(ft_split(input, ' '), envp); envp = builtin_cd(ft_split(input, " "), envp);
if (ft_strncmp(input, "export", 6) == 0) if (ft_strncmp(input, "export", 6) == 0)
envp = builtin_export(ft_split(input, ' '), envp); envp = builtin_export(ft_split(input, " "), envp);
free(input); free(input);
} }
return (0); return (0);