-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
105 lines (88 loc) · 1.96 KB
/
Copy pathexecute.c
File metadata and controls
105 lines (88 loc) · 1.96 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
102
103
104
#include "header.h"
void execute(char **tokens , int *pos, char* home,int cno,int noOfCommands)
{
int i, j, flag, fd, pos1 = -1, pos2 = -1, posf, test;
int in = dup(0);
int out = dup(1);
for(i=0;tokens[i]!=NULL;i++)
{
if(tokens[i][0] == '<')
{
fd=open(tokens[i+1], O_RDONLY);
test = dup2(fd,0);
if(test == -1)
{
perror("dup error");
return ;
}
close(fd);
pos1 = i;
}
else if(tokens[i][0] == '>')
{
if(tokens[i][1] == '>')
fd=open(tokens[i+1],O_CREAT | O_WRONLY |O_APPEND, 0644);
else
fd=open(tokens[i+1],O_CREAT | O_WRONLY | O_TRUNC, 0644);
test = dup2(fd,1);
if(test == -1)
{
perror("dup error");
return ;
}
close(fd);
pos2 = i;
}
}
if(pos1 != -1 && pos2 != -1)
{
posf = (pos1 < pos2) ? pos1 : pos2;
tokens[posf] = NULL;
}
else if(pos1 != -1)
tokens[pos1]=NULL;
else if(pos2 != -1)
tokens[pos2] = NULL;
*pos-=1;
if(strcmp(tokens[0], "cd")==0)
command_cd(tokens, home);
else if(strcmp(tokens[0], "pwd")==0)
command_pwd(tokens);
else if(strcmp(tokens[0], "pinfo")==0)
command_pinfo(tokens);
else if(strcmp(tokens[0], "echo")==0)
command_echo(tokens);
else if(strcmp(tokens[0],"listjobs") == 0)
listjobs();
else if(strcmp(tokens[0],"sendsig") == 0)
sendsig(tokens);
else if(strcmp(tokens[0],"killallbg") == 0)
{
killallbg();
}
else if(strcmp(tokens[0],"fg") == 0)
{
movetofg(tokens);
}
else if(strcmp(tokens[0],"quit") == 0)
exit(0);
else
{
// char *argv[] = { "ls", "-l", 0 };
// execvp(tokens[0], tokens);
// printf("%c\n",tokens[pos][strlen(tokens[pos])-1] );
// printf("%c\n",tokens[*pos][0] );
if(tokens[*pos][strlen(tokens[*pos])-1] == '&')
{
// printf("Hi" );
tokens[*pos][strlen(tokens[*pos])-1]=0;
system_command_background(tokens);
}
else if(tokens[*pos][0] == '&')
system_command_background(tokens);
else
system_command_foreground(tokens,cno,noOfCommands);
dup2(in,0);
dup2(out,1);
}
}