Advertisement
Guest User

casestuddy1

a guest
Dec 13th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<sys/wait.h>
  6. #include<unistd.h>
  7. #include<errno.h>
  8.  
  9. #define MAX_LINE 4096
  10. #define MAX_WORDS MAX_LINE/2
  11. /* a line can have at most MAX_LINE/2 words, why? */
  12. /* a word is at minimum 2 characters, so the most that can fit in a line is thus MAX/2? */
  13.  
  14. void tokenize(char *line, char **words, int *nwords);
  15. /* break line into words separated by whitespace, placing them in the
  16. array words, and setting the count to nwords */
  17.  
  18. int system(const char *command);
  19. int strcmp(const char *str1, const char *str2);
  20. int externalCommands(const char *command);
  21.  
  22. int main()
  23. {
  24. char line[MAX_LINE], *words[MAX_WORDS], message[MAX_LINE];
  25. char linecopy[MAX_LINE];
  26. int stop=0,nwords=0;
  27.  
  28. while(stop==0)
  29. {
  30. int i; /* for the for loops */
  31.  
  32. printf("OSP CLI $ "); /*this provides the look of a terminal*/
  33.  
  34. fgets (line, sizeof(line), stdin); /* fetches user input for a line */
  35.  
  36. /* read a line of text here */
  37.  
  38. if (strcmp(line, "")!=0) {
  39. strcpy(linecopy, line); /* copies the line to linecopy, since tokenizing the line changes it and I'm not sure if we need to keep the original line */
  40. tokenize(linecopy,words,&nwords); /*tokenizes the line, turning it from a long string into several smaller strings */
  41.  
  42. printf("\n input: %s" ,line); /* prints the line, may be removed later if deemed unnecessary */
  43. } else {
  44. break;
  45. }
  46.  
  47. for(i=0; i<nwords; i++) { /* this loop prints the words separately from one another (showing they have been token'd) and also shows the counter */
  48. printf("word %i = %s \n", i+1, words[i]);
  49. }
  50.  
  51. /* do builtin functions here */
  52.  
  53. printf("first word is %s \n", words[0]);
  54.  
  55. int isExit;
  56. int isCd;
  57.  
  58. isExit = strcmp(words[0], "exit");
  59. isCd = strcmp(words[0], "cd");
  60.  
  61. if(isExit ==0) { /* If the user input reads as "exit" then sends a message saying so and terminate the program */
  62. printf("Exiting program now... \n");
  63. stop=1; /* setting stop to 1 breaks the while loop */
  64. break;
  65. }
  66.  
  67. if(isCd==0) { /*This code is if the entered command is "cd", it changes the directory */
  68. int ret;
  69. printf("Changing Directory to %s \n", words[1]);
  70. ret = chdir(words[1]);
  71.  
  72. switch (ret) {
  73. case 0:
  74. printf("Path successfully changed! \n");
  75. break;
  76.  
  77. case -1:
  78. printf("Errors in changing directory! \n");
  79. break;
  80. }
  81.  
  82. }
  83.  
  84. /* do external functions with Fork and such */
  85.  
  86. /* run an externalCommands(words) */
  87.  
  88. if (isExit!=0 && isCd !=0) {
  89. externalCommands(words[0]);
  90. }
  91. }
  92. return 0;
  93. }
  94.  
  95. /* this function works, it is up to you to work out why! */
  96. void tokenize(char *line, char **words, int *nwords)
  97. {
  98. *nwords=1;
  99.  
  100. for(words[0]=strtok(line," \t\n");
  101. /*Below: if the number of words is below max, and the value at words[nwords] isn't null\n, tokenize the word then add 1 to nwords*/
  102. /*the function strtok breaks a line into words using a delimiter, */
  103. (*nwords<MAX_WORDS)&&(words[*nwords]=strtok(NULL, " \t\n"));
  104. *nwords=*nwords+1
  105. ); /* empty body */
  106. return;
  107. }
  108.  
  109. int externalCommands(const char *command) {
  110. pid_t child;
  111. switch(child=fork())
  112. {
  113. case -1:
  114. perror("fork");
  115. exit(1);
  116. break;
  117. case 0:
  118. printf("I am the child my pid is: %d\n",getpid());
  119.  
  120. exit(0);
  121. break;
  122. default:
  123. printf("I am the parent, my pid is %d," ,getpid());
  124. printf("and my childs is %d\n",child);
  125. break;
  126. }
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement