Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 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. // parseInput splits command line input based on spaces and calls
  13. // splitter to divide input that didn't have spaces
  14. COMMAND parseInput(char* input);
  15. char** splitter(char *token, int *count);
  16.  
  17. // finds position of left carrot within the arguments array
  18. // returns -1 if not found
  19. int whereLeftCarrot(char **args);
  20.  
  21. // finds position of right carrot within the arguments array
  22. // returns -1 if not found
  23. int whereRightCarrot(char **args);
  24.  
  25. // removes special symbol from comm.args by taking in the pos
  26. // of the special char and shifting from that spot over
  27. void shiftArgs(COMMAND *comm, int pos);
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.  
  32. while (1) {
  33.  
  34. char input[512]; // store input line
  35. char inputcpy[512];
  36. printf("$sshell "); // shell prompt
  37. fgets(input, 512, stdin); // read line into string
  38. input[strlen(input) - 1] = '\0';
  39. strcpy(inputcpy, input);
  40.  
  41. COMMAND comm = parseInput(input);
  42.  
  43. //printf("INPUT: %s\n", inputcpy);
  44.  
  45. //check if input is empty or whitespace
  46. if (comm.prog == NULL) {
  47. continue;
  48. }
  49.  
  50. // check for exit command
  51. if (strcmp(comm.prog, "exit") == 0) {
  52. printf("Bye...\n");
  53. exit(0);
  54.  
  55. } else
  56.  
  57. if (strcmp(comm.prog, "pwd") == 0) {
  58. char cwd[512];
  59. getcwd(cwd, sizeof(cwd));
  60. printf("%s\n", cwd);
  61. } else
  62.  
  63. if (strcmp(comm.prog, "cd") == 0) {
  64. if (chdir(comm.args[1]) == -1) {
  65. fprintf(stderr, "Error: no such directory.\n");
  66. }
  67. }
  68.  
  69. else {
  70.  
  71.  
  72. // create new process
  73. pid_t PID = fork();
  74.  
  75. // if child process
  76. if (PID == 0) {
  77.  
  78. int status;
  79.  
  80. // printf("Exec call.\n");
  81. status = execvp(comm.prog, comm.args);
  82. printf("STATUS: %d", status);
  83. // if exec fails
  84. if (status != 0) {
  85.  
  86. fprintf(stderr, "Error: command not found.\n");
  87. exit(42);
  88.  
  89. // else exit the process
  90. }
  91. else {
  92.  
  93. exit(0);
  94.  
  95. }
  96.  
  97. }
  98. else {
  99.  
  100. int status;
  101. wait(&status);
  102. fprintf(stderr, "+ completed '%s' [%d]\n", inputcpy, WEXITSTATUS(status));
  103.  
  104. }
  105. }
  106. }
  107. return EXIT_SUCCESS;
  108. }
  109.  
  110. COMMAND parseInput(char *input) {
  111.  
  112. COMMAND comm;
  113. int i = 1;
  114. char *token;
  115.  
  116. token = strtok(input, " \n");
  117. // printf("Got a token: %s\n", token);
  118.  
  119. // is empty or whitespace
  120. if (token == NULL) {
  121. comm.prog = malloc(sizeof(NULL));
  122. comm.prog = NULL;
  123. return comm;
  124. }
  125.  
  126. comm.prog = (char*)malloc(strlen(token) + 1);
  127. strcpy(comm.prog, token);
  128.  
  129. comm.args = (char**)malloc(sizeof(char*) * 512);
  130.  
  131. comm.args[0] = (char*)malloc(strlen(token) + 1);
  132. strcpy(comm.args[0], token);
  133.  
  134. while (1) {
  135. token = strtok(NULL, " \n");
  136.  
  137. if (token == NULL) {
  138. break;
  139. }
  140.  
  141. if (strpbrk(token, "<>|") != NULL) {
  142.  
  143. int count = 0;
  144. char **split = splitter(token, &count);
  145. int j;
  146.  
  147. for (j = 0; j < count; j++) {
  148. comm.args[i + j] = malloc(sizeof(split[j] + 1));
  149. strcpy(comm.args[i + j], split[j]);
  150. }
  151.  
  152. i = i + j;
  153.  
  154. }
  155. else {
  156.  
  157. comm.args[i] = (char*)malloc(strlen(token) + 1);
  158. strcpy(comm.args[i], token);
  159. i++;
  160. }
  161. }
  162. comm.args[i + 1] = (char*)malloc(sizeof((char*) NULL));
  163. comm.args[i + 1] = (char*) NULL;
  164.  
  165. /*
  166. for(int g = 0; g < i; g++) {
  167. printf("ARGS: %s\n", comm.args[g]);
  168. }
  169. */
  170.  
  171. return comm;
  172. }
  173.  
  174. char** splitter(char *token, int *count) {
  175.  
  176. char **tokens = malloc(sizeof(char*) * 512);
  177. char temp[512];
  178. int i = 0;
  179. int j = 0;
  180.  
  181.  
  182. while (token[i] != '\0') {
  183.  
  184. if (token[i] == '<' || token[i] == '>' || token[i] == '|') {
  185.  
  186. temp[j] = '\0';
  187.  
  188. tokens[*count] = (char*)malloc(strlen(temp) + 1);
  189. strcpy(tokens[*count], temp);
  190. ++*count;
  191.  
  192. if (token[i] == '<') {
  193. tokens[*count] = (char*)malloc(strlen("<") + 1);
  194. strcpy(tokens[*count], "<");
  195. }
  196. else if (token[i] == '>') {
  197. tokens[*count] = (char*)malloc(strlen(">") + 1);
  198. strcpy(tokens[*count], ">");
  199. }
  200. else if (token[i] == '|') {
  201. tokens[*count] = (char*)malloc(strlen("|") + 1);
  202. strcpy(tokens[*count], "|");
  203. }
  204.  
  205. ++*count;
  206. j = 0;
  207. i++;
  208.  
  209. } else {
  210.  
  211. temp[j] = token[i];
  212. i++;
  213. j++;
  214. }
  215. }
  216.  
  217. tokens[*count] = malloc(strlen(temp) + 1);
  218. strcpy(tokens[*count], temp);
  219. ++*count;
  220.  
  221. // printf("# of tokens: %d\n", *count);
  222.  
  223. /*
  224. for(int g = 0; g < *count; g++) {
  225. printf("Token Stored: %s\n", tokens[g]);
  226. }
  227. */
  228.  
  229. return tokens;
  230. }
  231.  
  232. int whereLeftCarrot(char **args) {
  233.  
  234. int pos = 0;
  235.  
  236. while(strcmp(args[pos], "NULL") != 0) {
  237. if (strcmp(args[pos], "<") == 0) {
  238. return pos;
  239. }
  240. }
  241. return -1;
  242. }
  243.  
  244. int whereRightCarrot(char **args) {
  245.  
  246. int pos = 0;
  247.  
  248. while(strcmp(args[pos], "NULL") != 0) {
  249. if (strcmp(args[pos], ">") == 0) {
  250. return pos;
  251. }
  252. }
  253. return -1;
  254. }
  255.  
  256. void shiftArgs(COMMAND *comm, int pos) {
  257.  
  258.  
  259. while ((*comm).args[pos + 1] != NULL) {
  260. (*comm).args[pos] = realloc((*comm).args[pos], sizeof((*comm).args[pos + 1]));
  261. strcpy((*comm).args[pos], (*comm).args[pos + 1]);
  262. pos++;
  263. }
  264.  
  265. (*comm).args[pos] = malloc(sizeof( NULL ));
  266. (*comm).args[pos] = NULL;
  267.  
  268. free((*comm).args[pos + 1]);
  269.  
  270. return;
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement