-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_01.c
82 lines (75 loc) · 2 KB
/
pipe_01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe_01.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: prranges <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/06 10:56:13 by prranges #+# #+# */
/* Updated: 2021/12/06 10:56:15 by prranges ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int number_of_pipes(char *str)
{
int count;
int flag_sq;
int flag_dq;
flag_sq = 2;
flag_dq = 2;
count = 0;
while (*str)
{
if ((*str == '|') && (!(flag_sq % 2)) && (!(flag_dq % 2)))
count++;
if (*str == '\'' && (!(flag_dq % 2)))
flag_sq++;
if (*str == '\"' && (!(flag_sq % 2)))
flag_dq++;
str++;
}
return (count);
}
int find_pipe(char *str)
{
int i;
int flag_sq;
int flag_dq;
flag_sq = 2;
flag_dq = 2;
i = 0;
while (str[i])
{
if ((str[i] == '|') && (!(flag_sq % 2)) && (!(flag_dq % 2)))
return (i);
if (str[i] == '\'' && (!(flag_dq % 2)))
flag_sq++;
if (str[i] == '\"' && (!(flag_sq % 2)))
flag_dq++;
i++;
}
return (i);
}
char **make_substrs_pipe_devided(char *str, t_arg *args)
{
int i;
int start;
int len;
char *p;
char **sub_strs;
i = 0;
p = str;
sub_strs = malloc(sizeof(char **) * (number_of_pipes(p) + 2));
if (!sub_strs)
my_exit(args, "malloc", 12, 0);
start = 0;
while (i < number_of_pipes(p) + 1)
{
len = find_pipe(p + start);
sub_strs[i] = ft_substr(p, start, len);
start += len + 1;
i++;
}
sub_strs[i] = NULL;
return (sub_strs);
}