-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
146 lines (131 loc) · 2.94 KB
/
shell.c
File metadata and controls
146 lines (131 loc) · 2.94 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
/*
* Program : Shell.c
* Programmer Name: Vigneshwar Padmanaban
* Date compiled : Sep 17, 2016
*
* A mini shell implementation, which executes every commands provided in prompt. It creates child process for
* every command given in consecutive manner, without waiting for each child process to complete. Designed in a
* way to avoid crashing at any point of time.
*
*
*/
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include <sys/wait.h>
#include<stdlib.h>
//Function Declaration:
void parse(char* p,char** q,int);
void exec_cmd(char** q);
//Global variables:
char *cmd[100];
pid_t parent_pid,pid;
int status,r,child_Count=0;
//Forking function:
void exec_cmd(char** q){
pid = fork();
if(pid == 0) {
if(execvp(*q,q) < 0) {
printf("exec return:%d\n",r);
printf("Warning: Invalid command. exec failed.\n");
exit(1);
}
}
}
//Parse Function:
void parse(char* p, char** q,int exit_Sh){
int semi=0,semi_good=0,exit_shell=exit_Sh;
char **t;
t=q;
while(*p != '\0'){
q=t;
while(semi == 0 && *p != '\0'){
while(*p == ' ' || *p == '\n' || *p == '\t' || *p == ';'){
if(*p == ';' && semi_good == 1){
semi = 1;
*p = '\0';
p++;
break;
}
*p = '\0';
p++;
}
if(semi == 1 || *p == '\0'){
break;
}
*q = p;
semi_good = 1;
q++;
while(*p!='\0' && *p!='\t' && *p!=' ' && *p!='\n'){
if(*p == ';'){
semi = 1;
*p = '\0';
p++;
break;
}
p++;
}
}
if(semi_good == 1){
*q = '\0';
if(strcmp(cmd[0], "quit") == 0)
exit_shell = 1;
if(strcmp(cmd[0], "quit") != 0){
child_Count+=1;
exec_cmd(cmd);
}
semi = 0;
semi_good = 0;
if(exit_shell == 1 && *p == '\0'){
exit(0);
}
}
else
printf("Improper command format. Please corect.\n");
}
}
int main( int argc, char *argv[] ) {
parent_pid = getpid();
FILE *f;
char usr_cmd[250];
int batch_mode=0;
int ip_Return,exit_Sh=0;
char prompt_show[11];
//Batch mode:
if(argc>=2){
printf("\nBatch file being executed:%s\n",argv[1]);
batch_mode=1;
/* Opening File:*/
f = fopen(argv[1],"r");
if (f == NULL){
printf("Unsuccessful file open............................ \n");
exit(0);
}
/*Reading File */
while(fgets(usr_cmd,250,f) != NULL)
{
usr_cmd[strlen(usr_cmd)-1]='\0';
printf("\nbatch cmd line being executed:%s\n\n",usr_cmd);
parse(usr_cmd, cmd, 0);
}
}
//Interactive mode:
while(batch_mode == 0 && parent_pid==getpid()) {
sprintf(prompt_show, "Prompt -> ");
write(1, prompt_show, strlen(prompt_show));
memset(&usr_cmd[0], 0, sizeof(usr_cmd));
ip_Return = scanf("%[^\n]%*c", usr_cmd);
if(ip_Return < 0)
exit(1);
int len=strlen(usr_cmd);
if(len == 0 && usr_cmd[0] == '\0') {
while((getchar())!='\n');
}
parse(usr_cmd, cmd,exit_Sh);
for(int x=1;x<=child_Count;x++)
wait(0);
}
fclose(f);
return 0;
}