Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <sys/time.h>
  8. #include <time.h>
  9. #include <sys/resource.h>
  10.  
  11. #define READ_BUFFER_SIZE 1024
  12. #define SPLIT_BUFFER_SIZE 100
  13. #define extras " \t\r\a\n"
  14. char *command_esp[100];
  15.  
  16. char **basic_shell_splitcommand(char *command)
  17. {
  18. int buff_size = SPLIT_BUFFER_SIZE;
  19. int pos = 0;
  20. char **words = malloc(buff_size * sizeof(char*));
  21. char *word;
  22. if(!words)
  23. {
  24. fprintf(stderr, "basic_shell: Memory allocation error\n");
  25. exit(EXIT_FAILURE);
  26. }
  27.  
  28. word = strtok(command,extras);
  29. while(word != NULL)
  30. {
  31. words[pos] = word;
  32. pos++;
  33. if(pos >=buff_size)
  34. {
  35. buff_size+= SPLIT_BUFFER_SIZE;
  36. words = realloc(words, buff_size* sizeof(char*));
  37. if(!words)
  38. {
  39. fprintf(stderr, "basic_shell: Memory allocation error!\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. }
  43.  
  44. word = strtok(NULL, extras);
  45.  
  46. }
  47. words[pos] = NULL;
  48. return words;
  49.  
  50.  
  51. }
  52.  
  53. char *basic_shell_readcommand()
  54. {
  55. int buff_size = READ_BUFFER_SIZE;
  56. char *allocate = malloc(buff_size* sizeof(char));
  57. int pos = 0;
  58. if(!allocate)
  59. {
  60. fprintf(stderr, "basic_shell: Memory allocation error!\n");
  61. exit(EXIT_FAILURE);
  62. }
  63. int n;
  64. while(1)
  65. {
  66. n = getchar();
  67. if(n == EOF || n == '\n')
  68. {
  69. allocate[pos] = '\0';
  70. return allocate;
  71. }
  72.  
  73. allocate[pos] = n;
  74. pos++;
  75.  
  76. if(pos >=buff_size)
  77. {
  78. buff_size+= READ_BUFFER_SIZE;
  79. allocate = realloc(allocate, buff_size* sizeof(char));
  80. if(!allocate)
  81. {
  82. fprintf(stderr, "basic_shell: Memory allocation error!\n");
  83. exit(EXIT_FAILURE);
  84. }
  85. }
  86. }
  87. }
  88.  
  89. int basic_shell_execute(char **words)
  90. {
  91. if(strcmp(words[0], "cd") == 0) // executes cd command
  92. {
  93. chdir(words[1]);
  94. return 1;
  95. }
  96. else if(strcmp(words[0], "exit") == 0) // executes exit command
  97. {
  98. return 0;
  99. }
  100.  
  101. else if(strcmp(words[0], "help") == 0) // executes help command
  102. {
  103. printf("basic_shell : v1.0.\nBasic shell commands will work in this shell.\n And also shell builtins like cd, exit, help and echo is valid here.\nTo know more about this commands type man command_name and press enter.\n\n\n");
  104. return 1;
  105. }
  106.  
  107. struct rusage R;
  108. struct timeval UserTime, SystemTime;
  109. struct timeval StartTime, EndTime;
  110. gettimeofday(&StartTime, NULL);
  111. int p= 0;
  112. pid_t pid;
  113. pid = fork();
  114. if(pid == 0)
  115. {
  116. printf("hello");
  117.  
  118. if(execvp(words[0], words)==-1)
  119. printf("Invalid command!\n");
  120.  
  121. exit(EXIT_FAILURE);
  122. }
  123.  
  124. if(pid <0)
  125. printf("Unexpected error occured!\n");
  126. if(pid>0)
  127. {
  128. wait(&p);
  129. getrusage(RUSAGE_CHILDREN, &R);
  130. UserTime = R.ru_utime;
  131. SystemTime = R.ru_stime;
  132. gettimeofday(&EndTime, NULL);
  133. printf("\n\tCPU time consumed by User: %ld ms\n",UserTime.tv_sec*1000+UserTime.tv_usec/1000);
  134. printf("\tCPU time consumed by System: %ld ms\n",SystemTime.tv_sec*1000+SystemTime.tv_usec/1000);
  135. printf("\tElapsed wall clock time: %ld ms\n",(EndTime.tv_sec-StartTime.tv_sec)*1000 + (EndTime.tv_usec-StartTime.tv_usec)/1000 );
  136. printf("\tThe number of times the process was preempted involuntarily: %ld\n",R.ru_nivcsw);
  137. printf("\tThe number of times the process was preempted voluntarily: %ld\n",R.ru_nvcsw);
  138. printf("\tNumber of page faults(Hard): %ld\n",R.ru_majflt);
  139. printf("\tNumber of page faults(Soft): %ld\n",R.ru_minflt);
  140. }
  141. return 1;
  142. }
  143.  
  144. void basic_shell_start(int command_given)
  145. {
  146. int flag;
  147. char *command;
  148. char **words;
  149. if(command_given)
  150. {
  151. words = command_esp;
  152. flag = basic_shell_execute(words);
  153. int i = 0;
  154. while(words[i]!=NULL)
  155. {
  156. i++;
  157. }
  158.  
  159.  
  160.  
  161. basic_shell_start(0);
  162. }
  163.  
  164. else
  165. {
  166. do
  167. {
  168. printf("-> ");
  169. command = basic_shell_readcommand();
  170.  
  171. words = basic_shell_splitcommand(command);
  172. flag = basic_shell_execute(words);
  173. int i = 0;
  174. while(words[i]!=NULL)
  175. {
  176. i++;
  177. }
  178.  
  179.  
  180. }while(flag);
  181. }
  182. }
  183.  
  184.  
  185.  
  186.  
  187. int main(int argc, char *argv[])
  188. {
  189. if(argc == 1)
  190. basic_shell_start(0);
  191. else
  192. {
  193. int i = 0, j = 0;
  194. for(i = 1; i<argc;i++)
  195. command_esp[j++] = argv[i];
  196. basic_shell_start(1);
  197. }
  198. return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement