-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
executable file
·214 lines (191 loc) · 6.03 KB
/
shell.c
File metadata and controls
executable file
·214 lines (191 loc) · 6.03 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<dirent.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<signal.h>
#include<sys/resource.h>
#include<time.h>
#include<sys/types.h>
extern struct stat statRes;
extern struct rusage usage;
extern char *command,cpy_cmd[100];
extern char home[100],pwd[100],dir[100],user[256],host[256];
extern int pidlst[20000][3];
extern int pidnumber;
extern int pids[1000]; // For the pid
extern char pidname[1000][200];
extern char input_file[100], output_file[100];
extern int inputD, outputD;
extern int current_fg;
extern int amIPiped[100], isNxtPiped[100];
extern int piping[100][2];
extern int cmdcnt;
//============================================================//
// Summary about each of the variable used.
// statRes ans usage are used in ls to get the information about the file.
// command contains a single command after strtok(semicolon).
// cpy_cmd contains the copy of the full command.
// home contains hoem name, pwd contains present working directory.
// dir, user, host are similar variables for working with directories.
// pidlst pidname contains the background processes.
// input_file contains the name of input redirection file.
// output_file contains the name of output redirection file.
//============================================================//
void pidover()
{
for (int i=0;i<pidnumber;i++)
{
if(pidlst[i][0]==0)
{
// printf("%s with pid %d exited \n", pidname[i], pidlst[i][1]);
// pidlst[i][0]=1;
}
}
}
long long int len(char string[])
{
long long int i;
while(string[i] == '\0')
i++;
return i;
}
void pinfo2(char *pid);
void ls();
void hme();
void pd();
void cd();
void username();
void vi();
void vim();
void hostname();
void echo();
void showpwd();
void printEveryTime();
void pinfo();
void history();
void addTohist();
void envSet();
void unenvSet();
void kjob();
void jobs();
void overkill();
void fg();
void bg();
void INThandler();
void INThandlerz();
void execute_cmd();
int main(void)
{
signal(SIGINT, INThandler);
signal(SIGTSTP, INThandlerz);
hme();
username();
hostname();
int pid, status;
while(1)
{
strcpy(input_file, "\0");
strcpy(output_file, "\0");
pidover();
int direction =0;
char *every, semicolon[100];
char *actual_cmd[10][100],*sccmd[100];
printEveryTime();
fgets(semicolon,100,stdin);
char *everysemi = semicolon;
inputD=0; outputD=0;
//============================================================================================//
// Getting multiple commands and splitting it.
//============================================================================================//
while((command = strtok_r(everysemi, ";",&everysemi)))
{
addTohist(command);
//============================================================//
// Getting the single command and splitting it.
// cpy_cmd contains the copy of the command we get.
//============================================================//
if(command[strlen(command)-1] == '\n')
command[strlen(command)-1]='\0';
if(strlen(command)!=0)
{
//======================================_======================================================//
// Command Parsing starts here.
//============================================================================================//
for(int i =0;i<=cmdcnt;i++)
{isNxtPiped[i]=0; amIPiped[i]=0;}
strcpy(cpy_cmd,command);
every = strtok(command, " ");
int len=0;
cmdcnt =0;
while(every!=0)
{
int pipeD=0;
// printf("%s\n", every);
if(strcmp(every,"\n")!=0)
{
if(every[strlen(every)-1]=='\n')
every[strlen(every)-1]='\0';
//============================================================//
// Checking input, output redirection here.
// direction = 1 means we need to take the input.
// direction = 2 means we need to give the output.
// direction = 3 means we ened to append the output
//============================================================//
if(direction == 1)
{strcpy(input_file, every); direction = 0; inputD = 1; }
else if(direction == 2)
{strcpy(output_file, every); direction = 0; outputD = 1; }
else if(direction ==3 )
{strcpy(output_file, every); direction = 0; outputD = 2;}
if(strcmp(every,"<")==0)
direction = 1;
else if(strcmp(every, ">")==0)
direction = 2;
else if(strcmp(every, ">>")==0)
direction = 3;
else if(strcmp(every, "|") == 0)
{
isNxtPiped[cmdcnt] = 1;
actual_cmd[cmdcnt][len]=NULL;
pipe(piping[cmdcnt]);
execute_cmd(actual_cmd[cmdcnt], len);
cmdcnt++;
len =0;
amIPiped[cmdcnt] = 1;
direction = 0;
pipeD =1;
inputD = 0;
outputD =0;
}
if(direction == 0 && !inputD && !outputD && !pipeD)
{
actual_cmd[cmdcnt][len] = every;
//printf("actual_cmd = %s\n", actual_cmd[cmdcnt][len]);
if(actual_cmd[cmdcnt][len][strlen(actual_cmd[cmdcnt][len])-1]=='\n')
actual_cmd[cmdcnt][len][strlen(actual_cmd[cmdcnt][len])-1]='\0';
len++;
}
//============================================================//
}
every = strtok(0, " ");
}
actual_cmd[cmdcnt][len]=NULL;
//============================================================================================//
// Command parsing ends here.
//============================================================================================//
//============================================================================================//
// Checking the command and executing it starts here.
//============================================================================================//
execute_cmd(actual_cmd[cmdcnt], len);
//============================================================================================//
// Command Execution ends here.
//============================================================================================//
}
}
}
return 0;
}