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

@@ -1,5 +1,15 @@
#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 **new_envp;
@@ -46,11 +56,50 @@ char **update_pwd(char **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)
{
/*
* OLDPWD = getcwd()
*/
// char *path;
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);
if (chdir(arg[1]) == -1)
{
@@ -58,8 +107,5 @@ char **builtin_cd(char **arg, char **envp)
return (envp);
}
envp = update_pwd(envp);
/*
* PWD = getcwd()
*/
return (envp);
}

View File

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