0% found this document useful (0 votes)
38 views4 pages

C C C C C

This C program acts as a simple shell that can execute commands. It uses fork to create child processes to run commands, and wait to wait for them to finish. It can also handle basic piping of commands using a pipe. The main function contains a loop that gets input, parses it, and executes any commands. Other functions support splitting input into arguments, forking child processes, and executing piped commands.

Uploaded by

leonytus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

C C C C C

This C program acts as a simple shell that can execute commands. It uses fork to create child processes to run commands, and wait to wait for them to finish. It can also handle basic piping of commands using a pipe. The main function contains a loop that gets input, parses it, and executes any commands. Other functions support splitting input into arguments, forking child processes, and executing piped commands.

Uploaded by

leonytus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdlib.

h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>

pid_t my_fork() ;
int process_args(char*, char***) ;
int isPiped(char *);
int execute_piped_command(char *);
/**
This program acts as a shell and executes simple commands.
It relies on the PATH env varibale to search the program.
To exit the program press Ctrl + C
**/
int main(int argc, char *argv[]){
pid_t pid ;
char line[255] ;

char **arg_list[255], name[BUFSIZ] ;


int status, i,k,j,l,bflag,piped;
while(1){ // non-terminating loop
printf("myshell:>") ;
fgets(line, BUFSIZ, stdin) ;
if(line[strlen(line)-1] == '\n'){
// remove the new line character from end
line[strlen(line)-1] = '\0' ;
}

piped = isPiped(line);

if(piped==1){
pid = fork();
if(pid==0){
execute_piped_command(line);
}
wait(&status);
}else{
process_args(line,arg_list) ;
k=0;
while(arg_list[k]!=NULL){

j=0;l=0;
bflag=0;

while(arg_list[k][j+1]!=NULL){
++j;
}
if(strcmp(arg_list[k][j],"&")==0){
bflag=1;
arg_list[k][j]=NULL;
}

if(arg_list[k][j]!=NULL){
while(arg_list[k][j][l+1]!='\0'){
++l;
}

if((arg_list[k][j][l]=='&')){
bflag=1;
arg_list[k][j][l]='\0';

}
}

pid = my_fork() ;
if(pid ==-1){
perror("fork failed\n") ;
exit(1) ;
}else if((pid > 0)&&(bflag==0)){
wait(&status) ;
++k;

}else if((pid>0)&&(bflag==1)){
++k;
}else{ // The child Process

if(execvp(arg_list[k][0], arg_list[k])==-1)
{
int i = 0 ;
while(arg_list[k][i] != NULL){
printf("%d:\t%s\n",
i, arg_list[k][i]) ;
++i ;
}
perror("Child process could not
exec\n") ;
exit(22) ;
}

}
}
}
}
int process_args(char *line, char ***args_list){
int i = 0 ;int j=0; char * process_list[256];
char *token ;char *token1;
token = strtok(line, ";");

while(token !=NULL){
process_list[i]=(char*)malloc(255);
strcpy(process_list[i],token);
token=strtok(NULL,";");
++i;
}
process_list[i]=NULL;
i=0;

while(process_list[i]!=NULL){

token1 = strtok(process_list[i]," ");


args_list[i]=(char**)malloc(255*255);

j=0;
//store the process name in arg_list[i][0] and the arguments
in arg_list[i][1] and the subsequent cells.
while(token1!=NULL){

args_list[i][j]=(char*)malloc(255);
strcpy(args_list[i][j],token1);
token1=strtok(NULL," ");
++j;
}

args_list[i][j]=NULL;
++i ;
}
args_list[i]=NULL;
return 0 ;
}

pid_t my_fork(){
static int count = 0 ;
count++ ;
if(count <= 20){
return(fork()) ;
}else{
return -1 ;
}
}

int execute_piped_command(char *cmd){


int fd[2] ;char **arg_list1[256];char **arg_list2[256];
char *token, prog1[256], prog2[256] ;
token = strtok(cmd, "|") ;
strcpy(prog1, token) ;
token = strtok(NULL, "|") ;

strcpy(prog2, token) ;
process_args(prog1,arg_list1);
process_args(prog2,arg_list2);
pipe(fd) ;
if(fork() > 0){ // executes prog1
close(fd[0]) ;
dup2(fd[1],1) ;
execvp(arg_list1[0][0], arg_list1[0]) ;
}else{ // executes prog2
close(fd[1]) ;
dup2(fd[0],0) ;
execvp(arg_list2[0][0], arg_list2[0]) ;
}
return -1 ;
}

int isPiped(char *str){


int i=0;
while(str[i]!='\0'){
if(str[i++]=='|')
return 1;
}
return 0;
}

You might also like