Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/wait.h>
  3. #include <cstdlib>
  4. #include <sys/stat.h>           // open()
  5. #include <signal.h>             // signal()
  6. #include <fcntl.h>              // open()
  7. #include <stdio.h>              // printf(), ...
  8. #include <time.h>               // time(), ...
  9. #include <string.h>             // strtok()
  10. #include <iostream>
  11. #include <string>
  12. #include <X11/Xlib.h>
  13. #include <X11/keysym.h>
  14.  
  15.  
  16.  
  17. #define MAXLINE 100
  18. #define MOD "exit with CTR C"
  19. using namespace std;
  20.  
  21. int read_command(char *command, char *parameters[]) { // prompt for user input and read a command line
  22.  
  23.     char pfad[256];
  24.  
  25.     // *** Das Ausgeben des Aktuelen Pfades
  26.     getcwd(pfad, 256);          // Zwischenspeicher fuer den Pfad
  27.     fprintf(stdout, "$ ");
  28.     cout << pfad << "> " ;      // Ausgabe des Aktuelen Pfads im Promt
  29.     // ***************
  30.  
  31.  
  32.     // *** Einlesen der eingaben in Command und parameters
  33.     int i = 0;
  34.     cin.getline(command,MAXLINE); // Lese eingabe von Prompt aus und schreibe sie in Command
  35.  
  36.  
  37.     // *** Erstellen und eintragen einer Logfile
  38.     time_t t;
  39.     char* timechar;                     // Die Zeit Zwischenspeicehr um \n rauszuloesen
  40.     timechar= ctime(&t);                // Die Aktuele Zeit in timechar speichern
  41.     timechar = strtok(timechar,"\n");   // \n herausloesen
  42.     FILE* logfile;                      // Eine File anlegen
  43.     logfile = fopen("logFile.txt", "a");// Die Logfile Oeffnen
  44.     fprintf(logfile,"[%s] %s \n", timechar, command);   // In die Logfile das Datum und den Komando speichern
  45.  
  46.     // ***************
  47.  
  48.  
  49.     parameters[i++] = strtok(command," "); // Teile die Zeichenkette nach jedem Leerzeichen
  50.     while(i<MAXLINE&&(parameters[i++] = strtok(NULL," ")) != NULL); //Schreibe die Teile der Zeichenkette in Parameters
  51.     // ***************
  52.  
  53.  
  54. } // read_command
  55.  
  56. void escapeHandler(int){
  57.     char input;
  58.  
  59.     cout << " Wollen Sie die Shell beenden? (y/n)" << endl;
  60.     cin >> input;
  61.     if(input == 'y')
  62.         exit(0);
  63.     else
  64.         return;
  65.  
  66. }
  67.  
  68. int main(int argc, char *argv[]) {
  69.     int childPid;
  70.     int status;
  71.     char command[MAXLINE];
  72.     char *parameters[MAXLINE];
  73.     int noParams;
  74.     bool backgroundCheck = false;
  75.     chdir("/tmp");
  76.     signal(SIGINT,escapeHandler); // CTR-C Handler
  77.     while (1) {
  78.         noParams = read_command(command, parameters);   // read user input
  79.  
  80.         // *** Erkennen von &
  81.         for(int i = 0; i<MAXLINE ; i++){
  82.  
  83.             if(parameters[i] == 0x0)               // Wenn die Letzte Eingabe ereicht wurde und es kein & gibt springe raus und fuehre das Programm normal aus;
  84.                 break;
  85.  
  86.             else if(strcmp(parameters[i], "&") == 0){        // Vergleiche an der Possition i den Inhalt von parameters mit &
  87.                 parameters[i] = 0x0;                         // Entferne das & und Füge ein NULL ein
  88.                 backgroundCheck = true;
  89.                 break;
  90.             }
  91.         }
  92.         // ***************
  93.  
  94.  
  95.         // *** Abfangen von CD Befehlen
  96.         if(strcmp(command, "cd") == 0){                 // Es Wird geprüft ob der Befehl CD ist
  97.             if(chdir(parameters[1]))                    // Führe chdir aus, wenn Pfad nicht Korrekt
  98.                 cout << "Pfad nicht gefunden"<< endl;   // wird Fehlermeldung ausgegeben
  99.         }
  100.         // ***************
  101.  
  102.  
  103.         else if ((childPid = fork()) == -1) { // create process
  104.             fprintf(stderr, "can't fork!\n");
  105.             exit(3);
  106.         } else if (childPid == 0) { // child process
  107.             execvp(command, parameters); // executes command
  108.  
  109.             exit(3);
  110.         } else { // father
  111.             if(backgroundCheck){
  112.                 waitpid(childPid,NULL,WNOHANG);          // Erstelle Das Kind ohne auf das Kind zu Warten
  113.                 signal(SIGCHLD,SIG_IGN);                 // Killt das Kind nach der Ausfuehrung , verhindert Zombies
  114.                 cout << "[" << childPid << "]" << endl;  // Ausgabe der erzeugten Kinder PID
  115.                 backgroundCheck = false;                 // Hintergrundcheck abschalten
  116.             }
  117.             else
  118.                 waitpid(childPid, &status, WUNTRACED | WCONTINUED); // Erstelle das Kind und Warte auf Beendigung des Kindes
  119.         }
  120.  
  121.  
  122.  
  123.  
  124.     }
  125.  
  126.     close(0);
  127.     exit(0);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement