-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.c
311 lines (253 loc) · 6.48 KB
/
main1.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/******************************************************************************
*
* File Name........: main1.c
*
* Description......: Micro shell
*
* Author...........: Ankit Gohil
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h> //getpriority() & setpriority()
#include <sys/resource.h> //getpriority() & setpriority()
#include <signal.h>
#include "parse.h"
//#include "init.h"
#include "builtins.h"
int nice_flag;
long int priority_val;
long int original_priority_val;
int get_fd(Token t, char* file_name){
int fd;
switch(t){
case Tin:
fd = open(file_name, O_RDONLY, 0666);
break;
case Tout:
case ToutErr:
fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
break;
case Tapp:
case TappErr:
fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
break;
}
return fd;
}
/*
* If nice command, check if a numeric value is passed with the command.
* If yes, change the Cmd struct by shifting left the args to two places.
* Else, shift left by one position and set the default priority to 4.
*/
int get_priority(Cmd c, long int* priority){
int shift = 2;
char* remain_str;
if(c->nargs == 1){
printf("Incorrect number of arguments passed to %s\n", c->args[0]);
return 0;
}
if(c->nargs > 1){
*priority = strtol(c->args[1], &remain_str, 10);
if(*remain_str != '\0'){
*priority = 4; //Default priority
shift = 1;
}
}
int i;
for(i=0; i < c->nargs - shift; i++){
strcpy(c->args[i], c->args[i+shift]);
}
int j;
for(j=i; j < c->nargs; j++){
c->args[j] = '\0';
free(c->args[j]);
}
c->nargs = c->nargs-shift;
//ps -fl -C "perl test.pl" to display nice value of the program
}
void launch_process(Cmd command, int infile_fd, int outfile_fd){
int index;
/* Set the standard input/output channels of the new process. */
if (infile_fd != STDIN_FILENO){
dup2 (infile_fd, STDIN_FILENO);
close (infile_fd);
}
if (outfile_fd != STDOUT_FILENO){
dup2 (outfile_fd, STDOUT_FILENO);
close (outfile_fd);
}
//Check if command is in shell_builtins, execute it
if((index = is_builtin(command->args[0]))){
exec_builtin(command, infile_fd, outfile_fd, index);
return;
}
if (execvp(command->args[0], command->args) == -1) {
perror("ush");
exit(1);
}
}
void exec_pipe(Pipe p){
Cmd c;
int pipe_fd[2], infile_fd, outfile_fd;
pid_t pid, wpid;
int status;
int index; //Index for built-in command in the builtins array
c = p->head;
if(c==NULL)
return;
//If command has input redirection
if(c->in == Tin){
infile_fd = get_fd(c->in, c->infile);
}else{
infile_fd = STDIN_FILENO;
}
while(c!=NULL){
//Set pipes if there is more than one command in the pipeline
if(c->next){
if(pipe(pipe_fd) == -1){
perror("Pipe error");
return;
}
outfile_fd = pipe_fd[1];
}else{
if(c->out!=Tnil){
outfile_fd = get_fd(c->out, c->outfile);
}else{
outfile_fd = STDOUT_FILENO;
}
}
if(strcmp(c->args[0], "nice") == 0){
get_priority(c,&priority_val);
nice_flag = 1;
// printf("Priority Value = %ld \n", priority_val);
// printf("Number of args = %d \n", c->nargs);
}
/*
*Built-in commands are executed within shell.
*If a built-in command occurs as any component of a pipeline
*except the last, it is executed in a subshell
*/
//Check if it's a built-in command and last in the pipeline
if((index = is_builtin(c->args[0])) && c->next == NULL){
/*If built-in was launched with modified priority, change
it's priority*/
if(nice_flag){
original_priority_val = getpriority(PRIO_PROCESS, 0);
setpriority(PRIO_PROCESS, 0, priority_val);
}
exec_builtin(c, infile_fd, outfile_fd, index);
if(nice_flag){
setpriority(PRIO_PROCESS, 0, original_priority_val);
}
}else{
//Fork a process and execute it
pid = fork();
if (pid == 0) {
// Child process
if(nice_flag){
setpriority(PRIO_PROCESS, 0, priority_val);
//printf("Priority = %ld", priority_val);
}
launch_process(c, infile_fd, outfile_fd);
}else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
/* Clean up after pipes. */
if (infile_fd != STDIN_FILENO)
close (infile_fd);
if (outfile_fd != STDOUT_FILENO)
close (outfile_fd);
infile_fd = pipe_fd[0];
c=c->next;
}
}
void ignore_signals(){
//'trap -l' gives a list of signals
signal(SIGQUIT,SIG_IGN);
signal(SIGHUP,SIG_IGN);
}
void exec_ushrc(char *file){
Pipe p;
int stdin_fd;
stdin_fd = dup(STDIN_FILENO);
FILE* fp;
int ushrc_fd;
char* line = NULL;
size_t len = 0;
ssize_t read;
int no_lines = 0;
fp = fopen(file, "r");
if (fp == NULL){
//printf(".ushrc file does not exist in home directory");
return;
}
ushrc_fd = fileno(fp);
dup2(ushrc_fd, STDIN_FILENO);
while ((read = getline(&line, &len, fp)) != -1) {
no_lines++;
}
rewind(fp); //To the beginning of file
int i;
for(i=0; i<no_lines; i++){
fflush(stdout);
p = parse();
while(p != NULL){
exec_pipe(p);
p = p->next;
}
//exec_pipe(p);
freePipe(p);
}
dup2(stdin_fd, STDIN_FILENO);
}
void init_shell(){
char ushrc_file[100];
char* home;
home=getenv("HOME");
sprintf(ushrc_file, "%s/.ushrc", home);
ignore_signals();
exec_ushrc(ushrc_file);
}
int main(int argc, char *argv[]){
int run_shell = 1;
Pipe p;
init_shell();
fflush(stdin);
char host[64];
gethostname(host,64);
/*The pipe handling is inspired by code at
https://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html#Launching-Jobs
*/
while(run_shell){
printf("%s%% ", host);
fflush(stdout);
p = parse();
while(p != NULL){
exec_pipe(p);
p = p->next;
}
//exec_pipe(p);
freePipe(p);
}
/*while ( 1 ) {
printf("%s%% ", host);
p = parse();
prPipe(p);
freePipe(p);
}*/
}
/*........................ end of main.c ....................................*/