Advertisement
Guest User

Untitled

a guest
May 4th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. char ***write_command(int row, int argc, char *argv[], char **string[]) {
  2. assert(argv);
  3. assert(row > -1);
  4. assert(argc > -0);
  5. int len = 0;
  6. for (int j = 0; j < argc; j++) {
  7. if (argv[j]) {
  8. len = len + (int) strlen(argv[j]);
  9. }
  10. }
  11. string[row] = malloc(row * argc * sizeof(char));
  12. for (int j = 0; j < argc; j++) {
  13. if (argv[j]) {
  14. string[row][j] = strdup(argv[j]);
  15. }
  16. }
  17. return string;
  18. }
  19.  
  20. ==29296== Invalid write of size 8
  21. ==29296== at 0x4030EE: write_command (main.c:129)
  22. ==29296== by 0x4030EE: runCmd (main.c:405)
  23. ==29296== by 0x4030EE: command (main.c:704)
  24. ==29296== by 0x40194E: main (main.c:803)
  25. ==29296== Address 0x5899c90 is 0 bytes after a block of size 0 alloc'd
  26. ==29296== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  27. ==29296== by 0x4030C0: write_command (main.c:126)
  28. ==29296== by 0x4030C0: runCmd (main.c:405)
  29. ==29296== by 0x4030C0: command (main.c:704)
  30. ==29296== by 0x40194E: main (main.c:803)
  31. ==29296==
  32. {ls} {|}
  33. ==29298== Invalid read of size 8
  34. ==29298== at 0x405903: fork_pipes (in /home/dac/ClionProjects/shell2/openshell/shell)
  35. ==29298== by 0x4036AC: runCmd (main.c:442)
  36. ==29298== by 0x4036AC: command (main.c:704)
  37. ==29298== by 0x40194E: main (main.c:803)
  38. ==29298== Address 0x5899c90 is 0 bytes after a block of size 0 alloc'd
  39. ==29298== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  40. ==29298== by 0x4030C0: write_command (main.c:126)
  41. ==29298== by 0x4030C0: runCmd (main.c:405)
  42. ==29298== by 0x4030C0: command (main.c:704)
  43. ==29298== by 0x40194E: main (main.c:803)
  44. ==29298==
  45. ==29298== Syscall param execve(argv) points to unaddressable byte(s)
  46. ==29298== at 0x513DCF7: execve (syscall-template.S:84)
  47. ==29298== by 0x513E50A: execvpe (execvpe.c:146)
  48. ==29298== by 0x405910: fork_pipes (in /home/dac/ClionProjects/shell2/openshell/shell)
  49. ==29298== by 0x4036AC: runCmd (main.c:442)
  50. ==29298== by 0x4036AC: command (main.c:704)
  51. ==29298== by 0x40194E: main (main.c:803)
  52. ==29298== Address 0x5899c90 is 0 bytes after a block of size 0 alloc'd
  53. ==29298== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  54. ==29298== by 0x4030C0: write_command (main.c:126)
  55. ==29298== by 0x4030C0: runCmd (main.c:405)
  56. ==29298== by 0x4030C0: command (main.c:704)
  57. ==29298== by 0x40194E: main (main.c:803)
  58. ==29298==
  59.  
  60. ## RUN_TESTS ##
  61.  
  62. #!/bin/sh
  63. echo "-- Testing our implementation of OpenShell --"
  64. echo ""
  65. echo "- If you have any problem in passing a test read the corresponding"
  66. echo "- source file to understand what the test is checking"
  67. echo ""
  68. printf "********************* PRESS ENTER TO RUN TESTS ... "
  69. #read _
  70. make
  71. valgrind --leak-check=yes ./shell .<< EOF
  72. ls -al|grep open|awk '{print $9}'
  73. EOF
  74. printf "********************* TEST WILDCARDS n***** Press any key to listing all files in current directory...nYou should see filesnames *.* below "
  75. read _
  76. ./shell << EOF
  77. ls
  78. EOF
  79. #printf "********************* TEST ALGORITHMS ... n***** Press any key to run the algorithms... .nYou should see the output from top -b -n1|head -8|tail -1 "
  80. #read _
  81. #valgrind./shell << EOF
  82. #top|head -8|tail -1|sort -n|wc -l
  83. #EOF
  84.  
  85. #printf "********************* TEST ALGORITHMS Part II. ... .nYou should see the output from who|awk '{print $4 ; print $3}'|sort -n|wc -l. "
  86. #read _
  87. #valgrind ./shell << EOF
  88. #who|awk '{print $4 ; print $3}'|sort -n|wc -l
  89. #EOF
  90.  
  91. #printf "********************* TEST CHECKENV. ..... .nYou should see the output checkenv below "
  92. #read _
  93. #valgrind ./shell << EOF
  94. #checkenv
  95. #EOF
  96. #printf "********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ... "
  97. #read _
  98.  
  99. struct command {
  100. char *const *argv;
  101. };
  102.  
  103. /* Helper function that forks pipes */
  104. void fork_pipes(int n, struct command *cmd) {
  105. int i;
  106. int in = 0;
  107. int fd[2];
  108.  
  109. for (i = 0; i < n - 1; ++i) {
  110.  
  111. if (pipe(fd) == -1) {
  112. err_syserr("Failed creating pipe");
  113. }
  114.  
  115. spawn_proc(in, fd[1], cmd + i);
  116. close(fd[1]);
  117. in = fd[0];
  118. }
  119. if (dup2(in, 0) < 0) {
  120. err_syserr("dup2() failed on stdin for %s: ", cmd[i].argv[0]);
  121. }
  122. /*fprintf(stderr, "%d: executing %sn", (int) getpid(), cmd[i].argv[0]);*/
  123. fprintf(stderr, "n");
  124. execvp(cmd[i].argv[0], cmd[i].argv);
  125. err_syserr("failed to execute %s: ", cmd[i].argv[0]);
  126. }
  127.  
  128. /* Helper function that spawns processes */
  129. int spawn_proc(int in, int out, struct command *cmd) {
  130. pid_t pid;
  131. fflush(NULL);
  132. pid = fork();
  133. if (pid == 0) {
  134. if (in != 0) {
  135. if (dup2(in, 0) < 0)
  136. err_syserr("dup2() failed on stdin for %s: ", cmd->argv[0]);
  137. close(in);
  138. }
  139. if (out != 1) {
  140. if (dup2(out, 1) < 0)
  141. err_syserr("dup2() failed on stdout for %s: ", cmd->argv[0]);
  142. close(out);
  143. }
  144. /*fprintf(stderr, "%d: executing %sn", (int) getpid(), cmd->argv[0]);*/
  145. fprintf(stderr, "[%d]n", (int) getpid());
  146. execvp(cmd->argv[0], cmd->argv);
  147. err_syserr("failed to execute %s: ", cmd->argv[0]);
  148. }
  149. else if (pid < 0) {
  150. err_syserr("fork failed: ");
  151. } else {
  152. /* printf("** we are the parent ***"); */
  153. }
  154. return pid;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement