/* Prosty interpreter polece */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define INPUTSIZ 256 #define MAXPAR 20 int outfd = -1; int readfd,dupfd,stderrfd; char *filename; pthread_mutex_t outfd_mutex = PTHREAD_MUTEX_INITIALIZER; #define BUFSIZE 1024 void *checkth(void *arg){ // fd_set fs; char buf[BUFSIZE]; while (1) { /* FD_ZERO(&fs); FD_SET(readfd,&fs); if (select(readfd+1,&fs,0,0,0)!=1) continue; */ ssize_t ret = read(readfd,buf,BUFSIZE); if (ret > 0) { write(0,buf,ret); pthread_mutex_lock(&outfd_mutex); if (outfd !=-1) write(outfd,buf,ret); pthread_mutex_unlock(&outfd_mutex); } } }; void doshell(char *cmd) { char *argv[MAXPAR+1]; int argc=0, status; pid_t pid; struct rusage ru; /* Dzielenie linii polecen na "wyrazy" */ if((argv[argc]=strtok(cmd," \t\n"))==NULL) return; while(argv[argc] && argc0 ){ if(WIFEXITED(status)){ printf("Proces nr %d zakonczony z kodem wyjscia %d\n", pid,WEXITSTATUS(status)); } if(WIFSIGNALED(status)){ printf("Proces nr %d dostal sygnal nr %d\n", pid,WTERMSIG(status)); } if(WIFSTOPPED(status)){ printf("Proces nr %d zatrzymany przez sygnal nr %d\n", pid, WSTOPSIG(status)); } /* Tu mona doda kod wykorzystujcy informacje zawarte w strukturze ru * Opis pl struktury w: man getrusage */ } } int main() { char buf[INPUTSIZ]; int pipes[2]; pthread_t tid; if (pipe(pipes)!=0) { perror("pipe"); exit (-1); } readfd = pipes[0]; dupfd = pipes[1]; stderrfd = open("stderr.txt",O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR); if (stderrfd == -1) { perror("stderr::open"); exit(-1); } if (pthread_create(&tid,0,checkth,0)!=0){ perror("pthread_create"); exit(-1); } printf("\n> "); while (fgets(buf, INPUTSIZ, stdin) != NULL) { doshell(buf); printf("\n> "); } exit(0); }