builtins
This commit is contained in:
@@ -21,4 +21,8 @@ char **builtin_export(char **arg, char **envp);
|
||||
|
||||
//UTILS
|
||||
int count_char(char *str);
|
||||
int count_args(char **tab);
|
||||
int extractenv(char *str, char **envp);
|
||||
char *ft_getenv(char *str, char **envp);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
# include <stdio.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
10
srcs/main.c
10
srcs/main.c
@@ -4,6 +4,7 @@ char *powerline(void)
|
||||
{
|
||||
char *pwd;
|
||||
char *tilt;
|
||||
char *input;
|
||||
|
||||
pwd = getcwd(NULL, 0);
|
||||
if (ft_strncmp(pwd, "/home/", 6) == 0)
|
||||
@@ -19,7 +20,10 @@ char *powerline(void)
|
||||
----------------------------------%s", POW5, RESET);
|
||||
printf("\n%s MMOAT %s%s%s%s%s %s%s%s ",
|
||||
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)
|
||||
@@ -58,9 +62,9 @@ int main(int ac, char **av, char **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);
|
||||
envp = builtin_cd(ft_split(input, " "), envp);
|
||||
if (ft_strncmp(input, "export", 6) == 0)
|
||||
envp = builtin_export(ft_split(input, ' '), envp);
|
||||
envp = builtin_export(ft_split(input, " "), envp);
|
||||
free(input);
|
||||
}
|
||||
return (0);
|
||||
|
||||
Reference in New Issue
Block a user