-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirection.c
110 lines (100 loc) · 2.56 KB
/
redirection.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emohamed <emohamed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/17 18:42:46 by haarab #+# #+# */
/* Updated: 2023/09/30 21:43:12 by emohamed ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_redirection(char *arg)
{
if (!ft_strncmp(">>", arg, ft_strlen(arg)) || !ft_strncmp("<<", arg,
ft_strlen(arg)) || !ft_strncmp(">", arg, ft_strlen(arg))
|| !ft_strncmp("<", arg, ft_strlen(arg)))
{
return (1);
}
return (0);
}
int size_cmds_redirection(char **args)
{
int count;
int size;
count = 0;
size = 0;
while (args[count])
{
if (is_redirection(args[count]))
size++;
count++;
}
return (size);
}
char **clear_cmds_arg_from_direct(char **args)
{
int count;
int size;
char **stack_args;
size = size_cmds_redirection(args);
stack_args = malloc_((sizeof(char *) * (lenght_of_the_2d(args) - (size * 2)
+ 1)), NULL, 0, NULL);
count = 0;
size = 0;
while (args[count])
{
if ((count == 0 && !is_redirection(args[count]))
|| (!is_redirection(args[count])
&& !is_redirection(args[count - 1])))
{
stack_args[size] = args[count];
size++;
}
count++;
}
stack_args[size] = 0;
return (stack_args);
}
char **get_redirectinsv(int size, char **old_stack)
{
char **stack;
int suui;
int count;
stack = malloc_((sizeof(char *) * (size + 1)), NULL, 0, NULL);
suui = 0;
count = 0;
while (old_stack[count] && suui < size)
{
if (is_redirection(old_stack[count]))
{
stack[suui] = old_stack[count];
suui++;
}
count++;
}
stack[suui] = 0;
return (stack);
}
char **get_files(int size, char **old_stack)
{
char **stack;
int suui;
int count;
suui = 0;
count = 0;
stack = malloc_((sizeof(char *) * (size + 1)), NULL, 0, NULL);
while (old_stack[count] && suui < size)
{
if (is_redirection(old_stack[count]))
{
stack[suui] = old_stack[count + 1];
suui++;
}
count++;
}
stack[suui] = 0;
return (stack);
}