-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
60 lines (45 loc) · 1.13 KB
/
parse.c
File metadata and controls
60 lines (45 loc) · 1.13 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
#include "headers.h"
int parse_command(char *cmd)
{
if (add_to_history(cmd) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
char **arguments = (char **)malloc(BUFFER_SIZE * sizeof(char *));
for (int i = 0; i < BUFFER_SIZE; ++i)
{
arguments[i] = (char *)malloc(BUFFER_SIZE);
if (arguments[i] == NULL)
{
fprintf(stderr, "Error allocating memory\n");
for (int j = 0; j < i; ++j)
{
free(arguments[j]);
}
free(arguments);
return EXIT_FAILURE;
}
}
char delimiter[5];
delimiter[0] = ' ';
delimiter[1] = '\t';
delimiter[2] = '\n';
delimiter[3] = '\0';
int arg_count = tokenize_commands(cmd, delimiter, arguments);
if (arg_count == -1)
{
for (int i = 0; i < BUFFER_SIZE; ++i)
{
free(arguments[i]);
}
free(arguments);
return EXIT_FAILURE;
}
int res = execute_command(arguments, arg_count);
for (int i = 0; i < BUFFER_SIZE; i++)
{
free(arguments[i]);
}
free(arguments);
return res;
}