Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <errno.h>
  7. extern int errno;
  8.  
  9. #ifndef MAX_PATH
  10. #define MAX_PATH 100
  11. #endif
  12.  
  13. #ifndef HISTORY_SIZE
  14. #define HISTORY_SIZE 10
  15. #endif
  16.  
  17. #ifndef ARGS_SIZE
  18. #define ARGS_SIZE 20
  19. #endif
  20.  
  21.  
  22. struct job {
  23. int pid;
  24. int pos;
  25. char** args;
  26. int status;
  27. struct job* next;
  28. };
  29.  
  30. struct history_item {
  31. int bg;
  32. char** args;
  33. };
  34.  
  35. struct job* jobs_head = NULL;
  36.  
  37. void print_jobs() {
  38. struct job* point = jobs_head;
  39.  
  40. if (jobs_head == NULL) {
  41. printf("in it!\n");
  42. return;
  43. }
  44.  
  45. int count = 0;
  46. do {
  47. // int the_pid = point->pid;
  48. if (point == NULL) { printf("something is weird"); }
  49.  
  50. printf("PID %d, id: %d, command: ", point->pos, point->pid);
  51.  
  52. count++;
  53.  
  54. for (int i = 0; i < 2; i++) {
  55. char* arg = point->args[i];
  56. if (arg == NULL) { break; }
  57.  
  58. printf("%s ", arg);
  59. }
  60.  
  61. printf("\n");
  62. point = point->next;
  63. } while(point != NULL && count < 2);
  64. }
  65.  
  66. void insert_job(int pid, char* args[20]) {
  67. struct job* point = jobs_head;
  68. int which_index = 0;
  69.  
  70. /*
  71. struct job new_job = {
  72. .pid = pid,
  73. .args = args,
  74. .status = 0,
  75. .pos = 0,
  76. .next = NULL,
  77. };
  78. */
  79. struct job * new_job = (struct job *)malloc(sizeof(struct job));
  80.  
  81. new_job->pid = pid;
  82. new_job->args = (char **) malloc(ARGS_SIZE*sizeof(char*));
  83. memcpy(new_job->args, args, ARGS_SIZE*sizeof(char*));
  84. new_job->status = 0;
  85. new_job->pos = 0;
  86. new_job->next = NULL;
  87.  
  88. do {
  89. if (jobs_head == NULL) {
  90. jobs_head = new_job;
  91.  
  92. break;
  93. }
  94.  
  95. if (point->next == NULL || point->next->pos != which_index+1) {
  96. new_job->next = point->next;
  97. point->next = new_job;
  98. new_job->pos = which_index+1;
  99. break;
  100. }
  101.  
  102. point = point->next;
  103. which_index++;
  104. } while (point != NULL);
  105. }
  106.  
  107. int getcmd(char *prompt, char *args[], int *background)
  108. {
  109. int length, i = 0;
  110. char *token, *loc;
  111. char *line = NULL;
  112. size_t linecap = 0;
  113.  
  114. printf("%s", prompt);
  115. length = getline(&line, &linecap, stdin);
  116. printf("%d\n", length);
  117. printf("errno: %d\n", errno);
  118.  
  119. if (length <= 0) {
  120. exit(-1);
  121. }
  122.  
  123. // Check if background is specified..
  124. if ((loc = index(line, '&')) != NULL) {
  125. *background = 1;
  126. *loc = ' ';
  127. } else
  128. *background = 0;
  129.  
  130. while ((token = strsep(&line, " \t\n")) != NULL) {
  131. for (unsigned int j = 0; j < strlen(token); j++)
  132. if (token[j] <= 32)
  133. token[j] = '\0';
  134. if (strlen(token) > 0)
  135. args[i++] = token;
  136. }
  137.  
  138. return i;
  139. }
  140.  
  141.  
  142. int history_count = 0;
  143. struct history_item ** shell_command_history;
  144.  
  145. void save_to_history(char** args) {
  146. struct history_item * his_item = (struct history_item *)
  147. malloc(sizeof(struct history_item));
  148.  
  149. his_item->args = args;
  150.  
  151. if (history_count > 9) {
  152. free(shell_command_history[history_count%HISTORY_SIZE]);
  153. }
  154.  
  155. shell_command_history[history_count%HISTORY_SIZE] = his_item;
  156.  
  157. history_count++;
  158. }
  159.  
  160. char curdir[MAX_PATH+1];
  161.  
  162. void show_history() {
  163. int first_history_index = history_count-HISTORY_SIZE;
  164. if (first_history_index < 0) {
  165. first_history_index = 0;
  166. }
  167.  
  168. for (int i = first_history_index; i < history_count; i++) {
  169. printf("%d ", i+1);
  170.  
  171. for (int j = 0; j < ARGS_SIZE; j++) {
  172. char * arg;
  173. if ( (arg = shell_command_history[i%HISTORY_SIZE]->args[j]) == NULL) {
  174. break;
  175. } else {
  176. printf("%s ", arg);
  177. }
  178. }
  179.  
  180. printf("\n");
  181. }
  182. }
  183.  
  184. int main()
  185. {
  186. int bg;
  187. char *args[ARGS_SIZE];
  188. int cnt;
  189.  
  190. char buf[MAX_PATH + 1];
  191. memcpy(curdir, getcwd(buf, MAX_PATH + 1), MAX_PATH + 1);
  192.  
  193. // free(shell_command_history);
  194. shell_command_history = (struct history_item **) malloc(HISTORY_SIZE*sizeof(struct history_item *));
  195. for(int i = 0; i < HISTORY_SIZE; i++) {
  196. shell_command_history[i] = (struct history_item *) malloc(sizeof(struct history_item));
  197. }
  198.  
  199. while(1) {
  200. bg = 0;
  201. cnt = getcmd("\n>> ", args, &bg);
  202. args[cnt] = NULL;
  203.  
  204. for (int i = 0; i < cnt; i++)
  205. printf("\nArg[%d] = %s", i, args[i]);
  206.  
  207. printf("\n");
  208.  
  209. int history_element_to_execute;
  210. if ( (history_element_to_execute = atoi(args[0])) > 0) {
  211. printf("history_element_to_execute: %d\n", history_element_to_execute);
  212. memcpy(args, shell_command_history[history_element_to_execute-1], ARGS_SIZE);
  213. }
  214.  
  215. save_to_history(args);
  216.  
  217. if (strcmp(args[0],"history") == 0) {
  218. if (args[1] != NULL) {
  219. printf("history does not require any arguments.\n");
  220. exit(-1);
  221. }
  222.  
  223. show_history();
  224.  
  225. continue;
  226. }
  227.  
  228. if (strcmp(args[0], "cd") == 0) {
  229. char* new_dir = NULL;
  230. strcat(new_dir, curdir);
  231. if (args[1][0] == '/') {
  232. memcpy(new_dir, args[1], strlen(args[1]));
  233. }
  234. strcat(new_dir, args[1]);
  235.  
  236. chdir(new_dir);
  237.  
  238. continue;
  239. }
  240.  
  241. if (strcmp(args[0], "pwd") == 0) {
  242. char* cwd;
  243. cwd = getcwd(buf, MAX_PATH + 1);
  244. printf("%s", cwd);
  245.  
  246. continue;
  247. }
  248.  
  249. if (strcmp(args[0], "exit") == 0) {
  250. exit(0);
  251. }
  252.  
  253. if (strcmp(args[0], "jobs") == 0) {
  254. print_jobs();
  255.  
  256. continue;
  257. }
  258.  
  259. if (strcmp(args[0], "fg") == 0) {
  260. // waitpid();
  261. }
  262.  
  263. // char * file = malloc(sizeof);
  264. int child_pid = 0;
  265. if((child_pid = fork()) == 0) {
  266. int result = execvp(args[0], args);
  267. if (result < 0) {
  268. printf("We got the errno %d", errno);
  269. }
  270. } else {
  271. // continue execution
  272. }
  273.  
  274. if (bg) {
  275. printf("\nBackground enabled..\n");
  276. insert_job(child_pid, args);
  277. } else {
  278. printf("\nBackground not enabled \n");
  279. int status = 0;
  280. waitpid(child_pid, &status, 0);
  281. }
  282.  
  283.  
  284. printf("\n\n");
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement