-
Notifications
You must be signed in to change notification settings - Fork 1
/
logical_ops.c
136 lines (115 loc) · 2.64 KB
/
logical_ops.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
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
#include "shell.h"
/**
* logical_and - checks for the presence of && operator
* @buff: the command input buffer
* Description: if && is found, it splits the command and return
* the first part to main to be run as a single command. if errno
* is not 0, stops the execution for subsequent commands
* Return: pointer to the next command, or NULL if none left
*/
char *logical_and(char *buff)
{
static char *buff_hold, *save_ptr;
static int counter;
char *curr_comm, *curr_comm_cpy, *curr_comm_cpy2;
if (buff != NULL && buff_hold == NULL)
{
buff_hold = str_dup(buff);
free(buff);
}
if (buff_hold == NULL)
return (NULL);
if (counter == 0)
curr_comm = strtok_r(buff_hold, "&&", &save_ptr);
else
{
if (errno == 0)
curr_comm = strtok_r(NULL, "&&", &save_ptr);
else
curr_comm = NULL;
}
counter++;
if (curr_comm == NULL)
{
free(buff_hold);
buff_hold = save_ptr = NULL;
counter = 0;
return (NULL);
}
else
{
curr_comm_cpy = str_dup(curr_comm);
curr_comm_cpy2 = str_dup(curr_comm);
if (strtok(curr_comm_cpy, " ") == NULL)
{
errno = 98;
free(curr_comm_cpy);
free(curr_comm_cpy2);
free(buff_hold);
buff_hold = save_ptr = NULL;
counter = 0;
return (NULL);
}
if (str_cmp(strtok(curr_comm_cpy, " "), "exit") == 0)
free(buff_hold);
free(curr_comm_cpy);
return (curr_comm_cpy2);
}
}
/**
* logical_or - checks for the presence of || operator
* @buff: the command input buffer
* Description: if || is found, it splits the command and return
* the first part to main to be run as a single command
* Return: pointer to the next command, or NULL if none left
*/
char *logical_or(char *buff)
{
static char *buff_hold, *save_ptr;
static int counter;
char *curr_comm, *curr_comm_cpy, *curr_comm_cpy2;
if (buff != NULL && buff_hold == NULL)
{
buff_hold = str_dup(buff);
free(buff);
}
if (buff_hold == NULL)
return (NULL);
if (counter == 0)
curr_comm = strtok_r(buff_hold, "||", &save_ptr);
else if (counter > 0 && errno == 0)
{
free(buff_hold);
buff_hold = save_ptr = NULL;
counter = 0;
return (NULL);
}
else
curr_comm = strtok_r(NULL, "||", &save_ptr);
counter++;
if (curr_comm == NULL)
{
free(buff_hold);
buff_hold = save_ptr = NULL;
counter = 0;
return (NULL);
}
else
{
curr_comm_cpy = str_dup(curr_comm);
curr_comm_cpy2 = str_dup(curr_comm);
if (strtok(curr_comm_cpy, " ") == NULL)
{
errno = 99;
free(curr_comm_cpy);
free(buff_hold);
buff_hold = save_ptr = NULL;
counter = 0;
return (NULL);
}
if (str_cmp(strtok(curr_comm_cpy, " "), "exit") == 0)
free(buff_hold);
free(curr_comm_cpy);
return (curr_comm_cpy2);
}
}