Add echo builtin function
authorLilia-42 <nikolovalilianenkova@gmail.com>
Thu, 22 Aug 2024 11:40:10 +0000 (13:40 +0200)
committerLilia-42 <nikolovalilianenkova@gmail.com>
Thu, 22 Aug 2024 11:40:10 +0000 (13:40 +0200)
Makefile
inc/execution.h
src/builtins/echo.c [new file with mode: 0644]
src/execution.c

index b5ed7458364461cac9b15c752dbc68786cb65705..05e8ecedb2094978f2b1e3d751a6ae908503359b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,6 +34,7 @@ SOURCES :=    main.c                          \
                        helpers.c                       \
                                                                \
                        builtins/cd.c           \
+                       builtins/echo.c         \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
 
index b413c7b0eb51f61846acd4ed06873a0c4be97975..84a55584b9a00426fa58ab7b9ef30e0ffe3bec3b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/02 17:40:19 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/02 17:42:18 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/22 13:35:41 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,5 +18,6 @@ const char    *get_env_var_value(const t_execution_env *env,
 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);
 
 #endif // EXECUTION_H
diff --git a/src/builtins/echo.c b/src/builtins/echo.c
new file mode 100644 (file)
index 0000000..2b45983
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include "minishell.h"
+#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')
+               return (1);
+
+       return (0);
+}
+
+int    echo(int argc, char **argv)
+{
+       if (argc == 1)
+               return (1);
+       int i = 1;
+       int flag = 0;
+       int t;
+
+       t = ft_check_n(argv[i]);
+       if (t == 1)
+       {
+               flag = 1;
+               i++;
+       }
+       while (i < argc)
+       {
+               ft_putstr_fd(argv[i], 1);
+               if (i + 1 < argc)
+                       ft_putstr_fd(" ", 1);
+               i++;
+       }
+       if (flag == 0)
+               ft_putstr_fd("\n", 1);
+
+       return 1;
+}
index 819fcf1ba3de876cacc048e40c75da493b8c9e2a..4dc97fe991e07789fadf304bba17fdebd3369012 100644 (file)
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   execution.c                                        :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/02 18:35:53 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/22 12:46:42 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -759,6 +759,8 @@ int is_builtin(const char *str)
 {
        if (!ft_strcmp(str, "cd"))
                return (1);
+       if (!ft_strcmp(str, "echo"))
+               return (1);
        return (0);
 }
 
@@ -776,6 +778,8 @@ int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr
 {
        if (!ft_strcmp(fields[0], "cd"))
                env->ret_val = cd(count_fields(fields), fields, env);
+       if (!ft_strcmp(fields[0], "echo"))
+               env->ret_val = echo(count_fields(fields), fields);
        else
                return (-1);
        return (0);