Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<string.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/wait.h>
- #include<signal.h>
- #define MAXL 500 //Max Input
- #define ARGS 20 //Max number of arguments
- //handle the signal SIGCHLD when a child process is terminated
- void signalHandler(int sig_num)
- {
- FILE *fp;
- fp=fopen("shell_logfile.log","a+");// Open log file
- if(fp == NULL)
- {
- printf("\nerror:couldn't open file");
- }
- else fprintf(fp,"Child process was terminated\n");
- fclose(fp);
- }
- void Execute(char * args[], int args_count) //Executes Command
- {
- int count=args_count;
- char cwd[1024];
- // status of the child
- int status;
- int background=0;
- //check if it's a background process or not
- if(!(strcmp(args[count-1],"&")))
- {
- //SET background flag to zero
- background=1;
- //remove the "&" so it could be executed
- args[count-1]='\0';
- count--;
- }
- // print the current directory
- if (strcmp(args[0],"pwd") == 0)
- {
- printf("%s\n", getcwd(cwd, sizeof(cwd)));
- }
- // change directory
- else if (strcmp(args[0],"cd") == 0)
- {
- changeDirectory(args);
- }
- //execute the command
- else
- {
- //process id
- pid_t pid=fork();
- // no error in forking
- if (pid >= 0)
- {
- // if it's a child process
- if(pid== 0)
- {
- execvp(args[0],args);
- }
- //if it's the parent process
- else
- {
- if(background==1) return;
- else
- {
- //suspends execution until the child specified by pid change state
- do
- {
- waitpid(pid,&status,WUNTRACED);
- }
- while(!WIFEXITED(status) && !WIFSIGNALED(status));
- //wait(&status);
- }
- }
- }
- else
- printf("\nfailed to fork!");
- }
- }
- int Parse(char line[], char *parsedArguments[]) //Parsing
- {
- int i=0;
- if((parsedArguments[i] = strtok(line," "))==NULL){
- printf("Please enter Command ");
- return 0;
- }
- int counter=1;
- while((parsedArguments[counter] = strtok(NULL, " ")) != NULL){ //Parse Using Spaces
- i++;
- }
- parsedArguments[i]= '\0';
- return i;
- }
- int ExecuteProcess(char *parsedArguments[]){
- pid_t pid = fork();
- if(pid==-1){
- printf("\nFailed Fork");
- return ;
- }
- else if(pid==0) {
- if(execvp(parsed[0],parsed)<0){
- printf("Couldnt Execute Command");
- }
- exit(0);
- }
- else{
- wait(Null);
- }
- }
- int main()
- {
- char line[MAX]; // User Input
- char *parsedArguemnt[ARGS]; // Arguments in Command (tokens)
- int tokenCount=0;
- int status;
- int flag=1;
- struct sigaction sa;//Child Signal
- sa.sa_handler = signalHandler; // to call my signal handling functions
- sigemptyset(&sa.sa_mask); //set sa with zeros
- sigaction(SIGCHLD,&sa,NULL);//call my signal handler when a child terinates
- printf("Simple Shell");
- while(1)
- {
- printf("\n>>");
- fgets(line, MAXLINE, stdin);//Read Input from User
- tokenCount=Parse(line,parsedArguemnt);//Parsing Input
- if(strcmp(parsedArguemnt[0],"exit") == 0){ //Exit Function
- exit(0);
- }
- if(flag==1){
- ExecuteProcess(parsedArguemnt);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement