-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
101 lines (79 loc) · 2.21 KB
/
shell.c
File metadata and controls
101 lines (79 loc) · 2.21 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 "headers.h"
int main(int argc, char **argv)
{
foreground_pid = -1;
cur_jobs = 0;
strcpy(cmd_name, "");
head = NULL;
tail = NULL;
if (getcwd(PATH_TO_HISTORY_FILE, BUFFER_SIZE) == NULL)
{
perror("getcwd() error");
return EXIT_FAILURE;
}
strcat(PATH_TO_HISTORY_FILE, "/history.txt");
signal(SIGCHLD, child_termination_handler);
signal(SIGTSTP, ctrl_z_handler);
signal(SIGINT, ctrl_c_handler);
while (1)
{
if (prompt() == EXIT_FAILURE)
{
fprintf(stderr, "Error starting shell\n");
return EXIT_FAILURE;
}
char *command = readline();
if (command == NULL)
{
continue;
}
char **tokenized_commands = (char **)malloc(sizeof(char *) * BUFFER_SIZE);
if (tokenized_commands == NULL)
{
fprintf(stderr, "Error allocating memory");
free(command);
continue;
}
int memory_allocation_error = 0;
for (int i = 0; i < BUFFER_SIZE; ++i)
{
tokenized_commands[i] = (char *)malloc(BUFFER_SIZE);
if (tokenized_commands[i] == NULL)
{
memory_allocation_error = 1;
fprintf(stderr, "Error allocating memory\n");
for (int j = 0; j < i; ++j)
{
free(tokenized_commands[j]);
}
free(tokenized_commands);
free(command);
fflush(stdout);
break;
}
}
if (memory_allocation_error == 1)
{
continue;
}
char delimiter[2];
delimiter[0] = ';';
delimiter[1] = '\0';
int comm_count = tokenize_commands(command, delimiter, tokenized_commands);
if (comm_count != -1)
{
for (int i = 0; i < comm_count; ++i)
{
int rc = parse_command(tokenized_commands[i]);
}
}
for (int i = 0; i < BUFFER_SIZE; ++i)
{
free(tokenized_commands[i]);
}
free(command);
free(tokenized_commands);
fflush(stdout);
}
return 0;
}