Advertisement
Guest User

myshell_v3.x

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.51 KB | None | 0 0
  1.  
  2. //myshell di ****
  3. //contributo di ****
  4. //C program
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>   //per strcmp()
  9. #include <stdio.h>    //per perror()
  10. #include <sys/wait.h> //per wait()
  11. #include <pwd.h>      //per nome user
  12. #include <netdb.h>
  13. #include <limits.h>
  14. #include <sys/socket.h>
  15. #define Red "\e[0;31m" //definisco tutti i colori
  16. #define BoldRed "\e[1;31m"
  17. #define Green "\e[0;32m"
  18. #define BoldGreen "\e[1;32m"
  19. #define Yellow "\e[0;33m"
  20. #define BoldYellow "\e[01;33m"
  21. #define Blue "\e[0;34m"
  22. #define BoldBlue "\e[1;34m"
  23. #define Magenta "\e[0;35m"
  24. #define BoldMagenta "\e[1;35m"
  25. #define Cyan "\e[0;36m"
  26. #define BoldCyan "\e[1;36m"
  27. #define White "\e[0;37m"
  28. #define reset "\e[0m" //definisco il codice per far tornare il colore normale
  29. #define lenght 1024   //definisco la lunghezza standard generale
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. char *replaceWord(char *s, char *oldW, char *newW)
  37. {
  38.     char *result;
  39.     int i, cnt = 0;
  40.     int newWlen = strlen(newW);
  41.     int oldWlen = strlen(oldW);
  42.     // Counting the number of times old word
  43.     // occur in the string
  44.     for (i = 0; s[i] != '\0'; i++)
  45.     {
  46.         if (strstr(&s[i], oldW) == &s[i])
  47.         {
  48.             cnt++;
  49.             // Jumping to index after the old word.
  50.             i += oldWlen - 1;
  51.         }
  52.     }
  53.     // Making new string of enough length
  54.     result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);
  55.     i = 0;
  56.     while (*s)
  57.     {
  58.         // compare the substring with the result
  59.         if (strstr(s, oldW) == s)
  60.         {
  61.             strcpy(&result[i], newW);
  62.             i += newWlen;
  63.             s += oldWlen;
  64.         }
  65.         else
  66.             result[i++] = *s++;
  67.     }
  68.     result[i] = '\0';
  69.     return result;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. const char *getUserName(void) //funzione per prendere il nome utente tramite funzioni incluse nel file .h
  80. {
  81.     uid_t uid = geteuid();
  82.     struct passwd *pw = getpwuid(uid);
  83.     if (pw)
  84.     {
  85.         return pw->pw_name;
  86.     }
  87.     return "";
  88. }
  89.  
  90. int main(void)                                                                                                               //main del programma
  91. {                                                                                                                            //dichiarazioni di tutte le variabili come 2 vettori, 1 matrice un char* e una serie di counter
  92.     char hostbuffer[lenght], command[lenght], m[lenght][lenght], cwd[lenght], colorS[8] = "\e[1;32m", x[lenght], w[1] = "~", out[lenght]; //in hostbuffer verrà inserito l'hostname della macchina usata
  93.     char *arguments[lenght];                                                                                                 //array di parametri
  94.     long long a = 0, i = 0, nSpace = 0, lastSpace = 0, j = 0, id = 0, color = 0, check = 0, k = 0;                           //svariate variabili per wait(), buffer e cicli
  95.     int s = 0;
  96.     gethostname(hostbuffer, sizeof(hostbuffer)); //processo per prendere l'hostname tramite funzioni dichiarate nel file .h
  97.     gethostbyname(hostbuffer);
  98.     getcwd(cwd, sizeof(cwd)); //per trovare la directory
  99.     do //ciclo che itera finche il comando inserito non è "exit"
  100.     {
  101.         sprintf(x, "/home/%s", getUserName());
  102.         getcwd(cwd, sizeof(cwd));
  103.         for (k = 0; x[k]; k++);
  104.         write(1, colorS, 8);
  105.         write(1, getUserName(), strlen(getUserName()));     //scrivo il nome utente
  106.         write(1, "@", 1);                                   //scrivo il carattere @
  107.         write(1, hostbuffer, strlen(hostbuffer));           //scrivo l'hostname
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.         sprintf(out, "%s:%s%s%s$ ", White, BoldBlue, replaceWord(cwd, x, w), White);
  115.         write(1, BoldBlue, 8);
  116.         write(1, out, strlen(out)); //scrivo i due caratteri :  per simulare al meglio la bash orignale
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.         fflush(stdout);
  124.         nSpace = 0, lastSpace = 0, j = 0, a = 0; //setto le varibili che mi serviranno
  125.         do
  126.         {
  127.             write(1, White, 8);
  128.             a = read(0, command, lenght); //uso la read per l'input
  129.         } while (a < 0);                  //lo legge finchè l'utente scrive effettivamente qualcosa
  130.         command[a - 1] = 0;               //regola della read di mettere a 0 l'ultimo valore input
  131.         if (strcmp(command, "help") == 0)
  132.         {
  133.             write(1, "myshell_v2.c © \nprogrammed by ****. Custom Command: \t 'set color'\n", 149);
  134.             check = 1;
  135.         }
  136.         if (strcmp(command, "set color") == 0)
  137.         {
  138.             write(1, "Color:\n 1 = Red\t 2 = BoldRed\t 3 = Green\n 4 = BoldGreen\t 5 = Yellow\t 6 = BoldYellow\n 7  = Blue\t 8 = BoldBlue\t 9 = Magenta\n 10 = BoldMagenta\t 11 = Cyan\t 12 = BoldCyan\t 13 = White\n", 177);
  139.             scanf("%lld", &color);
  140.             switch (color)
  141.             {
  142.             case 1:
  143.                 color = 0;
  144.                 memcpy(colorS, Red, 8);
  145.                 write(1, colorS, 8);
  146.                 break;
  147.             case 2:
  148.                 memcpy(colorS, BoldRed, 8);
  149.                 write(1, colorS, 8);
  150.                 color = 0;
  151.                 break;
  152.             case 3:
  153.                 memcpy(colorS, Green, 8);
  154.                 write(1, colorS, 8);
  155.                 color = 0;
  156.                 break;
  157.             case 4:
  158.                 memcpy(colorS, BoldGreen, 8);
  159.                 write(1, colorS, 8);
  160.                 color = 0;
  161.                 break;
  162.             case 5:
  163.                 memcpy(colorS, Yellow, 8);
  164.                 write(1, colorS, 8);
  165.                 color = 0;
  166.                 break;
  167.             case 6:
  168.                 memcpy(colorS, BoldYellow, 8);
  169.                 write(1, colorS, 8);
  170.                 color = 0;
  171.                 break;
  172.             case 7:
  173.                 memcpy(colorS, Blue, 8);
  174.                 write(1, colorS, 8);
  175.                 color = 0;
  176.                 break;
  177.             case 8:
  178.                 memcpy(colorS, BoldBlue, 8);
  179.                 write(1, colorS, 8);
  180.                 color = 0;
  181.                 break;
  182.             case 9:
  183.                 memcpy(colorS, Magenta, 8);
  184.                 write(1, colorS, 8);
  185.                 color = 0;
  186.                 break;
  187.             case 10:
  188.                 memcpy(colorS, BoldMagenta, 8);
  189.                 write(1, colorS, 8);
  190.                 color = 0;
  191.                 break;
  192.             case 11:
  193.                 memcpy(colorS, Cyan, 8);
  194.                 write(1, colorS, 8);
  195.                 color = 0;
  196.                 break;
  197.             case 12:
  198.                 memcpy(colorS, BoldCyan, 8);
  199.                 write(1, colorS, 8);
  200.                 color = 0;
  201.                 break;
  202.             case 13:
  203.                 memcpy(colorS, White, 8);
  204.                 write(1, colorS, 8);
  205.                 color = 0;
  206.                 break;
  207.             default:
  208.                 write(1, "[!] Error Choice\n", 18);
  209.                 color = 0;
  210.                 break;
  211.             }
  212.             check = 1;
  213.             fflush(stdout);
  214.         }
  215.         if ((strncmp(command, "cd", 2)) == 0)
  216.         {
  217.             for (i = 0; command[i]; i++) // implementazione con matrice
  218.             {
  219.                 if (command[i] == ' ')
  220.                 {
  221.                     m[j][i - lastSpace] = 0;
  222.                     j++;
  223.                     nSpace++;
  224.                     lastSpace = i + 1;
  225.                 }
  226.                 else
  227.                 {
  228.                     m[j][i - lastSpace] = command[i];
  229.                 }
  230.             }
  231.             for (i = lastSpace; command[i]; i++) // per ultima parola dopo l'ultimo spazio
  232.             {
  233.                 m[j][i - lastSpace] = command[i];
  234.             }
  235.             m[j][i - lastSpace] = 0;
  236.             for (i = 0; i < nSpace + 1; i++) // costruzione di arguments da matrice input
  237.             {
  238.                 arguments[i] = m[i];
  239.             }
  240.             arguments[i] = NULL;
  241.             if (arguments[1] == NULL)
  242.             {
  243.                 sprintf(x, "/home/%s", getUserName());
  244.                 chdir(x);
  245.             }
  246.             else if (chdir(arguments[1]) != 0)
  247.             {
  248.                 perror("[!] Error");
  249.             }
  250.         }
  251.         else if (a != 1 && check != 1) //se il comando inserito è vuoto, ricomincia il ciclo e la write
  252.         {
  253.             for (i = 0; command[i]; i++) // implementazione con matrice
  254.             {
  255.                 if (command[i] == ' ')
  256.                 {
  257.                     m[j][i - lastSpace] = 0;
  258.                     j++;
  259.                     nSpace++;
  260.                     lastSpace = i + 1;
  261.                 }
  262.                 else
  263.                 {
  264.                     m[j][i - lastSpace] = command[i];
  265.                 }
  266.             }
  267.             for (i = lastSpace; command[i]; i++) // per ultima parola dopo l'ultimo spazio
  268.             {
  269.                 m[j][i - lastSpace] = command[i];
  270.             }
  271.             m[j][i - lastSpace] = 0;
  272.             for (i = 0; i < nSpace + 1; i++) // costruzione di arguments da matrice input
  273.             {
  274.                 arguments[i] = m[i];
  275.             }
  276.             arguments[i] = NULL;         //inserisco all'ultima posizione il carattere NULL
  277.             if (strcmp(command, "exit")) // se il comando non è exit, esegue comando, con gestioni errori
  278.             {
  279.                 id = fork(); //creo un figlio
  280.                 if (id)      //se sono padre id=1
  281.                 {
  282.                     wait(&s); //aspetto la morte del figlio
  283.                 }
  284.                 else if (id == -1) //se la fork() non va, quindi restituisce -1 dice all'utente che è successo un errore interno
  285.                 {
  286.                     write(1, "[ERROR] Internal Error!", 23);
  287.                     exit(1); //uccido il figlio
  288.                 }
  289.                 else if (id == 0) //infine se sono il figlio id= 0
  290.                 {
  291.                     execvp(arguments[0], arguments); //eseguo il comando mettendo i parametri e il comando in un vettore
  292.                     perror("[!] Error");             //se non và l'execvp() manda un segnale di errore, giustificando il perchè di questo
  293.                     exit(1);                         //uccido il figlio
  294.                 }
  295.             }
  296.         }
  297.         write(1, colorS, 8);
  298.         check = 0;
  299.         fflush(stdout);
  300.     } while (strcmp(command, "exit")); //ciclo che itera finche il comando inserito non è "exit" ovvero se la strcmp() non ritorna 0
  301.     write(1, reset, 5);                //setto il colore standard
  302.     return 0;                          //il programma è stato eseguito correttamente e ritorna 0
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement