-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscall_functions.c
More file actions
207 lines (177 loc) · 4.57 KB
/
syscall_functions.c
File metadata and controls
207 lines (177 loc) · 4.57 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "shell.h"
/* Syscall functions */
/**
* handle_sigint - Signal handler for SIGINT (Ctrl+C)
*
* @sig_num: Signal number (unused int)
*
* Return: None
*/
void handle_sigint(__attribute__((unused))int sig_num)
{
/* Print a newline */
_puts("\n");
/* Display the shell prompt */
_puts("$ ");
/* Flush the output buffer */
_putchar(BUFFER_FLUSH);
}
/**
* write_buffer - Read input from the user and handle signals
*
* @data_use: Pointer to the data structure
* @buffer: Pointer to the buffer to store the input (char)
* @length: Pointer to the length of the input
*
* Return: The number of characters read on success, -1 on failure
*/
ssize_t write_buffer(data_t *data_use, char **buffer, size_t *length)
{
ssize_t ret_value = 0;
size_t ptr_length = 0;
if (!*length)
{
free(*buffer);
*buffer = NULL;
signal(SIGINT, handle_sigint);
/* Read input using _getline or getline */
#if !SYS_GETLINE
ret_value = _getline(data_use, buffer, &ptr_length);
#else
ret_value = getline(buffer, &ptr_length, stdin);
#endif
/* Process input if characters are read */
if (ret_value > 0)
{
/* Remove the newline character if present */
if ((*buffer)[ret_value - 1] == '\n')
{
(*buffer)[ret_value - 1] = '\0';
ret_value--;
}
/* Set the line_flag to indicate new input */
data_use->line_flag = 1;
/* Delete comments from the input */
delete_comments(*buffer);
/* Update the command history */
list_history(data_use, *buffer, data_use->history_count++);
{
/* Update the length and buffer_command in data_use */
*length = ret_value;
data_use->buffer_command = buffer;
}
}
}
return (ret_value);
}
/**
* get_buffer - Read input and handle signals, detect command chaining
*
* @data_use: Pointer to the data structure
*
* Return: The number of characters read on success, -1 on failure
*/
ssize_t get_buffer(data_t *data_use)
{
ssize_t ret_value = 0;
static char *buffer;
static size_t i, j, length;
char *ptr, **ptr_buffer = &(data_use->arg);
_putchar(BUFFER_FLUSH);
ret_value = write_buffer(data_use, &buffer, &length);
if (ret_value == -1)
return (-1);
if (length != 0)
{
j = i;
ptr = buffer + i;
check_chain(data_use, buffer, &j, i, length);
while (j < length)
{
if (detect_chain(data_use, buffer, &j))
break;
j++;
}
i = j + 1;
if (i >= length)
{
i = length = 0;
data_use->buffer_command_type = NORMAL_COMMAND;
}
*ptr_buffer = ptr;
return (length_string(ptr));
}
*ptr_buffer = buffer;
return (ret_value);
}
/**
* read_buffer - Read characters from the input file descriptor
* into the buffer
*
* @data_use: Pointer to the data structure
* @buffer: Buffer to store the read characters (char)
* @i: Pointer to the index to keep track of the current
* position in the buffer
*
* Return: The number of characters read on success,
* 0 if already read, -1 on failure
*/
ssize_t read_buffer(data_t *data_use, char *buffer, size_t *i)
{
/* Variable to store the return value of the read system call */
ssize_t ret_value = 0;
/* Check if characters are already read */
if (*i != 0)
return (0);
/* Read characters from the input file descriptor into the buffer */
ret_value = read(data_use->read_file_descriptor, buffer, READ_BUFFER);
/* Update the index with the number of characters read */
if (ret_value >= 0)
*i = ret_value;
/* Return the number of characters read or -1 on failure */
return (ret_value);
}
/**
* _getline - Custom implementation of getline to read input from the buffer
*
* @data_use: Pointer to the data structure
* @ptr: Pointer to the buffer to store the input (char)
* @length: Pointer to the length of the buffer
*
* Return: The number of characters read on success, -1 on failure
*/
int _getline(data_t *data_use, char **ptr, size_t *length)
{
static char buffer[MAX_BUFFER_SIZE];
static size_t i, len;
size_t j;
ssize_t ret_value = 0, str = 0;
char *p = NULL, *new_ptr = NULL, *c;
p = *ptr;
if (p != 0)
{
if (length != 0)
str = *length;
}
if (i == len)
i = len = 0;
ret_value = read_buffer(data_use, buffer, &len);
if (ret_value == -1 || (ret_value == 0 && len == 0))
return (-1);
c = strchar_locate(buffer + i, '\n');
j = c ? 1 + (unsigned int)(c - buffer) : len;
new_ptr = realloc_memory(p, str, str ? str + j : j + 1);
if (new_ptr == 0)
return (p ? free(p), -1 : -1);
if (str != 0)
concatenate_nstring(new_ptr, buffer + i, j - i);
else
copy_nstring(new_ptr, buffer + i, j - i + 1);
str += j - i;
i = j;
p = new_ptr;
if (length != 0)
*length = str;
*ptr = p;
return (str);
}