-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
144 lines (117 loc) · 3.4 KB
/
main.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
/**
main.c
Purpose: Core of posix-shell application
@author Michal Stefaniuk
@version 1.0
*/
#include "globals.h"
#include "functions.h"
#include <stdio.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define OPTION_FOUND 1
#define OPTION_NOT_FOUND 0
#define TRUE 1
#define OPTIONS 4
char *line_of_text = NULL, current_directory[1024];
char *FUNCTION[4] = {"<", ">", "|", "&"};
char *tmp_pointer = NULL, *home_dir;
char **input_arguments;
int ifFoundFlag = 0;
int it, iterator = 0;
int main()
{
globals value;
home_dir = getenv("HOME");
while(1)
{
ssize_t size = 0;
ifFoundFlag = 0;
if(getcwd(current_directory, sizeof(current_directory)) != NULL)
{
printf("\n[%s]#", current_directory);
}
getline(&line_of_text, &size, stdin);
input_arguments = parser(line_of_text);
char *first_input_cmd = input_arguments[0];
char *second_input_cmd = input_arguments[1];
// If no command was given
if(first_input_cmd == NULL)
{
free(line_of_text);
free(input_arguments);
continue;
}
// Handle "exit" command
if(strcmp(first_input_cmd,"exit") == 0)
{
break;
}
// Handle "cd" command
else if(strcmp(first_input_cmd,"cd") == 0)
{
if(second_input_cmd == NULL)
{
if(home_dir[0] != 0)
{
chdir(home_dir);
}
}
else
{
chdir(second_input_cmd);
}
}
// Handle other comments
else
{
it = 1;
while(input_arguments[it] != NULL)
{
for(iterator = 0; iterator < OPTIONS; iterator++)
{
if(strcmp(input_arguments[it],FUNCTION[iterator]) == 0)
break;
}
// One of OPTIONS was found
if(iterator < OPTIONS)
{
ifFoundFlag = OPTION_FOUND;
if(iterator < 3 && input_arguments[it+1] == NULL)
{
break;
}
// IORedirection
if(iterator < 2)
{
redirect(it, input_arguments, iterator);
}
// Pipe
else if(iterator == 2)
{
pipeIO(it, input_arguments);
}
// Run in background
else if(iterator == 3)
{
process_executor(it, input_arguments);
}
break;
}
it++;
}
// If IO/Pipe/Background was not found
// than handle other commands for instance "ls"
if(ifFoundFlag == OPTION_NOT_FOUND)
{
process_executor(0, input_arguments);
}
}
free(line_of_text);
free(input_arguments);
}
}