Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/execution/my_mess/my_echo.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* my_echo.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/08/27 10:42:26 by ssabbaji #+# #+# */
9
/* Updated: 2022/09/30 16:52:10 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../../minishell.h"
14
15
void print_rest(char **cmd, int i, int flag)
16
{
17
char *echo;
18
char *free_echo;
19
20
if (flag)
21
{
22
echo = join_cmd(cmd, count_cmds(cmd), i);
23
free_echo = echo;
24
echo = ft_substr(echo, 0, ft_strlen(echo) - 1);
25
printf("%s", echo);
26
printf("\n");
27
}
28
if (!flag)
29
{
30
echo = join_cmd(cmd, count_cmds(cmd), i);
31
free_echo = echo;
32
echo = ft_substr(echo, 0, ft_strlen(echo) - 1);
33
printf("%s", echo);
34
}
35
free(free_echo);
36
}
37
38
int start_index(char **cmd)
39
{
40
int idx;
41
int i;
42
43
i = 1;
44
idx = 0;
45
while (cmd[i])
46
{
47
if (!check_valid(cmd[i]))
48
return (i);
49
i++;
50
}
51
return (0);
52
}
53
54
int my_echo(t_cmd *cmd_lst)
55
{
56
char **cmd;
57
int idx;
58
59
idx = 0;
60
cmd = cmd_lst->cmd;
61
if (!cmd[1])
62
write(1, "\n", 1);
63
idx = start_index(cmd);
64
if (!idx)
65
return (0);
66
if (idx == 1)
67
print_rest(cmd, idx, 1);
68
else
69
print_rest(cmd, idx, 0);
70
return (0);
71
}
72
73