Commit 3c57bff8 authored by Rajiv Vaidyanathan's avatar Rajiv Vaidyanathan

Final commit

parents
myShell: myShell.c
gcc myShell.c -o myShell
Type 'make' to get the executable of the shell.
Name of the executable: myShell
Name of the code file: myShell.c
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<assert.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<signal.h>
void driver(char command[], char in[], char out[], int inVal, int outVal, int AppVal);
int readTotalCPU() {
char totalBuffer[200];
int totalFD = open("/proc/stat", O_RDONLY);
assert(totalFD != -1);
read(totalFD, totalBuffer, 200);
char *totalContent = strtok(totalBuffer, "\n");
totalContent += 5;
char *usage = strtok(totalContent, " ");
int total = 0;
while(usage != NULL) {
int temp;
sscanf(usage, "%d", &temp);
total += temp;
usage = strtok(NULL, " ");
}
close(totalFD);
return total;
}
void readProcCPU(char pid[], int *user, int *system) {
int utemp, stemp;
char path[20] = "/proc/";
char procContent[100];
strcat(path, pid);
strcat(path, "/stat");
char procBuffer[100];
int procFD = open(path, O_RDONLY);
assert(procFD != -1);
read(procFD, procBuffer, 100);
char *usage = strtok(procBuffer, " ");
int i=1;
while(usage != NULL) {
if(i==14) {
sscanf(usage, "%d", &utemp);
}
if(i==15) {
sscanf(usage, "%d", &stemp);
break;
}
usage = strtok(NULL, " ");
i++;
}
*user = utemp;
*system = stemp;
close(procFD);
}
void checkcpupercentage(char pid[]) {
int total1 = readTotalCPU();
int user1, system1;
readProcCPU(pid, &user1, &system1);
sleep(1);
int total2 = readTotalCPU();
int user2, system2;
readProcCPU(pid, &user2, &system2);
float userPerc, systemPerc;
float totalDiff = total2-total1;
float userDiff = user2-user1;
float systemDiff = system2-system1;
userPerc = userDiff/totalDiff*100;
systemPerc = systemDiff/totalDiff*100;
char temp[20], temp2[20];
sprintf(temp, "%f", userPerc);
sprintf(temp2, "%f", systemPerc);
char printUser[50] = "user mode cpu percentage: ";
char printSystem[50] = "system mode cpu percentage: ";
strcat(printUser, temp);
strcat(printUser, "\n");
write(1, printUser, 50);
strcat(printSystem, temp2);
strcat(printSystem, "\n");
write(1, printSystem, 50);
}
void checkresidentmemory(char pid[]) {
char *args[3];
args[0] = "ps";
args[1] = "-o";
args[2] = "rss=";
args[3] = pid;
args[4] = NULL;
execve("/bin/ps", args, NULL);
}
void listFiles() {
int fdLs = open("files.txt", O_CREAT | O_WRONLY, 0777);
dup2(fdLs, 1);
char *args[] = {"ls", NULL};
execve("/bin/ls", args, NULL);
close(fdLs);
}
void sortFile(char fileName[]) {
char *args[] = {"sort", fileName, NULL};
execve("/bin/sort", args, NULL);
}
void grepInput(char searchString[]) {
char *args[] = {"grep", "-a", searchString, NULL};
execve("/bin/grep", args, NULL);
}
void catInput(char file[]) {
char *args[] = {"cat", file, NULL};
execve("/bin/cat", args, NULL);
}
void executeCommands(char fileName[]) {
char *fileLines = malloc(10000*sizeof(char));
int fdExec = open(fileName, O_RDONLY);
read(fdExec, fileLines, 10000);
char *line = strtok(fileLines, "\n");
while(line!=NULL) {
int cc = fork();
if(cc>0) {
wait(NULL);
} else {
driver(line, NULL, NULL, 0, 0, 0);
exit(0);
}
line = strtok(NULL, "\n");
}
close(fdExec);
free(fileLines);
}
void handler(int sigNo) {
if(sigNo==SIGINT) {
write(1, "the program is interrupted, do you want to exit [Y/N]", 53);
char *d = (char*)malloc(sizeof(char));
read(0, d, 1);
if(*d=='Y') {
free(d);
exit(0);
}
free(d);
} else if(sigNo==SIGTERM) {
write(1, "Got SIGTERM-Leaving\n", 20);
exit(1);
}
}
void driver(char command[], char in[], char out[], int inVal, int outVal, int appVal) {
char *cmd = strtok(command, " ");
char *spid = strtok(NULL, "\n");
if(spid!=NULL) {
if(strstr(spid, " ") != NULL) {
spid = strtok(spid, " ");
}
}
if(appVal!=0) {
int fdApp = open(out, O_CREAT | O_WRONLY | O_APPEND, 0777);
dup2(fdApp, 1);
} else if(outVal!=0) {
int fdOut = open(out, O_CREAT | O_WRONLY, 0777);
dup2(fdOut, 1);
}
if(strstr(cmd, "checkcpupercentage")!=NULL) {
checkcpupercentage(spid);
} else if(strstr(cmd, "checkresidentmemory") != NULL) {
checkresidentmemory(spid);
} else if(strstr(cmd, "listFiles") != NULL) {
listFiles();
} else if(strstr(cmd, "sortFile") != NULL) {
if(inVal!=0) {
sortFile(in);
} else {
sortFile(spid);
}
} else if(strstr(cmd, "executeCommands") != NULL) {
if(inVal!=0) {
executeCommands(in);
} else {
executeCommands(spid);
}
} else if(strstr(cmd, "grep") != NULL) {
grepInput(spid);
} else if(strstr(cmd, "cat") != NULL) {
catInput(spid);
} else {
write(1, "Illegal command or arguments\n", 30);
}
}
void setInOut(char command[], char inVal[], char outVal[], int trailer, int *inP, int *outP, int *appP) {
char *out = strstr(command, ">");
char *in = strstr(command, "<");
char *app = strstr(command, ">>");
if(in != NULL) {
*inP = 1;
in += 2;
in = strtok(in, " ");
strcpy(inVal, in);
}
if(app!=NULL) {
*appP = 1;
app += 3;
app = strtok(app, "\n");
if(trailer) {
app = strtok(app, " ");
}
strcpy(outVal, app);
} else if(out != NULL) {
*outP = 1;
out += 2;
out = strtok(out, "\n");
if(trailer) {
out = strtok(out, " ");
}
strcpy(outVal, out);
}
}
void pipeDriver(char command[]) {
int pipeFD[2];
char *cmmd = strtok(command, "|");
char *cmmd2 = strtok(NULL, "\n");
cmmd2 += 1;
int inVal1=0, outVal1=0, appVal1=0, inVal2=0, outVal2=0, appVal2=0;
char in1[20], in2[20], out1[20], out2[20];
setInOut(cmmd, in1, out1, 1, &inVal1, &outVal1, &appVal1);
setInOut(cmmd2, in2, out2, 0, &inVal2, &outVal2, &appVal2);
if(pipe(pipeFD)==-1) {
write(1, "Error\n", 6);
}
int pc = fork();
if(pc>0) {
wait(NULL);
dup2(pipeFD[0], 0);
close(pipeFD[1]);
driver(cmmd2, NULL, out2, 0, outVal2, appVal2);
exit(0);
} else if(pc==0) {
dup2(pipeFD[1], 1);
close(pipeFD[0]);
driver(cmmd, in1, NULL, inVal1, 0, 0);
exit(0);
}
}
int main() {
while(1) {
struct sigaction sa;
sa.sa_handler = handler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
write(1, "myShell> ", 10);
char *command = (char *)malloc(100*sizeof(char));
read(0, command, 100);
if(strstr(command, "exit")!=NULL) {
return 0;
}
if(strlen(command)==1)
continue;
int c = fork();
if(c > 0) {
wait(NULL);
} else if(c==0) {
if(strstr(command, " ; ") != NULL) {
char *cmmd = strtok(command, ";");
char *cmmd2 = strtok(NULL, "\n");
cmmd2 += 1;
int c2 = fork();
if(c2>0) {
wait(NULL);
char in[20], out[20];
int inVal=0, outVal=0, appVal=0;
setInOut(cmmd2, in, out, 0, &inVal, &outVal, &appVal);
if(strstr(cmmd2, "|") != NULL) {
pipeDriver(cmmd2);
}
else {
driver(cmmd2, in, out, inVal, outVal, appVal);
}
exit(0);
} else if(c2==0) {
char in[20], out[20];
int inVal=0, outVal=0, appVal=0;
setInOut(cmmd, in, out, 1, &inVal, &outVal, &appVal);
if(strstr(cmmd, "|") != NULL) {
pipeDriver(cmmd);
} else {
driver(cmmd, in, out, inVal, outVal, appVal);
}
exit(0);
}
} else {
char in[20], out[20];
int inVal=0, outVal=0, appVal=0;
setInOut(command, in, out, 0, &inVal, &outVal, &appVal);
if(strstr(command, "|") != NULL) {
pipeDriver(command);
}
driver(command, in, out, inVal, outVal, appVal);
exit(0);
}
free(command);
}
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment