?? shell1.c
字號:
#include <sys/types.h>#include <sys/wait.h>#include <stdio.h>#include <unistd.h>#include <signal.h>#define MAXLINE 128int main(void){ char buf[MAXLINE]; pid_t pid; int status; sigset_t newmask_C,oldmask_C,newmask_Z,oldmask_Z,zeromask; sigemptyset(&zeromask); sigemptyset(&newmask_C); sigaddset(&newmask_C,SIGINT); sigemptyset(&newmask_Z); sigaddset(&newmask_Z,SIGTSTP); if(sigprocmask(SIG_BLOCK,&newmask_C,&oldmask_C)<0) { printf("SIG_BLOCK error\n"); exit(-1); } if(sigprocmask(SIG_BLOCK,&newmask_Z,&oldmask_Z)<0) { printf("SIG_BLCOK error\n"); exit(-1); } printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { buf[strlen(buf) - 1] = 0;/* replace newline with null */ if(strcmp(buf,"quit")==0) break; if ( (pid = fork()) < 0) printf("fork error"); else if (pid == 0) { /* child */ execlp(buf, buf, (char *) 0); printf("The command %s doesn't exist\n", buf); exit(127); } /* parent */ if ( (pid = waitpid(pid, &status, 0)) < 0) printf("waitpid error"); printf("%% "); } if(sigsuspend(&zeromask)!=-1) { printf("sigsuspend error\n"); exit(-1); } if(sigprocmask(SIG_SETMASK,&oldmask_C,NULL)<0) { printf("SIG_SETMASK error\n"); exit(-1); } if(sigprocmask(SIG_SETMASK,&oldmask_Z,NULL)<0) { printf("SIG_SETMASK error\n"); exit(-1); } exit(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -