Add env & pwd
authorLilia-42 <nikolovalilianenkova@gmail.com>
Thu, 22 Aug 2024 15:07:07 +0000 (17:07 +0200)
committerLilia-42 <nikolovalilianenkova@gmail.com>
Thu, 22 Aug 2024 15:07:07 +0000 (17:07 +0200)
Makefile
inc/execution.h
src/builtins/echo.c
src/builtins/env.c [new file with mode: 0644]
src/builtins/pwd.c [new file with mode: 0644]
src/execution.c

index 05e8ecedb2094978f2b1e3d751a6ae908503359b..f56c1b4795f96ddd87264e24967781cfc5b31839 100644 (file)
--- 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))
 
index 84a55584b9a00426fa58ab7b9ef30e0ffe3bec3b..8a0f7ad2de63e62b414121d8b45e569b771dae06 100644 (file)
@@ -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
index 2b459831ac0c165945256ad2bf01d598960c1de9..441f920269128120608035895e060f59addeb787 100644 (file)
@@ -3,24 +3,6 @@
 #include "execution.h"
 #include <unistd.h>
 
-// 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 (file)
index 0000000..03597df
--- /dev/null
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <unistd.h>
+#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 (file)
index 0000000..ebfafda
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <unistd.h>
+#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
index 4dc97fe991e07789fadf304bba17fdebd3369012..319776e6e4f783ff83260619463243c7f61a920f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/22 12:46:42 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/08/22 16:34:14 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -761,6 +761,10 @@ int        is_builtin(const char *str)
                return (1);
        if (!ft_strcmp(str, "echo"))
                return (1);
+       if (!ft_strcmp(str, "pwd"))
+               return (1);
+       if (!ft_strcmp(str, "env"))
+               return (1);
        return (0);
 }
 
@@ -780,6 +784,10 @@ int        ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr
                env->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);