Commit 1e0b1296 authored by desiredeveloper's avatar desiredeveloper

reaping children in case of error

parent dda893a2
No preview for this file type
......@@ -6,94 +6,106 @@
#include <time.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
extern char **environ;
void funcExit(){
void funcExit()
{
exit(0);
}
void funcEnv(){
void funcEnv()
{
int i;
for (i = 0; environ[i] != NULL; i++)
printf("%s\n",environ[i]);
printf("%s\n", environ[i]);
}
void funcClear(){
void funcClear()
{
printf("\e[1;1H\e[2J");
}
void funcDir(char **tokens){
char a[150]="ls -la ";
system(strcat(a,tokens[1]));
void funcDir(char **tokens)
{
char a[150] = "ls -la ";
system(strcat(a, tokens[1]));
}
void funcPwd(){
char *temp = (char *)malloc(sizeof(char)*150);
getcwd(temp,150);
printf("%s:$ ",temp);
void funcPwd()
{
char *temp = (char *)malloc(sizeof(char) * 150);
getcwd(temp, 150);
printf("%s:$ ", temp);
free(temp);
}
void funcCd(char *fpath){
void funcCd(char *fpath)
{
char *temp = (char *)malloc(sizeof(char)*150);
getcwd(temp,150);
char *temp = (char *)malloc(sizeof(char) * 150);
getcwd(temp, 150);
if(fpath && chdir(fpath)!=0){
if (fpath && chdir(fpath) != 0)
{
printf("ERROR: Directory doesn't exist!\n");
return;
}
printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n",getenv("OLDPWD"),getenv("PWD"));
if(fpath){
setenv("OLDPWD",temp,1);
getcwd(temp,150);
setenv("PWD",temp,1);
printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n", getenv("OLDPWD"), getenv("PWD"));
if (fpath)
{
setenv("OLDPWD", temp, 1);
getcwd(temp, 150);
setenv("PWD", temp, 1);
}
else
setenv("OLDPWD",temp,1);
setenv("OLDPWD", temp, 1);
printf("AFTER:\nOLDPWD=%s\nPWD=%s\n",getenv("OLDPWD"),getenv("PWD"));
printf("AFTER:\nOLDPWD=%s\nPWD=%s\n", getenv("OLDPWD"), getenv("PWD"));
free(temp);
}
void ioRedirect(char **tokens,int n){
void ioRedirect(char **tokens, int n)
{
FILE *fp;
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0;
int j = 0;
for(int i=0;strcmp(tokens[i],">>") && strcmp(tokens[i],">") && strcmp(tokens[i],"<");i++){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i]);
for (int i = 0; strcmp(tokens[i], ">>") && strcmp(tokens[i], ">") && strcmp(tokens[i], "<"); i++)
{
arglist[j] = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
strcpy(arglist[j++], tokens[i]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
arglist[j] = NULL;
char *temp = (char *)malloc(sizeof(char) * 150);
switch (fork()) {
switch (fork())
{
case -1:
break;
case 0:
if(!strcmp(tokens[n-2],">>"))
fp = freopen(tokens[n-1],"a",stdout);
else if(!strcmp(tokens[n-2],">"))
fp = freopen(tokens[n-1],"w",stdout);
if (!strcmp(tokens[n - 2], ">>"))
fp = freopen(tokens[n - 1], "a", stdout);
else if (!strcmp(tokens[n - 2], ">"))
fp = freopen(tokens[n - 1], "w", stdout);
else
fp = freopen(tokens[n-1],"r",stdin);
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
fp = freopen(tokens[n - 1], "r", stdin);
strcpy(temp, "/bin/");
strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1)
{
strcpy(temp, "/usr/bin/");
strcat(temp, arglist[0]);
if(execv(temp, arglist)==-1)
exit(0);
}
free(temp);
}
wait(NULL);
for(int i=0;arglist[i]!=NULL;i++)
for (int i = 0; arglist[i] != NULL; i++)
free(arglist[i]);
free(arglist);
......@@ -101,146 +113,168 @@ void ioRedirect(char **tokens,int n){
// http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal-sigchld-266290/
void handler(int sig){
char *buffer = (char *)malloc(sizeof(char)*100);
char *temp = (char *)malloc(sizeof(char)*150);
void handler(int sig)
{
char *buffer = (char *)malloc(sizeof(char) * 100);
char *temp = (char *)malloc(sizeof(char) * 150);
pid_t pid;
pid = wait(NULL);
printf("\nBackground process [%d] finished.\n", pid);
signal(SIGCHLD, NULL);
getcwd(temp,150);
sprintf(buffer,"%s:$ ",temp);
write(1,buffer,50);
getcwd(temp, 150);
sprintf(buffer, "%s:$ ", temp);
write(1, buffer, 50);
free(temp);
free(buffer);
}
void exBackground(char **tokens,int n){
void exBackground(char **tokens, int n)
{
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0;
int j = 0;
for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&")!=0 ){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
for (int i = 0; i < n; i++)
{
while (tokens[i] != NULL && strcmp(tokens[i], "&") != 0)
{
arglist[j] = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
strcpy(arglist[j++], tokens[i++]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
arglist[j] = NULL;
char *temp = (char *)malloc(sizeof(char) * 150);
signal(SIGCHLD, handler);
switch (fork()) {
switch (fork())
{
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
strcpy(temp, "/bin/");
strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1)
{
strcpy(temp, "/usr/bin/");
strcat(temp, arglist[0]);
if(execv(temp, arglist)==-1)
exit(0);
}
free(temp);
}
j=0;
j = 0;
}
for(int i=0;arglist[i]!=NULL;i++)
for (int i = 0; arglist[i] != NULL; i++)
free(arglist[i]);
free(arglist);
}
void exSerial(char **tokens,int n){
void exSerial(char **tokens, int n)
{
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0;
int j = 0;
for(int i=0;i<n;i++){
for (int i = 0; i < n; i++)
{
int ioFlag = 0;
while(tokens[i]!=NULL && strcmp(tokens[i],"&&")!=0 ){
if(!ioFlag)
ioFlag=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0;
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
if(!ioFlag){
switch (fork()) {
while (tokens[i] != NULL && strcmp(tokens[i], "&&") != 0)
{
if (!ioFlag)
ioFlag = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0;
arglist[j] = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
strcpy(arglist[j++], tokens[i++]);
}
arglist[j] = NULL;
char *temp = (char *)malloc(sizeof(char) * 150);
if (!ioFlag)
{
switch (fork())
{
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
strcpy(temp, "/bin/");
strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1)
{
strcpy(temp, "/usr/bin/");
strcat(temp, arglist[0]);
if(execv(temp, arglist)==-1)
exit(0);
}
free(temp);
}
}
else {
ioRedirect(arglist,j);
else
{
ioRedirect(arglist, j);
}
wait(NULL);
j=0;
j = 0;
}
for(int i=0;arglist[i]!=NULL;i++)
for (int i = 0; arglist[i] != NULL; i++)
free(arglist[i]);
free(arglist);
}
void exParallel(char **tokens,int n){
void exParallel(char **tokens, int n)
{
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0,forkcalls=0;
int j = 0, forkcalls = 0;
for(int i=0;i<n;i++){
for (int i = 0; i < n; i++)
{
int ioFlag = 0;
while(tokens[i]!=NULL && strcmp(tokens[i],"&&&")!=0 ){
if(!ioFlag)
ioFlag=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0;
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
if(!ioFlag){
while (tokens[i] != NULL && strcmp(tokens[i], "&&&") != 0)
{
if (!ioFlag)
ioFlag = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0;
arglist[j] = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
strcpy(arglist[j++], tokens[i++]);
}
arglist[j] = NULL;
char *temp = (char *)malloc(sizeof(char) * 150);
if (!ioFlag)
{
forkcalls++;
switch (fork()) {
switch (fork())
{
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
strcpy(temp, "/bin/");
strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1)
{
strcpy(temp, "/usr/bin/");
strcat(temp, arglist[0]);
if(execv(temp, arglist)==-1)
exit(0);
}
free(temp);
}
}
else {
ioRedirect(arglist,j);
else
{
ioRedirect(arglist, j);
}
j=0;
j = 0;
}
for(int i=0;i<forkcalls;i++)
for (int i = 0; i < forkcalls; i++)
wait(NULL);
for(int i=0;arglist[i]!=NULL;i++)
for (int i = 0; arglist[i] != NULL; i++)
free(arglist[i]);
free(arglist);
}
/* Splits the string by space and returns the array of tokens
*/
......@@ -250,118 +284,137 @@ char **tokenize(char *line)
char *token = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
int i, tokenIndex = 0, tokenNo = 0;
for(i =0; i < strlen(line); i++){
for (i = 0; i < strlen(line); i++)
{
char readChar = line[i];
if (readChar == ' ' || readChar == '\n' || readChar == '\t'){
if (readChar == ' ' || readChar == '\n' || readChar == '\t')
{
token[tokenIndex] = '\0';
if (tokenIndex != 0){
tokens[tokenNo] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
if (tokenIndex != 0)
{
tokens[tokenNo] = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
strcpy(tokens[tokenNo++], token);
tokenIndex = 0;
}
} else {
}
else
{
token[tokenIndex++] = readChar;
}
}
free(token);
tokens[tokenNo] = NULL ;
tokens[tokenNo] = NULL;
return tokens;
}
int main(int argc, char* argv[]) {
int main(int argc, char *argv[])
{
char line[MAX_INPUT_SIZE];
char **tokens;
int i;
clock_t t1;
FILE* fp;
if(argc == 2) {
fp = fopen(argv[1],"r");
if(fp < 0) {
FILE *fp;
if (argc == 2)
{
fp = fopen(argv[1], "r");
if (fp < 0)
{
printf("File doesn't exists.");
return -1;
}
}
while(1) {
int parallel=0,serial=0,background=0,io=0,cdir=0;
while (1)
{
int parallel = 0, serial = 0, background = 0, io = 0, cdir = 0;
/* BEGIN: TAKING INPUT */
bzero(line, sizeof(line));
if(argc == 2) { // batch mode
if(fgets(line, sizeof(line), fp) == NULL) { // file reading finished
if (argc == 2)
{ // batch mode
if (fgets(line, sizeof(line), fp) == NULL)
{ // file reading finished
break;
}
line[strlen(line) - 1] = '\0';
} else { // interactive mode
}
else
{ // interactive mode
funcPwd();
scanf("%[^\n]", line);
getchar();
}
if(!strlen(line))
if (!strlen(line))
continue;
/* END: TAKING INPUT */
line[strlen(line)] = '\n'; //terminate with new line
tokens = tokenize(line);
for(i=0;tokens[i]!=NULL;i++){
if(!parallel)
parallel=strcmp(tokens[i],"&&&")==0;
for (i = 0; tokens[i] != NULL; i++)
{
if (!parallel)
parallel = strcmp(tokens[i], "&&&") == 0;
if(!serial)
serial=strcmp(tokens[i],"&&")==0;
if (!serial)
serial = strcmp(tokens[i], "&&") == 0;
if(!background)
background=strcmp(tokens[i],"&")==0;
if(!io)
io=strcmp(tokens[i],">>")==0 || strcmp(tokens[i],">")==0 || strcmp(tokens[i],"<")==0;
if (!background)
background = strcmp(tokens[i], "&") == 0;
if (!io)
io = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0;
}
t1 = clock();
if(parallel)
exParallel(tokens,i);
else if(serial)
exSerial(tokens,i);
else if(background)
exBackground(tokens,i);
else if(io)
ioRedirect(tokens,i);
else {
if(!strcmp(tokens[0],"dir") && i==2){
if (parallel)
exParallel(tokens, i);
else if (serial)
exSerial(tokens, i);
else if (background)
exBackground(tokens, i);
else if (io)
ioRedirect(tokens, i);
else
{
if (!strcmp(tokens[0], "dir") && i == 2)
{
funcDir(tokens);
}
else if(!strcmp(tokens[0],"clear")){
else if (!strcmp(tokens[0], "clear"))
{
funcClear();
}
else if(!strcmp(tokens[0],"quit")){
else if (!strcmp(tokens[0], "quit"))
{
funcExit();
}
else if(!strcmp(tokens[0],"env")){
else if (!strcmp(tokens[0], "env"))
{
funcEnv();
}
else if(!strcmp(tokens[0],"cd")){
else if (!strcmp(tokens[0], "cd"))
{
funcCd(tokens[1]);
}
else{
else
{
system(line);
}
}
t1 = clock() - t1;
printf("%f sec\n",(double)t1/CLOCKS_PER_SEC);
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
// Freeing the allocated memory
for(i=0;tokens[i]!=NULL;i++){
for (i = 0; tokens[i] != NULL; i++)
{
free(tokens[i]);
}
free(tokens);
}
return 0;
}
\ No newline at end of file
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