Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //myshell di ****
- //contributo di ****
- //C program
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h> //per strcmp()
- #include <stdio.h> //per perror()
- #include <sys/wait.h> //per wait()
- #include <pwd.h> //per nome user
- #include <netdb.h>
- #include <limits.h>
- #include <sys/socket.h>
- #define Red "\e[0;31m" //definisco tutti i colori
- #define BoldRed "\e[1;31m"
- #define Green "\e[0;32m"
- #define BoldGreen "\e[1;32m"
- #define Yellow "\e[0;33m"
- #define BoldYellow "\e[01;33m"
- #define Blue "\e[0;34m"
- #define BoldBlue "\e[1;34m"
- #define Magenta "\e[0;35m"
- #define BoldMagenta "\e[1;35m"
- #define Cyan "\e[0;36m"
- #define BoldCyan "\e[1;36m"
- #define White "\e[0;37m"
- #define reset "\e[0m" //definisco il codice per far tornare il colore normale
- #define lenght 1024 //definisco la lunghezza standard generale
- char *replaceWord(char *s, char *oldW, char *newW)
- {
- char *result;
- int i, cnt = 0;
- int newWlen = strlen(newW);
- int oldWlen = strlen(oldW);
- // Counting the number of times old word
- // occur in the string
- for (i = 0; s[i] != '\0'; i++)
- {
- if (strstr(&s[i], oldW) == &s[i])
- {
- cnt++;
- // Jumping to index after the old word.
- i += oldWlen - 1;
- }
- }
- // Making new string of enough length
- result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);
- i = 0;
- while (*s)
- {
- // compare the substring with the result
- if (strstr(s, oldW) == s)
- {
- strcpy(&result[i], newW);
- i += newWlen;
- s += oldWlen;
- }
- else
- result[i++] = *s++;
- }
- result[i] = '\0';
- return result;
- }
- const char *getUserName(void) //funzione per prendere il nome utente tramite funzioni incluse nel file .h
- {
- uid_t uid = geteuid();
- struct passwd *pw = getpwuid(uid);
- if (pw)
- {
- return pw->pw_name;
- }
- return "";
- }
- int main(void) //main del programma
- { //dichiarazioni di tutte le variabili come 2 vettori, 1 matrice un char* e una serie di counter
- 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
- char *arguments[lenght]; //array di parametri
- 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
- int s = 0;
- gethostname(hostbuffer, sizeof(hostbuffer)); //processo per prendere l'hostname tramite funzioni dichiarate nel file .h
- gethostbyname(hostbuffer);
- getcwd(cwd, sizeof(cwd)); //per trovare la directory
- do //ciclo che itera finche il comando inserito non è "exit"
- {
- sprintf(x, "/home/%s", getUserName());
- getcwd(cwd, sizeof(cwd));
- for (k = 0; x[k]; k++);
- write(1, colorS, 8);
- write(1, getUserName(), strlen(getUserName())); //scrivo il nome utente
- write(1, "@", 1); //scrivo il carattere @
- write(1, hostbuffer, strlen(hostbuffer)); //scrivo l'hostname
- sprintf(out, "%s:%s%s%s$ ", White, BoldBlue, replaceWord(cwd, x, w), White);
- write(1, BoldBlue, 8);
- write(1, out, strlen(out)); //scrivo i due caratteri : per simulare al meglio la bash orignale
- fflush(stdout);
- nSpace = 0, lastSpace = 0, j = 0, a = 0; //setto le varibili che mi serviranno
- do
- {
- write(1, White, 8);
- a = read(0, command, lenght); //uso la read per l'input
- } while (a < 0); //lo legge finchè l'utente scrive effettivamente qualcosa
- command[a - 1] = 0; //regola della read di mettere a 0 l'ultimo valore input
- if (strcmp(command, "help") == 0)
- {
- write(1, "myshell_v2.c © \nprogrammed by ****. Custom Command: \t 'set color'\n", 149);
- check = 1;
- }
- if (strcmp(command, "set color") == 0)
- {
- 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);
- scanf("%lld", &color);
- switch (color)
- {
- case 1:
- color = 0;
- memcpy(colorS, Red, 8);
- write(1, colorS, 8);
- break;
- case 2:
- memcpy(colorS, BoldRed, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 3:
- memcpy(colorS, Green, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 4:
- memcpy(colorS, BoldGreen, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 5:
- memcpy(colorS, Yellow, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 6:
- memcpy(colorS, BoldYellow, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 7:
- memcpy(colorS, Blue, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 8:
- memcpy(colorS, BoldBlue, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 9:
- memcpy(colorS, Magenta, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 10:
- memcpy(colorS, BoldMagenta, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 11:
- memcpy(colorS, Cyan, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 12:
- memcpy(colorS, BoldCyan, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- case 13:
- memcpy(colorS, White, 8);
- write(1, colorS, 8);
- color = 0;
- break;
- default:
- write(1, "[!] Error Choice\n", 18);
- color = 0;
- break;
- }
- check = 1;
- fflush(stdout);
- }
- if ((strncmp(command, "cd", 2)) == 0)
- {
- for (i = 0; command[i]; i++) // implementazione con matrice
- {
- if (command[i] == ' ')
- {
- m[j][i - lastSpace] = 0;
- j++;
- nSpace++;
- lastSpace = i + 1;
- }
- else
- {
- m[j][i - lastSpace] = command[i];
- }
- }
- for (i = lastSpace; command[i]; i++) // per ultima parola dopo l'ultimo spazio
- {
- m[j][i - lastSpace] = command[i];
- }
- m[j][i - lastSpace] = 0;
- for (i = 0; i < nSpace + 1; i++) // costruzione di arguments da matrice input
- {
- arguments[i] = m[i];
- }
- arguments[i] = NULL;
- if (arguments[1] == NULL)
- {
- sprintf(x, "/home/%s", getUserName());
- chdir(x);
- }
- else if (chdir(arguments[1]) != 0)
- {
- perror("[!] Error");
- }
- }
- else if (a != 1 && check != 1) //se il comando inserito è vuoto, ricomincia il ciclo e la write
- {
- for (i = 0; command[i]; i++) // implementazione con matrice
- {
- if (command[i] == ' ')
- {
- m[j][i - lastSpace] = 0;
- j++;
- nSpace++;
- lastSpace = i + 1;
- }
- else
- {
- m[j][i - lastSpace] = command[i];
- }
- }
- for (i = lastSpace; command[i]; i++) // per ultima parola dopo l'ultimo spazio
- {
- m[j][i - lastSpace] = command[i];
- }
- m[j][i - lastSpace] = 0;
- for (i = 0; i < nSpace + 1; i++) // costruzione di arguments da matrice input
- {
- arguments[i] = m[i];
- }
- arguments[i] = NULL; //inserisco all'ultima posizione il carattere NULL
- if (strcmp(command, "exit")) // se il comando non è exit, esegue comando, con gestioni errori
- {
- id = fork(); //creo un figlio
- if (id) //se sono padre id=1
- {
- wait(&s); //aspetto la morte del figlio
- }
- else if (id == -1) //se la fork() non va, quindi restituisce -1 dice all'utente che è successo un errore interno
- {
- write(1, "[ERROR] Internal Error!", 23);
- exit(1); //uccido il figlio
- }
- else if (id == 0) //infine se sono il figlio id= 0
- {
- execvp(arguments[0], arguments); //eseguo il comando mettendo i parametri e il comando in un vettore
- perror("[!] Error"); //se non và l'execvp() manda un segnale di errore, giustificando il perchè di questo
- exit(1); //uccido il figlio
- }
- }
- }
- write(1, colorS, 8);
- check = 0;
- fflush(stdout);
- } while (strcmp(command, "exit")); //ciclo che itera finche il comando inserito non è "exit" ovvero se la strcmp() non ritorna 0
- write(1, reset, 5); //setto il colore standard
- return 0; //il programma è stato eseguito correttamente e ritorna 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement