This commit is contained in:
Loic Deridder
2025-01-15 13:41:40 +01:00
parent eedfe4a0ee
commit 0ec78ee34c
11 changed files with 182 additions and 48 deletions

View File

@@ -1,27 +1,6 @@
#include "../../includes/builtins.h"
// static char *find_home(char **envp)
// {
// int i;
// char *str;
//
// i = 0;
// if (!envp)
// return (NULL);
// while (ft_strnstr(envp[i], "HOME=", 5) == 0)
// i++;
// str = envp[i + 5];
// return (str);
// }
//
// int cd(char *path, char **envp)
// {
// char *str;
//
// if (path == NULL)
// {
// //cd $HOME
// str = find_home(envp);
// chdir(str);
// }
// }
void builtin_cd(char *str)
{
(void)str;
}

View File

@@ -24,14 +24,52 @@ int is_silent(char *str)
return (0);
}
void builtin_echo(char *arg)
static int extractenv(char *str, char **envp)
{
int i;
char *var;
char *tmp;
i = 0;
(void)envp;
while (str[i] && str[i] != ' ')
i++;
if (i >= 1)
tmp = ft_substr(str, 1, i - 1);
var = getenv(tmp);
free(tmp);
if (var)
ft_printf("%s", var);
return (i);
}
static void echo_print(char *str, char **envp)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '$')
{
if (!str[i + 1] || str[i + 1] == ' ')
ft_put_c(str[i++]);
else
i += extractenv(&str[i], envp);
}
else
ft_put_c(str[i++]);
}
}
void builtin_echo(char *arg, char **envp)
{
int i;
i = 4;
while (arg[i] && is_silentchar(arg[i]))
i++;
printf("%s", &arg[i]);
echo_print(&arg[i], envp);
if (!is_silent(arg))
printf("\n");
}

View File

@@ -1 +1,15 @@
#include "../../includes/builtins.h"
void builtin_env(char *str, char **envp)
{
int i;
i = 0;
(void)str;
while (envp[i])
{
if (envp[i][0])
printf("%s\n", envp[i]);
i++;
}
}

View File

@@ -5,6 +5,8 @@ int ft_isnumeric(char *str)
int i;
i = 0;
if (!str)
return (1);
while (str[i])
{
if (ft_isdigit(str[i]))
@@ -30,8 +32,23 @@ void bash_exit_error(char *arg)
exit(2);
}
void builtin_exit(char *arg, bool depth)
static char *get_arg(char *str)
{
int i;
i = 4;
if (!str[i])
return (NULL);
while (str[i] && str[i] == ' ')
i++;
return (&str[i]);
}
void builtin_exit(char *str, bool depth)
{
char *arg;
arg = get_arg(str);
if (depth == true)
{
if (!ft_isnumeric(arg))

View File

@@ -1,20 +1,36 @@
#include "../../includes/builtins.h"
int count_char(char *str)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] != ' ')
count++;
i++;
}
return (count);
}
void builtin_pwd(char *arg)
{
char *cwd;
if (ft_strlen(arg) > 3)
if (count_char(arg) > 3)
ft_putendl_fd("pwd: too many arguments", 2);
else
{
cwd = getcwd(NULL, 4096);
cwd = getcwd(NULL, 0);
if (cwd != NULL)
{
printf("%s\n", cwd);
free(cwd);
}
if (!cwd)
perror("pwd");
perror("pwd");
}
}

View File

@@ -1 +1,22 @@
#include "../../includes/builtins.h"
void builtin_unset(char *str, char **envp)
{
int i;
char *var;
if (count_char(str) == 5)
{
ft_putendl_fd("unset: not enough arguments", 2);
// exit(1);
}
i = 0;
while(str[5 + i] && str[5 + i] == ' ')
i++;
var = &str[5 + i];
i = 0;
while (ft_strnstr(envp[i], var, ft_strlen(var)) == NULL)
i++;
if (envp[i])
envp[i][0] = '\0';
}