-
Notifications
You must be signed in to change notification settings - Fork 51
Closed
Description
custom shell 을 만드는 과제를 하고 있습니다.
main을 실행해서 resch>> 를 보여주는 쉘 안으로 들어간 다음에,
유저인풋을 커맨드 입력으로 인식해 execv로 실행하려 합니다. (사실은 유저인풋을 input.log라는 파일에 출력하는 동시에 커맨드입력까지 실행하는 게 목적입니다).
그런데 20행 execv가 생각대로 작동하질 않습니다.
fork()해서 child까지 진입하는 것 까지 성공했구요,
21행에서 확인한 결과 path와 user_input 값이 제대로 입력되고 있는 것을 확인했습니다.
그런데 커맨드를 실행하려고 pwd 같은 인풋을 입력해도, 아무런 반응이 없이 다시 인풋을 넣으라고 반복 될 뿐입니다.
왜 이러는 걸까요? 제가 execv 활용법을 오해한 걸까요?
3시간정도 웹을 헤멨는데도 이해할 수 있는 자료를 찾기가 힘드네요... 잘못된 예제도 너무 많고.
도움 부탁드립니다.
1. #include <time.h>
2. #include <stdio.h>
3. #include <unistd.h>
4. #include <sys/wait.h>
5. #include <string.h>
6. #include <stdlib.h>
7.
8. void execute(char *user_input)
9. {
10. printf("Entered execute successfully\n");
11. pid_t pid;
12. int state_loc;
13. if( (pid = fork()) == -1){
14. printf("fork failed\n");
15. exit(1);
16. }
17. else if(pid == 0){
18. printf("In Child\n");
19. printf("User input is %s\n", user_input);
20. char *argv[] = {"커맨드를 실행하고 싶은 경로", user_input};
21. printf("argv[0]: %s\n",argv[0]);
22. execv(argv[0],argv);
23. }
24. else{
25. wait(&state_loc);
26. printf("the child process terminated with %d\n", state_loc);
27. }
28. }
29.
30. int main ()
31. {
32. char user_input[1024];
33. while(1)
34. {
35. //the_file = argv[1];
36. printf("recsh>> ");
37. scanf("%s", user_input);
38. if(strcmp(user_input, "exit") == 0){
39. printf("Exiting\n");
40. exit(0);
41. }
42. printf("Okay. Execute command: \n");
43. execute(user_input);
44. }
45. return 0;
46. }