Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. //pipe
  8. #include <sys/types.h>
  9.  
  10. #include <fcntl.h>
  11. //signal
  12. #include <signal.h>
  13.  
  14. int main(){
  15. char str[1024];//입력받은 문자열
  16. char *text1=NULL;
  17. char *text2=NULL;
  18. char *command1[4];//앞쪽명령어
  19. char *command2[5];//뒤쪽명령어
  20. int i ;
  21. char *pch;//문장나눌때 임시 저장
  22. //pipe
  23. int fd[2]; // 파이프 처리
  24. int fdr;// '>', '<'
  25. pid_t pid;
  26.  
  27. //signal
  28.  
  29. sigset_t blockset;// 막아놓을 시그널 목록
  30. sigemptyset(&blockset);
  31. sigaddset(&blockset, SIGINT);//목록에 해당 ^C시그널 추가
  32. sigaddset(&blockset, SIGQUIT);//^\시그널 추가
  33. sigprocmask(SIG_BLOCK, &blockset, NULL);
  34.  
  35. while(1){
  36.  
  37. for(i=0 ; i < 1024 ; i++){
  38. str[i]='\0';
  39. }
  40. command1[0]=NULL;command1[1]=NULL;command1[2]=NULL;command1[3]=NULL;
  41. command2[0]=NULL;command2[1]=NULL;command2[2]=NULL;
  42.  
  43. printf("$");
  44.  
  45. fgets(str,sizeof(str),stdin);
  46. if (feof(stdin)){
  47. printf("Ctrl+D exit \n");
  48. exit(0);
  49. }
  50. str[strlen(str)-1] ='\0';
  51. fflush( stdin );
  52.  
  53. if(strchr(str,'|')!=NULL){//파이프 사용하는 경우
  54. text1 = strtok (str,"|");
  55. text2 = strtok (NULL, "|");
  56. //printf("cmd1 :%s\n cmd2 :%s\n",text1,text2);
  57. strcat(text1,"\0");
  58. strcat(text2, "\0");
  59. i=0;//초기화
  60. pch = strtok (text1," ");
  61. while (pch != NULL && i<3)
  62. {
  63. command1[i]=pch;
  64. pch = strtok (NULL, " ");
  65. //printf ("command1[%d]:%s\n",i,command1[i]);
  66. i++;
  67. }
  68. command1[i]=(char*)0;
  69. i=0;//초기화
  70. pch = strtok (text2," ");
  71. while (pch != NULL && i<3)
  72. {
  73. command2[i]=pch;
  74. pch = strtok (NULL, " ");
  75. //printf ("command2[%d]:%s\n",i,command2[i]);
  76. i++;
  77. }
  78. command2[i]=(char*)0;
  79.  
  80. if(pipe(fd) == -1){//파이프 생성 fd[0] 읽기용, fd[1] 쓰기용
  81. printf("fail to call pipe()\n");
  82. exit(1);
  83. }
  84. switch(fork())//앞쪽 명령어프로세스 생성
  85. {
  86. case -1 : perror("fork error"); break;
  87. case 0 :
  88. if(close(1)==-1) perror("close1");
  89. if(dup(fd[1]) != 0);//표준출력 파이프 연결
  90. if(close(fd[0]) == -1 || close(fd[1]) == -1){
  91. perror("close2");
  92. }
  93. execvp(command1[0], command1);
  94. printf("command not found \n");
  95. exit(0);
  96. }
  97. switch(fork())//뒤쪽명령어 프로세스 생성
  98. {
  99. case -1 : perror ("fork"); break;
  100. case 0 :
  101. if(close(0) == -1) perror("close3");
  102. if(dup(fd[0]) != 0);//표준입력 파이프 연결
  103. if(close(fd[0]) == -1 || close(fd[1]) == -1) perror("close4");
  104. execvp(command2[0], command2);
  105. printf("command not found \n");
  106. exit(0);
  107. }
  108.  
  109. if(close(fd[0]) == -1 || close(fd[1]) == -1) perror("close5");
  110. while(wait(NULL) != -1);
  111. }else if(strchr(str,'>')!=NULL){// '>'사용된 경우
  112. text1 = strtok (str,">");
  113. text2 = strtok (NULL, ">");
  114. i=0;//초기화
  115. pch = strtok (text1," ");
  116. while (pch != NULL && i<3)
  117. {
  118. command1[i]=pch;
  119. pch = strtok (NULL, " ");
  120. //printf ("command1[%d]:%s\n",i,command1[i]);
  121. i++;
  122. }
  123. command1[i]=(char*)0;
  124. i=0;//초기화
  125. pch = strtok (text2," ");
  126. while (pch != NULL && i<3)
  127. {
  128. command2[i]=pch;
  129. pch = strtok (NULL, " ");
  130. //printf ("command2[%d]:%s\n",i,command2[i]);
  131. i++;
  132. }
  133. command2[i]=(char*)0;
  134.  
  135. switch(fork())//앞쪽명령어 프로세스 생성, 뒤쪽 파일 생성
  136. {
  137. case -1 : perror ("fork"); break;
  138. case 0 :
  139. fdr = open(command2[0], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  140. if(fdr==-1) {
  141. perror("파일 새로생성 오류");exit(1);
  142. }
  143. if( dup2(fdr, 1) == -1){
  144. perror("fdr dup error");
  145. }
  146. close(fdr);
  147. execvp(command1[0], command1);
  148. printf("command not found \n");
  149. exit(0);
  150. break;
  151. default : wait(NULL);
  152. }
  153. }else if(strchr(str,'<')!=NULL){// '<' 사용된 경우
  154. text1 = strtok (str,"<");
  155. text2 = strtok (NULL, "<");
  156. i=0;//초기화
  157. pch = strtok (text1," ");
  158. while (pch != NULL && i<3)
  159. {
  160. command1[i]=pch;
  161. pch = strtok (NULL, " ");
  162. //printf ("command1[%d]:%s\n",i,command1[i]);
  163. i++;
  164. }
  165. command1[i]=(char*)0;
  166. i=0;//초기화
  167. pch = strtok (text2," ");
  168. while (pch != NULL && i<3)
  169. {
  170. command2[i]=pch;
  171. pch = strtok (NULL, " ");
  172. //printf ("command2[%d]:%s\n",i,command2[i]);
  173. i++;
  174. }
  175. command2[i]=(char*)0;
  176. switch(fork())//앞쪽 파일 생성,뒤쪽 프로세스 생성
  177. {
  178. case -1 : perror ("fork"); break;
  179. case 0 :
  180. fdr = open(command1[0], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  181. if(fdr==-1) {
  182. perror("파일 새로생성 오류");exit(1);
  183. }
  184. if( dup2(fdr, 1) == -1){
  185. perror("fdr dup error");
  186. }
  187. close(fdr);
  188. execvp(command2[0], command2);
  189. printf("command not found \n");
  190. exit(0);
  191. break;
  192. default : wait(NULL);
  193. }
  194. }else{ //단일 명령문
  195. str[strlen(str)]='\0';
  196. //printf("onlycmd1:%s \n",str);
  197. i=0;//초기화
  198. pch = strtok (str," ");
  199. while (pch != NULL && i<3)
  200. {
  201. command1[i]=pch;
  202. pch = strtok (NULL, " ");
  203. //printf ("command1[%d]:%s\n",i,command1[i]);
  204. i++;
  205. }
  206. command1[i]=(char*)0;
  207. //printf ("command1[0]:%s\n",command1[0]);
  208. if(command1[0]!=NULL){
  209. if(strcmp(command1[0],"logout")==0){//명령어 logout
  210. exit(0);
  211. }else if(strcmp(command1[0],"cd")==0){//내장명령어 cd
  212. chdir(command1[1]);//현재 디렉토리 변경
  213. }else if(fork()==0){
  214. execvp(command1[0], command1);
  215. printf("command not found \n");
  216. exit(0);
  217. }
  218. wait(NULL);
  219. }
  220.  
  221. }
  222.  
  223. }
  224. return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement