Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<sys/types.h>
  6. #include<sys/wait.h>
  7. #include<signal.h>
  8. #define MAXL 500 //Max Input
  9. #define ARGS 20 //Max number of arguments
  10. //handle the signal SIGCHLD when a child process is terminated
  11. void signalHandler(int sig_num)
  12. {
  13. FILE *fp;
  14. fp=fopen("shell_logfile.log","a+");// Open log file
  15. if(fp == NULL)
  16. {
  17. printf("\nerror:couldn't open file");
  18. }
  19. else fprintf(fp,"Child process was terminated\n");
  20. fclose(fp);
  21.  
  22. }
  23.  
  24.  
  25. void Execute(char * args[], int args_count) //Executes Command
  26. {
  27. int count=args_count;
  28. char cwd[1024];
  29. // status of the child
  30. int status;
  31. int background=0;
  32.  
  33. //check if it's a background process or not
  34. if(!(strcmp(args[count-1],"&")))
  35. {
  36. //SET background flag to zero
  37. background=1;
  38. //remove the "&" so it could be executed
  39. args[count-1]='\0';
  40. count--;
  41. }
  42.  
  43. // print the current directory
  44. if (strcmp(args[0],"pwd") == 0)
  45. {
  46. printf("%s\n", getcwd(cwd, sizeof(cwd)));
  47. }
  48. // change directory
  49. else if (strcmp(args[0],"cd") == 0)
  50. {
  51. changeDirectory(args);
  52. }
  53. //execute the command
  54. else
  55. {
  56. //process id
  57. pid_t pid=fork();
  58.  
  59. // no error in forking
  60. if (pid >= 0)
  61. {
  62. // if it's a child process
  63. if(pid== 0)
  64. {
  65. execvp(args[0],args);
  66.  
  67. }
  68. //if it's the parent process
  69. else
  70. {
  71. if(background==1) return;
  72. else
  73. {
  74. //suspends execution until the child specified by pid change state
  75. do
  76. {
  77. waitpid(pid,&status,WUNTRACED);
  78. }
  79. while(!WIFEXITED(status) && !WIFSIGNALED(status));
  80. //wait(&status);
  81. }
  82. }
  83. }
  84. else
  85. printf("\nfailed to fork!");
  86. }
  87. }
  88.  
  89. int Parse(char line[], char *parsedArguments[]) //Parsing
  90. {
  91. int i=0;
  92. if((parsedArguments[i] = strtok(line," "))==NULL){
  93. printf("Please enter Command ");
  94. return 0;
  95. }
  96. int counter=1;
  97. while((parsedArguments[counter] = strtok(NULL, " ")) != NULL){ //Parse Using Spaces
  98. i++;
  99. }
  100. parsedArguments[i]= '\0';
  101. return i;
  102. }
  103. int ExecuteProcess(char *parsedArguments[]){
  104. pid_t pid = fork();
  105. if(pid==-1){
  106. printf("\nFailed Fork");
  107. return ;
  108. }
  109. else if(pid==0) {
  110. if(execvp(parsed[0],parsed)<0){
  111. printf("Couldnt Execute Command");
  112. }
  113. exit(0);
  114. }
  115. else{
  116. wait(Null);
  117. }
  118. }
  119. int main()
  120. {
  121. char line[MAX]; // User Input
  122. char *parsedArguemnt[ARGS]; // Arguments in Command (tokens)
  123. int tokenCount=0;
  124. int status;
  125. int flag=1;
  126.  
  127. struct sigaction sa;//Child Signal
  128.  
  129. sa.sa_handler = signalHandler; // to call my signal handling functions
  130. sigemptyset(&sa.sa_mask); //set sa with zeros
  131. sigaction(SIGCHLD,&sa,NULL);//call my signal handler when a child terinates
  132. printf("Simple Shell");
  133. while(1)
  134. {
  135. printf("\n>>");
  136. fgets(line, MAXLINE, stdin);//Read Input from User
  137. tokenCount=Parse(line,parsedArguemnt);//Parsing Input
  138. if(strcmp(parsedArguemnt[0],"exit") == 0){ //Exit Function
  139. exit(0);
  140. }
  141. if(flag==1){
  142. ExecuteProcess(parsedArguemnt);
  143. }
  144. }
  145.  
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement