-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_functions.c
More file actions
101 lines (94 loc) · 2.45 KB
/
chain_functions.c
File metadata and controls
101 lines (94 loc) · 2.45 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
#include "shell.h"
/* Chain functions */
/**
* detect_chain - Detect and update the command type based
* on the chaining operator
*
* @data_use: A pointer to the data structure containing shell information
* @buffer: The input buffer containing the command line (char)
* @ptr: A pointer to the position in the buffer
*
* Return: 1 if a chaining operator is detected, 0 otherwise
*/
int detect_chain(data_t *data_use, char *buffer, size_t *ptr)
{
size_t j = *ptr;
/* Check for '||' operator */
if (buffer[j] == '|')
{
if (buffer[j + 1] == '|')
{
/* Replace '|' with null terminator */
buffer[j] = '\0';
j++;
data_use->buffer_command_type = OR_COMMAND;
}
}
/* Check for ';' operator */
else if (buffer[j] == ';')
{
/* Replace ';' with null terminator */
buffer[j] = '\0';
data_use->buffer_command_type = CHAIN_COMMAND;
}
/* Check for '&&' operator */
else if (buffer[j] == '&')
{
if (buffer[j + 1] == '&')
{
/* Replace '&' with null terminator */
buffer[j] = '\0';
j++;
data_use->buffer_command_type = AND_COMMAND;
}
}
/* Return 0 if no chaining operator is detected */
else
return (0);
/* Update the position pointer */
*ptr = j;
/* Return 1 to indicate a chaining operator is detected */
return (1);
}
/**
* check_chain - Adjust the buffer based on the command type
*
* @data_use: Pointer to the data structure containing shell information
* @buffer: Input buffer containing the command line (char)
* @ptr: Pointer to the position in the buffer
* @i: Current index in the buffer
* @length: Length of the buffer
*
* Return: None
*/
void check_chain(data_t *data_use, char *buffer, size_t *ptr,
size_t i, size_t length)
{
size_t j = *ptr;
/* Check for AND_COMMAND type */
if (data_use->buffer_command_type == AND_COMMAND)
{
/* If the previous command had a non-zero exit status */
if (data_use->status != 0)
{
/* Replace character at the current index with a null terminator */
buffer[i] = '\0';
/* Update the position pointer to the end of the buffer */
j = length;
}
}
/* Check for OR_COMMAND type */
if (data_use->buffer_command_type == OR_COMMAND)
{
/* If the previous command had a zero exit status */
if (data_use->status == 0)
{
/* Replace character at the current index with a null terminator */
buffer[i] = '\0';
/* Update the position pointer to the end of the buffer */
j = length;
}
}
/* Update the position pointer */
*ptr = j;
}