Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <pthread.h>
  4. #include <string.h>
  5.  
  6. void parse(char *line, char **argv)
  7. {
  8. while (*line != '') // If not the end of line...
  9. {
  10. while (*line == ' ' || *line == 't' || *line == 'n')
  11. {
  12. // Replace white spaces with 0.
  13. *line++ = '';
  14. }
  15.  
  16. // Save the argument position.
  17. *argv++ = line;
  18.  
  19. while ((*line != '') && (*line != ' ') &&
  20. (*line != 't') && (*line != 'n'))
  21. {
  22. // Skip the argument until...
  23. line++;
  24. }
  25. }
  26.  
  27. // Mark the end of argument list.
  28. *argv = '';
  29. }
  30.  
  31. void execute(char **argv)
  32. {
  33. pid_t pid;
  34. int status;
  35.  
  36. // Fork a child process.
  37. if ((pid = fork()) < 0)
  38. {
  39. printf("*** ERROR: forking child process failedn");
  40. exit(1);
  41. }
  42. else if (pid == 0) // For the child process:
  43. {
  44. // Execute the command.
  45. if (execvp(*argv, argv) < 0)
  46. {
  47. printf("*** ERROR: exec failedn");
  48. exit(1);
  49. }
  50. }
  51. else // For the parent:
  52. {
  53. // Wait for completion.
  54. while (wait(&status) != pid);
  55.  
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. char line[1024]; /* the input line */
  62. char *argv[64]; /* the command line argument */
  63.  
  64. // Repeat until done...
  65. while (1)
  66. {
  67. printf("Shell -> "); /* display a prompt */
  68. gets(line); /* read in the command line */
  69. printf("n");
  70.  
  71. parse(line, argv); /* parse the line */
  72.  
  73. // Is it an "exit"?
  74. if (strcmp(argv[0], "exit") == 0)
  75. exit(0); /* exit if it is */
  76.  
  77. execute(argv); /* otherwise, execute the command */
  78. }
  79. }
  80.  
  81. g++ -pthread test.cpp
  82. test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  83. *argv = ''; /* mark the end of argument list */
  84. ^~~~
  85. test.cpp:24:14: error: use of undeclared identifier 'fork'
  86. if ((pid = fork()) < 0) { /* fork a child process */
  87. ^
  88. test.cpp:26:5: error: use of undeclared identifier 'exit'
  89. exit(1);
  90. ^
  91. test.cpp:29:9: error: use of undeclared identifier 'execvp'
  92. if (execvp(*argv, argv) < 0) { /* execute the command */
  93. ^
  94. test.cpp:31:7: error: use of undeclared identifier 'exit'
  95. exit(1);
  96. ^
  97. test.cpp:35:12: error: use of undeclared identifier 'wait'
  98. while (wait(&status) != pid) /* wait for completion */
  99. ^
  100. test.cpp:51:7: error: use of undeclared identifier 'exit'
  101. exit(0); /* exit if it is */
  102. ^
  103. 1 warning and 6 errors generated.
  104. Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ g++ -lpthread test.cpp
  105. test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  106. *argv = ''; /* mark the end of argument list */
  107. ^~~~
  108. test.cpp:24:14: error: use of undeclared identifier 'fork'
  109. if ((pid = fork()) < 0) { /* fork a child process */
  110. ^
  111. test.cpp:26:5: error: use of undeclared identifier 'exit'
  112. exit(1);
  113. ^
  114. test.cpp:29:9: error: use of undeclared identifier 'execvp'
  115. if (execvp(*argv, argv) < 0) { /* execute the command */
  116. ^
  117. test.cpp:31:7: error: use of undeclared identifier 'exit'
  118. exit(1);
  119. ^
  120. test.cpp:35:12: error: use of undeclared identifier 'wait'
  121. while (wait(&status) != pid) /* wait for completion */
  122. ^
  123. test.cpp:51:7: error: use of undeclared identifier 'exit'
  124. exit(0); /* exit if it is */
  125. ^
  126. 1 warning and 6 errors generated.
  127. Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ clang -pthread -c test.cpp
  128. test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  129. *argv = ''; /* mark the end of argument list */
  130. ^~~~
  131. test.cpp:24:14: error: use of undeclared identifier 'fork'
  132. if ((pid = fork()) < 0) { /* fork a child process */
  133. ^
  134. test.cpp:26:5: error: use of undeclared identifier 'exit'
  135. exit(1);
  136. ^
  137. test.cpp:29:9: error: use of undeclared identifier 'execvp'
  138. if (execvp(*argv, argv) < 0) { /* execute the command */
  139. ^
  140. test.cpp:31:7: error: use of undeclared identifier 'exit'
  141. exit(1);
  142. ^
  143. test.cpp:35:12: error: use of undeclared identifier 'wait'
  144. while (wait(&status) != pid) /* wait for completion */
  145. ^
  146. test.cpp:51:7: error: use of undeclared identifier 'exit'
  147. exit(0); /* exit if it is */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement