Guest User

Untitled

a guest
Apr 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7.  
  8. #define MAX_ARGS 20
  9. #define BUFSIZE 1024
  10.  
  11. int get_args(char* cmdline, char* args[])
  12. {
  13. int i = 0;
  14.  
  15. /* if no args */
  16. if((args[0] = strtok(cmdline, "nt ")) == NULL)
  17. return 0;
  18.  
  19. while((args[++i] = strtok(NULL, "nt ")) != NULL) {
  20. if(i >= MAX_ARGS) {
  21. printf("Too many arguments!n");
  22. exit(1);
  23. }
  24. }
  25. /* the last one is always NULL */
  26. return i;
  27. }
  28.  
  29. void execute(char* cmdline)
  30. {
  31. int pid, async, oneapp;
  32. char* args[MAX_ARGS];
  33. char* args2[] = {"-l", NULL};
  34. int nargs = get_args(cmdline, args);
  35. if(nargs <= 0) return;
  36.  
  37. if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) {
  38. exit(0);
  39. }
  40.  
  41. printf("before the ifn");
  42. printf("%sn",args[nargs - 2]);
  43. int i = 0;
  44. while (args[i] != ">" && i < nargs - 1) {
  45. printf("%sn",args[i]);
  46. i++;
  47. }
  48.  
  49. // Presence of ">" token in args
  50. // causes errors in execvp() because ">" is not
  51. // a built-in Unix command, so remove it from args
  52. args[i - 1] = NULL;
  53.  
  54. printf("Escaped the whilen");
  55.  
  56. // File descriptor array for the pipe
  57. int fd[2];
  58.  
  59. // PID for the forked process
  60. pid_t fpid1;
  61.  
  62. // Open the pipe
  63. pipe(fd);
  64.  
  65. // Here we fork
  66. fpid1 = fork();
  67.  
  68. if (fpid1 < 0)
  69. {
  70. // The case where the fork fails
  71. perror("Fork failed!n");
  72. exit(-1);
  73. }
  74. else if (fpid1 == 0)
  75. {
  76. //dup2(fd[1], STDOUT_FILENO);
  77. close(fd[1]);
  78. //close(fd[0]);
  79.  
  80. // File pointer for the file that'll be written to
  81. FILE * file;
  82.  
  83. // freopen() redirects stdin to args[nargs - 1],
  84. // which contains the name of the file we're writing to
  85. file = freopen(args[nargs - 1], "w+", stdin);
  86.  
  87. // If we include this line, the functionality works
  88. //execvp(args[0],args);
  89.  
  90. // We're done writing to the file, so close it
  91. fclose(file);
  92.  
  93. // We're done using the pipe, so close it (unnecessary?)
  94. //close(fd[1]);
  95. }
  96. else
  97. {
  98. // Wait for the child process to terminate
  99. wait(0);
  100. printf("This is the parentn");
  101.  
  102. // Connect write end of pipe (fd[1]) to standard output
  103. dup2(fd[1], STDOUT_FILENO);
  104.  
  105. // We don't need the read end, so close it
  106. close(fd[0]);
  107.  
  108. // args[0] contains the command "ls", which is
  109. // what we want to execute
  110. execvp(args[0], args);
  111.  
  112. // This is just a test line I was using before to check
  113. // whether anything was being written to stdout at all
  114. printf("Exec was heren");
  115. }
  116.  
  117. // This is here to make sure program execution
  118. // doesn't continue into the original code, which
  119. // currently causes errors due to incomplete functionality
  120. exit(0);
  121.  
  122. /* check if async call */
  123. printf("Async call partn");
  124. if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; }
  125. else async = 0;
  126.  
  127. pid = fork();
  128. if(pid == 0) { /* child process */
  129. execvp(args[0], args);
  130. /* return only when exec fails */
  131. perror("exec failed");
  132. exit(-1);
  133. } else if(pid > 0) { /* parent process */
  134. if(!async) waitpid(pid, NULL, 0);
  135. else printf("this is an async calln");
  136. } else { /* error occurred */
  137. perror("fork failed");
  138. exit(1);
  139. }
  140. }
  141.  
  142. int main (int argc, char* argv [])
  143. {
  144. char cmdline[BUFSIZE];
  145.  
  146. for(;;) {
  147. printf("COP4338$ ");
  148. if(fgets(cmdline, BUFSIZE, stdin) == NULL) {
  149. perror("fgets failed");
  150. exit(1);
  151. }
  152. execute(cmdline) ;
  153. }
  154. return 0;
  155. }
  156.  
  157. else if (fpid1 == 0)
  158. {
  159. printf("This is the child.n");
  160.  
  161. //dup2(fd[1], STDOUT_FILENO);
  162. close(fd[1]);
  163. //close(fd[0]);
  164.  
  165. // File pointer for the file that'll be written to
  166. FILE * file;
  167.  
  168. // freopen() redirects stdin to args[nargs - 1],
  169. // which contains the name of the file we're writing to
  170. file = freopen(args[nargs - 1], "w+", stdout);
  171.  
  172. // If we include this line, the functionality works
  173. //execvp(args[0],args);
  174.  
  175. // We're done writing to the file, so close it
  176. fclose(file);
  177.  
  178. // We're done using the pipe, so close it (unnecessary?)
  179. //close(fd[1]);
  180. }
  181. else
  182. {
  183. // Wait for the child process to terminate
  184. wait(0);
  185. printf("This is the parentn");
  186.  
  187. // Connect write end of pipe (fd[1]) to standard output
  188. dup2(fd[1], STDOUT_FILENO);
  189.  
  190. // We don't need the read end, so close it
  191. close(fd[0]);
  192.  
  193. // args[0] contains the command "ls", which is
  194. // what we want to execute
  195. execvp(args[0], args);
  196.  
  197. // This is just a test line I was using before to check
  198. // whether anything was being written to stdout at all
  199. printf("Exec was heren");
  200. }
  201.  
  202. file = freopen(args[nargs - 1], "w+", stdout)
  203.  
  204. // freopen() redirects stdin to args[nargs - 1],
  205. // which contains the name of the file we're writing to
  206. file = freopen(args[nargs - 1], "w+", stdin);
  207.  
  208. // If we include this line, the functionality works
  209. execvp(args[0],args);
Add Comment
Please, Sign In to add comment