-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.c
More file actions
167 lines (154 loc) · 4.61 KB
/
Copy pathoperator.c
File metadata and controls
167 lines (154 loc) · 4.61 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "main.h"
/**
* handle_and_or_true - Handle AND or OR operations with true condition
* @or: The original command string
* @x: The logical operator and the following command
* @and: If true, it's a logical AND operation; otherwise, it's a logical OR operation
*/
void handle_and_or_true(char *or, char *x, bool and)
{
char *compute, *new_str;
int n, result;
compute = malloc(strlen(or) - strlen(x) + 2);
strncpy(compute, or, strlen(or) - strlen(x));
result = exec_command(compute);
new_str = malloc(strlen(x) - 1);
for (n = 0; x[n + 2] != '\0'; n++)
{
new_str[n] = x[n + 2];
}
new_str[n] = '\0';
if (and)
{
if (result == -1)
_operator(false, new_str, false, false);
else
_operator(true, new_str, false, false);
}
else
{
if (result == -1)
_operator(true, new_str, false, false);
else
_operator(false, new_str, false, true);
}
// free(compute);
// free(new_str);
}
/**
* _operator - Handle logical AND and OR operations
* @doIt: Indicates whether to execute the command
* @string: The input string
* @start: Indicates if it's the start of a logical operation
* @wasOr: Indicates if the previous logical operation was an OR operation
*/
void _operator(bool doIt, char *string, bool start, bool wasOr)
{
char *new_str = NULL, *compute = NULL, *or;
char *x = strstr(string, "&&");
char *y = strstr(string, "||");
int n, result;
unsigned long int w = (unsigned long int)x;
unsigned long int z = (unsigned long int)y;
if (((w < z) && w != 0) ||(w && z == 0))
{
if (start == true || (doIt == true))
{
or = malloc(sizeof(string));
or = strdup(string);
handle_and_or_true(or, x, true);
} else
{
new_str = malloc(strlen(x));
for (n = 0; n < (_strlen(x) - 2); n++)
{
new_str[n] = x[n + 2];
}
new_str[n] = '\0';
_operator(wasOr? true : false, new_str, false, false);
}
} else if (((z < w) && z != 0) || (z && w == 0))
{
if ((doIt == true) || start == true)
{
or = malloc(sizeof(string));
or = strdup(string);
handle_and_or_true(or, y, false);
} else
{
new_str = malloc(strlen(y));
for (n = 0; n < (_strlen(y) - 2); n++)
{
new_str[n] = y[n + 2];
}
new_str[n] = '\0';
_operator(wasOr? false : true, new_str, false, true);
}
} else
{
if (doIt == true && wasOr == false)
exec_command(string);
}
}
// /**
// * _operator - Handle logical AND and OR operations
// * @doIt: Indicates whether to execute the command
// * @string: The input string
// * @start: Indicates if it's the start of a logical operation
// * @wasOr: Indicates if the previous logical operation was an OR operation
// */
// void _operator(bool doIt, char *string, bool start, bool wasOr)
// {
// char *new_str = NULL, *compute = NULL, *or = NULL;
// char *x = strstr(string, "&&");
// char *y = strstr(string, "||");
// int n, result;
// unsigned long int w = (unsigned long int)x;
// unsigned long int z = (unsigned long int)y;
// if ((w < z && w != 0) || (w && z == 0))
// {
// if (start == true || doIt == true)
// {
// or = strdup(string);
// handle_and_or_true(or, x, true);
// free(or);
// }
// else
// {
// new_str = malloc(strlen(x));
// for (n = 0; n < (_strlen(x) - 2); n++)
// {
// new_str[n] = x[n + 2];
// }
// new_str[n] = '\0';
// _operator(wasOr ? true : false, new_str, false, false);
// }
// }
// else if ((z < w && z != 0) || (z && w == 0))
// {
// if (doIt == true || start == true)
// {
// or = strdup(string);
// handle_and_or_true(or, y, false);
// free(or);
// }
// else
// {
// new_str = malloc(strlen(y));
// for (n = 0; n < (_strlen(y) - 2); n++)
// {
// new_str[n] = y[n + 2];
// }
// new_str[n] = '\0';
// _operator(wasOr ? false : true, new_str, false, true);
// }
// }
// else
// {
// if (doIt == true && !wasOr)
// {
// exec_command(string);
// }
// }
// free(new_str);
// }