Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6.  
  7. typedef struct {
  8. char *prog;
  9. char **args;
  10. } COMMAND;
  11.  
  12. COMMAND parseInput(char* input);
  13. char** splitter(char *token, int *count);
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.  
  18. while (1) {
  19.  
  20. char input[512]; //store input line
  21. char inputcpy[512];
  22. printf("$sshell "); //shell prompt
  23. fgets(input, 512, stdin); //read line into string
  24. input[strlen(input) - 1] = '\0';
  25. strcpy(inputcpy, input);
  26.  
  27. COMMAND comm = parseInput(input);
  28.  
  29. printf("INPUT: %s\n", inputcpy);
  30.  
  31. //check for exit command
  32. if (strcmp(comm.prog, "exit") == 0) {
  33. printf("Bye...\n");
  34. exit(0);
  35.  
  36. }
  37.  
  38. if (strcmp(comm.prog, "pwd") == 0) {
  39. char cwd[512];
  40. getcwd(cwd, sizeof(cwd));
  41. printf("%s\n", cwd);
  42. }
  43.  
  44. if (strcmp(comm.prog, "cd") == 0) {
  45. if (chdir(comm.args[1]) == -1) {
  46. fprintf(stderr, "Error: no such directory.\n");
  47. }
  48. }
  49.  
  50. else {
  51.  
  52. //create new process
  53. pid_t PID = fork();
  54.  
  55. //if child process
  56. if (PID == 0) {
  57.  
  58. int status;
  59.  
  60. //printf("Exec call.\n");
  61. status = execvp(comm.prog, comm.args);
  62.  
  63. //if exec fails
  64. if (status != 0) {
  65.  
  66. fprintf(stderr, "Error: command not found.\n");
  67. exit(42);
  68.  
  69. //else exit the process
  70. }
  71. else {
  72.  
  73. exit(0);
  74.  
  75. }
  76.  
  77. }
  78. else {
  79.  
  80. int status;
  81. wait(&status);
  82. fprintf(stderr, "+ completed '%s' [%d]\n", input, WEXITSTATUS(status));
  83.  
  84. }
  85. }
  86. }
  87. return EXIT_SUCCESS;
  88. }
  89.  
  90. COMMAND parseInput(char *input) {
  91.  
  92. COMMAND comm;
  93. int i = 1;
  94. char *token;
  95.  
  96. token = strtok(input, " ");
  97. //printf("Got a token: %s\n", token);
  98.  
  99. comm.prog = (char*)malloc(strlen(token) + 1);
  100. strcpy(comm.prog, token);
  101.  
  102. comm.args = (char**)malloc(sizeof(char*) * 512);
  103.  
  104. comm.args[0] = (char*)malloc(strlen(token) + 1);
  105. strcpy(comm.args[0], token);
  106.  
  107. while (1) {
  108. token = strtok(NULL, " \n");
  109.  
  110. if (token == NULL) {
  111. break;
  112. }
  113.  
  114. if (strpbrk(token, "<>|") != NULL) {
  115.  
  116. int count = 0;
  117. char **split = splitter(token, &count);
  118. int j = 0;
  119.  
  120. for (j; j < count; j++) {
  121. comm.args[i + j] = malloc(sizeof(split[j] + 1);
  122. strcpy(comm.args[i + j], split[j]);
  123. }
  124.  
  125. i = i + j;
  126.  
  127. }
  128. else {
  129.  
  130. comm.args[i] = (char*)malloc(strlen(token) + 1);
  131. strcpy(comm.args[i], token);
  132. i++;
  133. }
  134. }
  135. comm.args[i + 1] = (char*)malloc(sizeof((char*)NULL));
  136. comm.args[i + 1] = (char*)NULL;
  137.  
  138. return comm;
  139. }
  140.  
  141. char** splitter(char *token, int *count) {
  142.  
  143. char **tokens = malloc(sizeof(char*) * 512);
  144. char *temp;
  145. int i = 0;
  146. int j = 0;
  147.  
  148.  
  149. while (token[i] != '\0') {
  150.  
  151. if (token[i] == '<' || token[i] == '>' || token[i] == '|') {
  152.  
  153. tokens[*count] = (char*)malloc(strlen(temp) + 1);
  154. strcpy(tokens[*count], temp);
  155. ++*count;
  156.  
  157. if (token[i] == '<') {
  158. tokens[*count] = (char*)malloc(strlen("<") + 1);
  159. strcpy(tokens[*count], "<");
  160. }
  161. else if (token[i] == '>') {
  162. tokens[*count] = (char*)malloc(strlen(">") + 1);
  163. strcpy(tokens[*count], ">");
  164. }
  165. else if (token[i] == '|') {
  166. tokens[*count] = (char*)malloc(strlen("|") + 1);
  167. strcpy(tokens[*count], "|");
  168. }
  169.  
  170. ++*count;
  171. temp[0] = 0; //will hopefully clear the string
  172. j = 0;
  173. i++;
  174. }
  175. else {
  176. temp[j] = (char)malloc(sizeof(char));
  177. temp[j] = token[i];
  178. i++;
  179. j++;
  180. }
  181. }
  182.  
  183. while (*tokens) {
  184. printf("%s ", *tokens);
  185. printf("\n");
  186. ++tokens;
  187.  
  188. }
  189.  
  190. return tokens;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement