Advertisement
Guest User

Untitled

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