MiniDevil As beautiful as a shell
builtin_echo.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* builtin_echo.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/11 05:00:41 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 04:40:33 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "builtins.h"
14 #include "libft.h"
15 
24 static int is_n_flag(char *arg)
25 {
26  int i;
27 
28  if (!arg || arg[0] != '-')
29  return (0);
30  i = 1;
31  if (!arg[i])
32  return (0);
33  while (arg[i])
34  {
35  if (arg[i] != 'n')
36  return (0);
37  i++;
38  }
39  return (1);
40 }
41 
52 int builtin_echo(char **args)
53 {
54  int i;
55  int newline;
56 
57  i = 1;
58  newline = 1;
59  while (args[i] && is_n_flag(args[i]))
60  {
61  newline = 0;
62  i++;
63  }
64  while (args[i])
65  {
66  ft_putstr_fd(args[i], STDOUT_FILENO);
67  if (args[i + 1])
68  ft_putchar_fd(' ', STDOUT_FILENO);
69  i++;
70  }
71  if (newline)
72  ft_putchar_fd('\n', STDOUT_FILENO);
73  return (0);
74 }
int builtin_echo(char **args)
Implement the echo command.
Definition: builtin_echo.c:52
static int is_n_flag(char *arg)
Check if a given argument is a valid -n flag.
Definition: builtin_echo.c:24
Shell builtin commands prototypes.