builtins
This commit is contained in:
2
Makefile
2
Makefile
@@ -26,7 +26,7 @@ $(LIBFT):
|
|||||||
|
|
||||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c
|
$(OBJDIR)/%.o: $(SRCDIR)/%.c
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
@$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -g3 -ggdb -o $@ $(LINK)
|
@$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -o $@ $(LINK)
|
||||||
|
|
||||||
$(NAME): $(LIBFT) $(OBJS)
|
$(NAME): $(LIBFT) $(OBJS)
|
||||||
@$(CC) $(WFLAGS) $(OBJS) $(LIBFT) -o $(NAME) $(LINK)
|
@$(CC) $(WFLAGS) $(OBJS) $(LIBFT) -o $(NAME) $(LINK)
|
||||||
|
|||||||
@@ -8,11 +8,15 @@
|
|||||||
# include <stdbool.h>
|
# include <stdbool.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <errno.h>
|
# include <errno.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
|
||||||
// void echo(char *msg, int flag);
|
// void echo(char *msg, int flag);
|
||||||
void builtin_echo(char *arg);
|
void builtin_echo(char *arg, char **envp);
|
||||||
void builtin_exit(char *arg, bool depth);
|
void builtin_exit(char *arg, bool depth);
|
||||||
void builtin_pwd(char *arg);
|
void builtin_pwd(char *arg);
|
||||||
|
void builtin_env(char *str, char **envp);
|
||||||
|
void builtin_unset(char *str, char **envp);
|
||||||
|
|
||||||
|
//UTILS
|
||||||
|
int count_char(char *str);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ int ft_atoi(const char *str)
|
|||||||
int current_digit;
|
int current_digit;
|
||||||
|
|
||||||
if (!str)
|
if (!str)
|
||||||
return (-1);
|
return (0);
|
||||||
res = 0;
|
res = 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (ft_isspace(str[i]))
|
while (ft_isspace(str[i]))
|
||||||
|
|||||||
@@ -15,23 +15,47 @@
|
|||||||
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
|
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!haystack || !needle)
|
if (!haystack || !needle || *needle == 0)
|
||||||
return (NULL);
|
|
||||||
if (needle[0] == 0)
|
|
||||||
return ((char *)haystack);
|
return ((char *)haystack);
|
||||||
while (haystack[i] && i < len)
|
while (haystack[i] && i < len)
|
||||||
{
|
{
|
||||||
j = 0;
|
if (haystack[i] == needle[0])
|
||||||
while (haystack[i + j] == needle[j] && i + j < len)
|
|
||||||
{
|
{
|
||||||
j++;
|
j = 0;
|
||||||
if (needle[j] == 0)
|
while (haystack[i + j] == needle[j] && i + j < len)
|
||||||
return ((char *)(haystack + i));
|
{
|
||||||
|
j++;
|
||||||
|
if (needle[j] == 0)
|
||||||
|
return ((char *)(haystack + i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (0);
|
||||||
}
|
}
|
||||||
|
// char *ft_strnstr(const char *haystack, const char *needle, size_t len)
|
||||||
|
// {
|
||||||
|
// size_t i;
|
||||||
|
// size_t j;
|
||||||
|
//
|
||||||
|
// i = 0;
|
||||||
|
// if (!haystack || !needle)
|
||||||
|
// return (NULL);
|
||||||
|
// if (needle[0] == 0)
|
||||||
|
// return ((char *)haystack);
|
||||||
|
// while (haystack[i] && i < len)
|
||||||
|
// {
|
||||||
|
// j = 0;
|
||||||
|
// while (haystack[i + j] == needle[j] && i + j < len)
|
||||||
|
// {
|
||||||
|
// j++;
|
||||||
|
// if (needle[j] == 0)
|
||||||
|
// return ((char *)(haystack + i));
|
||||||
|
// }
|
||||||
|
// i++;
|
||||||
|
// }
|
||||||
|
// return (NULL);
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,27 +1,6 @@
|
|||||||
#include "../../includes/builtins.h"
|
#include "../../includes/builtins.h"
|
||||||
|
|
||||||
// static char *find_home(char **envp)
|
void builtin_cd(char *str)
|
||||||
// {
|
{
|
||||||
// int i;
|
(void)str;
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -24,14 +24,52 @@ int is_silent(char *str)
|
|||||||
return (0);
|
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;
|
int i;
|
||||||
|
|
||||||
i = 4;
|
i = 4;
|
||||||
while (arg[i] && is_silentchar(arg[i]))
|
while (arg[i] && is_silentchar(arg[i]))
|
||||||
i++;
|
i++;
|
||||||
printf("%s", &arg[i]);
|
echo_print(&arg[i], envp);
|
||||||
if (!is_silent(arg))
|
if (!is_silent(arg))
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,15 @@
|
|||||||
#include "../../includes/builtins.h"
|
#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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ int ft_isnumeric(char *str)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
if (!str)
|
||||||
|
return (1);
|
||||||
while (str[i])
|
while (str[i])
|
||||||
{
|
{
|
||||||
if (ft_isdigit(str[i]))
|
if (ft_isdigit(str[i]))
|
||||||
@@ -30,8 +32,23 @@ void bash_exit_error(char *arg)
|
|||||||
exit(2);
|
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 (depth == true)
|
||||||
{
|
{
|
||||||
if (!ft_isnumeric(arg))
|
if (!ft_isnumeric(arg))
|
||||||
|
|||||||
@@ -1,14 +1,30 @@
|
|||||||
#include "../../includes/builtins.h"
|
#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)
|
void builtin_pwd(char *arg)
|
||||||
{
|
{
|
||||||
char *cwd;
|
char *cwd;
|
||||||
|
|
||||||
if (ft_strlen(arg) > 3)
|
if (count_char(arg) > 3)
|
||||||
ft_putendl_fd("pwd: too many arguments", 2);
|
ft_putendl_fd("pwd: too many arguments", 2);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cwd = getcwd(NULL, 4096);
|
cwd = getcwd(NULL, 0);
|
||||||
if (cwd != NULL)
|
if (cwd != NULL)
|
||||||
{
|
{
|
||||||
printf("%s\n", cwd);
|
printf("%s\n", cwd);
|
||||||
|
|||||||
@@ -1 +1,22 @@
|
|||||||
#include "../../includes/builtins.h"
|
#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';
|
||||||
|
}
|
||||||
|
|||||||
27
srcs/main.c
27
srcs/main.c
@@ -5,7 +5,7 @@ char *powerline(void)
|
|||||||
char *pwd;
|
char *pwd;
|
||||||
char *tilt;
|
char *tilt;
|
||||||
|
|
||||||
pwd = getenv("PWD");
|
pwd = getcwd(NULL, 0);
|
||||||
if (ft_strncmp(pwd, "/home/", 6) == 0)
|
if (ft_strncmp(pwd, "/home/", 6) == 0)
|
||||||
{
|
{
|
||||||
pwd = pwd + 6;
|
pwd = pwd + 6;
|
||||||
@@ -22,20 +22,41 @@ char *powerline(void)
|
|||||||
return (readline(""));
|
return (readline(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char **ft_setnewenv(void)
|
||||||
|
{
|
||||||
|
char **envp;
|
||||||
|
|
||||||
|
envp = malloc(sizeof(char *) * 2);
|
||||||
|
if (!envp)
|
||||||
|
return (NULL);
|
||||||
|
envp[0] = ft_strjoin("PWD=", getcwd(NULL, 0));
|
||||||
|
envp[1] = ft_strdup("SHLVL=1");
|
||||||
|
if (!envp[0] || !envp[1])
|
||||||
|
return (ft_free(&envp[0]), ft_free(&envp[1]), NULL);
|
||||||
|
return (envp);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av, char **envp)
|
int main(int ac, char **av, char **envp)
|
||||||
{
|
{
|
||||||
char *input;
|
char *input;
|
||||||
|
|
||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(void)av;
|
||||||
(void)envp;
|
if (!envp[0])
|
||||||
|
envp = ft_setnewenv();
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
input = powerline();
|
input = powerline();
|
||||||
if (ft_strncmp(input, "exit", 4) == 0)
|
if (ft_strncmp(input, "exit", 4) == 0)
|
||||||
builtin_exit(&input[5], true);
|
builtin_exit(input, true);
|
||||||
if (ft_strncmp(input, "pwd", 3) == 0)
|
if (ft_strncmp(input, "pwd", 3) == 0)
|
||||||
builtin_pwd(input);
|
builtin_pwd(input);
|
||||||
|
if (ft_strncmp(input, "echo", 4) == 0)
|
||||||
|
builtin_echo(input, envp);
|
||||||
|
if (ft_strncmp(input, "env", 3) == 0)
|
||||||
|
builtin_env(input, envp);
|
||||||
|
if (ft_strncmp(input, "unset", 5) == 0)
|
||||||
|
builtin_unset(input, envp);
|
||||||
free(input);
|
free(input);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
Reference in New Issue
Block a user