From 23f7a694e7d06d036c355897f541615581c94ca2 Mon Sep 17 00:00:00 2001 From: Lilia-42 Date: Thu, 22 Aug 2024 17:07:07 +0200 Subject: [PATCH] Add env & pwd --- Makefile | 2 ++ inc/execution.h | 2 ++ src/builtins/echo.c | 21 +-------------------- src/builtins/env.c | 34 ++++++++++++++++++++++++++++++++++ src/builtins/pwd.c | 23 +++++++++++++++++++++++ src/execution.c | 10 +++++++++- 6 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 src/builtins/env.c create mode 100644 src/builtins/pwd.c diff --git a/Makefile b/Makefile index 05e8ece..f56c1b4 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,8 @@ SOURCES := main.c \ \ builtins/cd.c \ builtins/echo.c \ + builtins/pwd.c \ + builtins/env.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/execution.h b/inc/execution.h index 84a5558..8a0f7ad 100644 --- a/inc/execution.h +++ b/inc/execution.h @@ -19,5 +19,7 @@ int set_env_var_value(t_execution_env *env, const char *var_name, const char *new_value); int cd(int argc, char **argv, t_execution_env *env); int echo(int argc, char **argv); +int pwd(int argc); +int ft_env(int argc, t_execution_env *env); #endif // EXECUTION_H diff --git a/src/builtins/echo.c b/src/builtins/echo.c index 2b45983..441f920 100644 --- a/src/builtins/echo.c +++ b/src/builtins/echo.c @@ -3,24 +3,6 @@ #include "execution.h" #include -// size_t ft_strlen(const char *str) //libft () -// { -// unsigned int i; -// i = 0; -// while (str[i] != '\0') -// { -// i++; -// } -// return (i); -// } - -// void ft_putstr_fd(char *s, int fd) //libft fuction -// { -// if (!s) -// return; -// write(fd, s, ft_strlen(s)); -// } - int ft_check_n(char *str) { if (str[0] == '-' && str[1] == 'n') @@ -52,6 +34,5 @@ int echo(int argc, char **argv) } if (flag == 0) ft_putstr_fd("\n", 1); - - return 1; + return (1); } diff --git a/src/builtins/env.c b/src/builtins/env.c new file mode 100644 index 0000000..03597df --- /dev/null +++ b/src/builtins/env.c @@ -0,0 +1,34 @@ +#include +#include +#include "minishell.h" +#include "execution.h" +#include "libft.h" + +int env_exec(const t_execution_env *env) +{ + const t_vec *exported; + size_t i; + char *string; + + exported = &env->vars->exported; + i = 0; + while (i + 1 < exported->size) + { + string = *(char **)ft_vec_caccess(exported, i); + ft_putstr_fd(string, 1); + ft_putstr_fd("\n", 1); + ++i; + } + return (1); +} + +int ft_env(int argc, t_execution_env *env) +{ + if (argc == 1) + { + env_exec(env); + return (1); + } + ft_putstr_fd("env: invalid option\n", 1); + return (0); +} \ No newline at end of file diff --git a/src/builtins/pwd.c b/src/builtins/pwd.c new file mode 100644 index 0000000..ebfafda --- /dev/null +++ b/src/builtins/pwd.c @@ -0,0 +1,23 @@ +#include +#include +#include "minishell.h" +#include "execution.h" + +int pwd(int argc) +{ + char cwd[256]; + + if (argc > 1) + { + ft_putstr_fd("pwd: too many arguments\n", 1); + return (1); + } + else if (getcwd(cwd, sizeof(cwd)) == NULL) + perror("getcwd() error"); + else + { + ft_putstr_fd(cwd, 1); + ft_putstr_fd("\n", 1); + } + return (0); +} \ No newline at end of file diff --git a/src/execution.c b/src/execution.c index 4dc97fe..319776e 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: lnikolov ret_val = cd(count_fields(fields), fields, env); if (!ft_strcmp(fields[0], "echo")) env->ret_val = echo(count_fields(fields), fields); + if (!ft_strcmp(fields[0], "pwd")) + env->ret_val = pwd(count_fields(fields)); + if (!ft_strcmp(fields[0], "env")) + env->ret_val = ft_env(count_fields(fields), env); else return (-1); return (0); -- 2.30.2