-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommand_line.c
More file actions
61 lines (55 loc) · 1.33 KB
/
command_line.c
File metadata and controls
61 lines (55 loc) · 1.33 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
#include "header.h"
bool command_line()
{
char *arg[1024];
bool flag_pipe = false;
bool flag_redirection = false;
bool flag_background = false;
int type = 0;
int index = 0;
int how = 0;
fgets(input, 1024, stdin);
tok = tokens;
ptr = input;
type = get_token(&arg[index++]);
while (type != EOL)
{
type = get_token(&arg[index]);
if(type == AMPERSAND)
flag_background = true;
if(type == ARG)
index++;
}
arg[index] = NULL;
if (!strcmp(arg[0], "quit") || !strcmp(arg[0], "exit"))
return false;
else
{
if (!strcmp(arg[0], "cd"))
{
change_directory(arg, index);
return true;
}
flag_pipe = is_pipe(arg, index);
flag_redirection = is_redirection(arg, index);
if (flag_pipe)
process_pipe(arg, index, how);
else if (flag_redirection)
fork_and_call_process_redir(arg, index);
else if (flag_background)
background(arg, index);
else
{
int pid = fork();
int status = 0;
if(pid==0)
{
execvp(*arg, arg);
exit(1);
}
else
waitpid(-1,&status,0);
}
return true;
}
}