pwd/exit
This commit is contained in:
27
srcs/builtins/cd.c
Normal file
27
srcs/builtins/cd.c
Normal 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
8
srcs/builtins/echo.c
Normal 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
1
srcs/builtins/env.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "../../includes/builtins.h"
|
||||
50
srcs/builtins/exit.c
Normal file
50
srcs/builtins/exit.c
Normal 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
1
srcs/builtins/export.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "../../includes/builtins.h"
|
||||
20
srcs/builtins/pwd.c
Normal file
20
srcs/builtins/pwd.c
Normal 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
9
srcs/builtins/test.c
Normal 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
1
srcs/builtins/unset.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "../../includes/builtins.h"
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../minishell.h"
|
||||
#include "../includes/minishell.h"
|
||||
|
||||
char *powerline(void)
|
||||
{
|
||||
@@ -33,7 +33,9 @@ int main(int ac, char **av, char **envp)
|
||||
{
|
||||
input = powerline();
|
||||
if (ft_strncmp(input, "exit", 4) == 0)
|
||||
break ;
|
||||
builtin_exit(&input[5], true);
|
||||
if (ft_strncmp(input, "pwd", 3) == 0)
|
||||
builtin_pwd(input);
|
||||
free(input);
|
||||
}
|
||||
return (0);
|
||||
|
||||
Reference in New Issue
Block a user