-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.c
267 lines (243 loc) · 6.34 KB
/
utilities.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "main.h"
void free_args(char **args, char **front);
char *get_pid(void);
char *get_env_value(char *beginning, int len);
void variable_replacement(char **args, int *exe_ret);
ssize_t get_new_len(char *line);
int should_insert_space(char previous, char current, char next);
void handle_line(char **line, ssize_t read);
void logical_ops(char *line, ssize_t *new_len);
/**
* free_args - Frees up memory taken by args.
* @args: A null-terminated double pointer containing commands/arguments.
* @front: A double pointer to the beginning of args.
*/
void free_args(char **args, char **front)
{
size_t i;
for (i = 0; args[i] || args[i + 1]; i++)
free(args[i]);
free(front);
}
/**
* get_pid - Gets the current process ID.
* Description: Opens the stat file, a space-delimited file containing
* information about the current process. The PID is the
* first word in the file. The function reads the PID into
* a buffer and replace the space at the end with a \0 byte.
*
* Return: The current process ID or NULL on failure.
*/
char *get_pid(void)
{
size_t i = 0;
char *buffer;
ssize_t file;
file = open("/proc/self/stat", O_RDONLY);
if (file == -1)
{
perror("Cant read file");
return (NULL);
}
buffer = malloc(120);
if (!buffer)
{
close(file);
return (NULL);
}
read(file, buffer, 120);
while (buffer[i] != ' ')
i++;
buffer[i] = '\0';
close(file);
return (buffer);
}
/**
* get_env_value - Gets the value corresponding to an environmental variable.
* @beginning: The environmental variable to search for.
* @len: The length of the environmental variable to search for.
*
* Return: If the variable is not found - an empty string.
* Otherwise - the value of the environmental variable.
*
* Description: Variables are stored in the format VARIABLE=VALUE.
*/
char *get_env_value(char *beginning, int len)
{
char **var_addr;
char *replacement = NULL, *temp, *var;
var = malloc(len + 1);
if (!var)
return (NULL);
var[0] = '\0';
_strncat(var, beginning, len);
var_addr = _getenv(var);
free(var);
if (var_addr)
{
temp = *var_addr;
while (*temp != '=')
temp++;
temp++;
replacement = malloc(_strlen(temp) + 1);
if (replacement)
_strcpy(replacement, temp);
}
return (replacement);
}
/**
* variable_replacement - Handles variable replacement.
* @line: A double pointer containing the command and arguments.
* @exe_ret: A pointer to the return value of the last executed command.
*
* Description: Replaces $$ with the current PID, $? with the return value
* of the last executed program, and envrionmental variables
* preceded by $ with their corresponding value.
*/
void variable_replacement(char **line, int *exe_ret)
{
int j, k = 0, len;
char *replacement = NULL, *old_line = NULL, *new_line;
old_line = *line;
for (j = 0; old_line[j]; j++)
{
if (old_line[j] == '$' && old_line[j + 1] &&
old_line[j + 1] != ' ')
{
if (old_line[j + 1] == '$')
{
replacement = get_pid();
k = j + 2;
}
else if (old_line[j + 1] == '?')
{
replacement = _itoa(*exe_ret);
k = j + 2;
}
else if (old_line[j + 1])
{
/* extract the variable name to search for */
for (k = j + 1; old_line[k] &&
old_line[k] != '$' &&
old_line[k] != ' '; k++)
;
len = k - (j + 1);
replacement = get_env_value(&old_line[j + 1], len);
}
new_line = malloc(j + _strlen(replacement)
+ _strlen(&old_line[k]) + 1);
if (!line)
return;
new_line[0] = '\0';
_strncat(new_line, old_line, j);
if (replacement)
{
_strcat(new_line, replacement);
free(replacement);
replacement = NULL;
}
_strcat(new_line, &old_line[k]);
free(old_line);
*line = new_line;
old_line = new_line;
j = -1;
}
}
}
/**
* should_insert_space - Checks whether a space should be inserted between
* characters.
* @previous: The previous character.
* @current: The current character.
* @next: The next character.
*
* Return: 1 if a space should be inserted, 0 otherwise.
*
* Description: Determines whether a space should be inserted between
* characters based on specific conditions. It checks for cases
* involving semicolons, double ampersands, and double vertical
* bars (logical operators). If the conditions are met, it returns
* 1 to indicate that a space should be inserted; otherwise, it
* returns 0.
*/
int should_insert_space(char previous, char current, char next)
{
if (current == ';')
{
if (next == ';' && previous != ' ' && previous != ';')
return (1);
else if (previous == ';' && next != ' ')
return (1);
if (previous != ' ')
return (1);
if (next != ' ')
return (1);
}
else if (current == '&' && next == '&' && previous != ' ')
return (1);
else if (current == '|' && next == '|' && previous != ' ')
return (1);
return (0);
}
/**
*handle_line - Partitions a line read from standard input as needed.
*@line: A pointer to a line read from standard input.
*@read: The length of line.
*
*Description: Spaces are inserted to separate ";", "||", and "&&".
* Replaces "#" with '\0'.
*/
void handle_line(char **line, ssize_t read)
{
char *old_line, *new_line;
char previous, current, next;
size_t i, j;
ssize_t new_len;
new_len = get_new_len(*line);
if (new_len == read - 1)
return;
new_line = malloc(new_len + 1);
if (!new_line)
return;
j = 0;
old_line = *line;
for (i = 0; old_line[i]; i++)
{
current = old_line[i];
next = old_line[i + 1];
if (i != 0)
previous = old_line[i - 1];
if (i != 0 && should_insert_space(previous, current, next))
new_line[j++] = ' ';
new_line[j++] = old_line[i];
}
new_line[j] = '\0';
free(*line);
*line = new_line;
}
/**
* logical_ops - Checks a line for logical operators "||" or "&&".
* @line: A pointer to the character to check in the line.
* @new_len: Pointer to new_len in get_new_len function.
*/
void logical_ops(char *line, ssize_t *new_len)
{
char previous, current, next;
previous = *(line - 1);
current = *line;
next = *(line + 1);
if (current == '&')
{
if (next == '&' && previous != ' ')
(*new_len)++;
else if (previous == '&' && next != ' ')
(*new_len)++;
}
else if (current == '|')
{
if (next == '|' && previous != ' ')
(*new_len)++;
else if (previous == '|' && next != ' ')
(*new_len)++;
}
}