This commit is contained in:
Loic Deridder
2025-01-14 14:02:16 +01:00
parent 2bd971c726
commit e47ba2f8f8
11 changed files with 129 additions and 3 deletions

27
srcs/builtins/cd.c Normal file
View File

@@ -0,0 +1,27 @@
#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);
// }
// }

8
srcs/builtins/echo.c Normal file
View File

@@ -0,0 +1,8 @@
#include "../../includes/builtins.h"
// void echo(char *msg, int flag)
// {
// printf("%s", msg);
// if (flag == 0)
// printf("\n");
// }

1
srcs/builtins/env.c Normal file
View File

@@ -0,0 +1 @@
#include "../../includes/builtins.h"

50
srcs/builtins/exit.c Normal file
View File

@@ -0,0 +1,50 @@
#include "../../includes/builtins.h"
int ft_isnumeric(char *str)
{
int i;
i = 0;
while (str[i])
{
if (ft_isdigit(str[i]))
i++;
else
return (0);
}
return (1);
}
void bash_exit(int code)
{
ft_putendl_fd("exit", 2);
exit(code);
}
void bash_exit_error(char *arg)
{
ft_putendl_fd("exit", 2);
ft_put_s_fd("minishell: exit: ", 2);
write(2, arg, ft_strlen(arg));
ft_putendl_fd(": numeric argument required", 2);
exit(2);
}
void builtin_exit(char *arg, bool depth)
{
if (depth == true)
{
if (!ft_isnumeric(arg))
{
ft_put_s_fd("minishell: exit: ", 2);
write(2, arg, ft_strlen(arg));
ft_putendl_fd(": numeric argument required", 2);
exit(2);
}
exit(ft_atoi(arg));
}
if (ft_isnumeric(arg))
bash_exit(ft_atoi(arg));
else
bash_exit_error(arg);
}

1
srcs/builtins/export.c Normal file
View File

@@ -0,0 +1 @@
#include "../../includes/builtins.h"

20
srcs/builtins/pwd.c Normal file
View File

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

9
srcs/builtins/test.c Normal file
View File

@@ -0,0 +1,9 @@
#include "../../includes/builtins.h"
#include <stdlib.h>
#include <stdio.h>
//
// int main(int ac, char **av, char **envp)
// {
// if (envp != NULL)
// printf("%s\n", envp[0]);
// }

1
srcs/builtins/unset.c Normal file
View File

@@ -0,0 +1 @@
#include "../../includes/builtins.h"