Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <sys/stat.h> // open()
- #include <signal.h> // signal()
- #include <fcntl.h> // open()
- #include <stdio.h> // printf(), ...
- #include <time.h> // time(), ...
- #include <string.h>
- #include <sstream>
- #include <iostream>
- #include <unistd.h> // strtok()
- #define MAXLINE 100
- #define MOD "exit with CTR C"
- time_t start;
- int read_command(char *command,char *parameters[]) { // prompt for user input and read a command line
- char dir[FILENAME_MAX];
- getcwd(dir,FILENAME_MAX); //get current dir and safe into dir
- fprintf(stdout, "%s >",dir); //Print out current dir
- int noParam=0;
- char linearray[MAXLINE];
- fgets(linearray, MAXLINE, stdin);
- char* pointer;
- pointer = strtok(linearray," \n"); //Delimit at blank and \n (Enter)
- while(pointer != NULL){
- if(noParam == 0)
- strcpy(command,pointer);
- parameters[noParam] = pointer;
- noParam++;
- pointer = strtok(NULL," \n");
- }
- parameters[noParam] = NULL;
- return(noParam);
- } // read_command
- void childhandler(int) {
- pid_t pid;
- int status;
- //waitpid(-1, &status, WNOHANG) checks if any zombie children exists
- //If yes, it will be reaped and return either 0 if another unterminated child exists
- //Or -1 when no more are left
- /* Aus Dokumentation pubs.opengroup.org:
- *WNOHANG
- *The waitpid() function shall not suspend execution of the calling thread
- *if status is not immediately available for one of the child processes specified by pid.
- */
- while((pid = waitpid(-1, &status, WNOHANG)) > 0);
- }
- void exithandler(int){
- time_t end = time(NULL); //nehme Zeit, wenn STRG+C gedrückt wird
- int seconds = difftime(end,start); //Differenz zwischen Start und Ende
- int minutes = seconds / 60;
- int hours = minutes / 60;
- seconds = seconds % 60;
- minutes = minutes % 60;
- fprintf(stdout,"\nTerminated\nTime elapsed:%dh %dm %ds\n",hours,minutes,seconds);
- exit(0);
- }
- int main(int argc, char *argv[]) {
- start = time(NULL); //Zeit bei Start des Programms
- signal(SIGINT, exithandler);
- int childPid;
- int status;
- char command[MAXLINE];
- char *parameters[MAXLINE];
- int noParams;
- while (1) {
- noParams = read_command(command, parameters); // read user input
- if(strncmp(command,"cd",2)==0){
- chdir(parameters[1]);
- continue;
- }
- if(strcmp(parameters[noParams-1],"&") == 0) {
- parameters[noParams-1] = NULL;
- noParams--;
- //SIGCHLD is the Signal when a Childprocess terminates
- signal(SIGCHLD,childhandler);
- if (noParams == 0) {
- fprintf(stderr, "no command ?!\n");
- exit(1);
- }
- if ((childPid = fork()) == -1) { // create process
- fprintf(stderr, "can't fork!\n");
- exit(2);
- } else if (childPid == 0) { // child process
- execvp(command, parameters); // executes command
- exit(3);
- }
- else { // father
- printf("[%d]\n",childPid);
- }
- } else {
- if (noParams == 0) {
- fprintf(stderr, "no command ?!\n");
- exit(1);
- }
- if ((childPid = fork()) == -1) { // create process
- fprintf(stderr, "can't fork!\n");
- exit(2);
- } else if (childPid == 0) { // child process
- execvp(command, parameters); // executes command
- exit(3);
- }
- else { // father
- waitpid(childPid, &status, WUNTRACED | WCONTINUED);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement