Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "ssh_admin.h"
- struct Fichier{
- //0: Dossier, 1: Texte, 2; Programme
- int type;
- char nom[20];
- char chemin[50];
- //type = 0
- Fichier *pere;
- int nb_fichier;
- Fichier *contenu[30];
- //type = 1
- char texte[200];
- //type = 2
- void (*pf)();
- };
- void cls(){
- #if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
- system("clear");
- #endif
- #if defined(_WIN32) || defined(_WIN64)
- system("cls");
- #endif
- }
- void printc(char *format, char *string, color c){
- switch(c){
- case red:
- printf("\033[1;31m");
- break;
- case green:
- printf("\033[1;32m");
- break;
- case blue:
- printf("\033[1;34m");
- break;
- case none:
- break;
- }
- printf(format, string);
- printf("\033[0m");
- }
- void wait(int second){
- for (int i=0; i<3; i++){
- printf(".");
- fflush(stdout);
- sleep(second);
- }
- }
- void auth(){
- cls();
- printf("Connexion au serveur codeanon:22");
- wait(1);
- printf("\nConnexion au serveur réussie !\n");
- printf("\nAuthentification en cours");
- wait(1);
- printf("\nConnexion en tant que admin réussie !\n");
- sleep(1);
- cls();
- }
- void help(){
- printf("\nListe des commandes disponibles:\n\n");
- printf("\thelp\tAffiche ce message\n");
- printf("\tcd\tChange de répertoire courant\n");
- printf("\tls\tAffiche le contenu du répertoire courant\n");
- printf("\tclear\tEfface le contenu de la console\n");
- printf("\tcat\tAffiche le contenu d'un fichier texte\n");
- printf("\texec\tExecute un programme\n");
- printf("\tquit\tFerme la connexion avec le serveur\n");
- printf("\nLes");
- printc(" %s ", "dossiers", blue);
- printf("sont en bleu, les fichiers sont en blanc, les");
- printc(" %s ", "programmes", green);
- printf("sont en vert.\n\n");
- }
- void ls(Fichier *pwd){
- color couleur;
- for (int i=0; i < pwd->nb_fichier; i++){
- switch ((pwd->contenu[i])->type){
- case 0:
- couleur = blue;
- break;
- case 1:
- couleur = none;
- break;
- case 2:
- couleur = green;
- break;
- }
- printc("%s ", (pwd->contenu[i])->nom, couleur);
- }
- if (pwd->nb_fichier != 0)
- printf("\n");
- }
- void cd(char *arg, Fichier **ppwd, Fichier *proot){
- int argc=0, valide=1, j;
- Fichier *wdtmp;
- if (arg[0] == '/')
- wdtmp = proot;
- else
- wdtmp = *ppwd;
- char *arg_path[50];
- argc = split(arg, arg_path, "/");
- for (int i=0; valide && i < argc; i++){
- if (!strcmp(arg_path[i], ".."))
- wdtmp = wdtmp->pere;
- else
- {
- j=0;
- while (j < wdtmp->nb_fichier && strcmp((wdtmp->contenu[j])->nom, arg_path[i]))
- j++;
- if (j == wdtmp->nb_fichier)
- valide = 0;
- else
- wdtmp = wdtmp->contenu[j];
- }
- }
- if (valide)
- *ppwd = wdtmp;
- else
- printf("Chemin non valide\n");
- }
- void cat(Fichier *pwd, char *arg){
- int i=0;
- Fichier *fichier_texte;
- while (i < pwd->nb_fichier && strcmp((pwd->contenu[i])->nom, arg))
- i++;
- if (i == pwd->nb_fichier)
- printf("Fichier introuvable\n");
- else{
- fichier_texte = pwd->contenu[i];
- if (fichier_texte->type != 1)
- printf("Ce fichier n'est pas un fichier texte\n");
- else
- printf("%s\n", fichier_texte->texte);
- }
- }
- void exec(Fichier *pwd, char *arg){
- int i=0;
- Fichier *fichier_prog;
- while (i < pwd->nb_fichier && strcmp((pwd->contenu[i])->nom, arg))
- i++;
- if (i == pwd->nb_fichier)
- printf("Fichier introuvable\n");
- else{
- fichier_prog = pwd->contenu[i];
- if (fichier_prog->type != 2)
- printf("Ce fichier n'est pas un programme\n");
- else
- fichier_prog->pf();
- }
- }
- int split(char *chaine, char **argv, char *delim){
- int argc=0;
- char *pToken = strtok(chaine, delim);
- while (pToken != NULL){
- argv[argc] = pToken;
- argc ++;
- pToken = strtok(NULL, delim);
- }
- argv[argc] = NULL;
- return argc;
- }
- int saisie_commande(Fichier **ppwd, Fichier *proot){
- int argc, boucle = 1;
- char commande[100], *argv[50];
- printc("%s", "admin@codeanon", red);
- printf(":");
- printc("%s", (*ppwd)->chemin, blue);
- printf("# ");
- fgets(commande, sizeof(commande), stdin);
- argc = split(commande, argv, " \n");
- if (argc == 0)
- return boucle;
- else if (!strcmp(argv[0], "quit")){
- if (argc != 1)
- printf("Trop d'arguments !\n");
- else
- boucle = 0;
- }
- else if (!strcmp(argv[0], "help")){
- if (argc != 1)
- printf("Trop d'arguments !\n");
- else
- help();
- }
- else if (!strcmp(argv[0], "clear")){
- if (argc != 1)
- printf("Trop d'arguments !\n");
- else
- cls();
- }
- else if (!strcmp(argv[0], "ls")){
- if (argc != 1)
- printf("Trop d'arguments !\n");
- else
- ls(*ppwd);
- }
- else if (!strcmp(argv[0], "cd")){
- if (argc == 1){
- printf("La commande cd prend en argument un chemin, exemple:\n");
- printf("\tcd /home/Varga\tpour aller dans le dossier nommé Varga\n");
- printf("\tcd ..\t\tpour aller dans le dossie parent (précédent)\n");
- }
- else if (argc != 2)
- printf("Trop d'arguments !\n");
- else
- cd(argv[1], ppwd, proot);
- }
- else if (!strcmp(argv[0], "cat")){
- if (argc == 1){
- printf("La commande cat prend en argument un fichier texte\n");
- }
- else if (argc != 2)
- printf("Trop d'arguments !\n");
- else
- cat(*ppwd, argv[1]);
- }
- else if (!strcmp(argv[0], "exec")){
- if (argc == 1){
- printf("La commande exec prend en argument un fichier programme\n");
- }
- else if (argc != 2)
- printf("Trop d'arguments !\n");
- else
- exec(*ppwd, argv[1]);
- }
- else
- printf("Commande inconnue\n");
- return boucle;
- }
- void nouveau_fichier(Fichier *pfichier, int type, char nom[], Fichier *pparent){
- pfichier->type = type;
- strcpy(pfichier->nom, nom);
- strcpy(pfichier->chemin, pparent->chemin);
- if (strcmp(pparent->nom, "root"))
- strcat(pfichier->chemin, "/");
- strcat(pfichier->chemin, pfichier->nom);
- if (type == 0){
- pfichier->pere = pparent;
- pfichier->nb_fichier = 0;
- }
- pparent->contenu[pparent->nb_fichier] = pfichier;
- pparent->nb_fichier++;
- }
- void set_env(Fichier **ppwd, Fichier *proot){
- Fichier *home, *bin, *varga, *ixxios, *prog, *txt;
- home = malloc(sizeof(Fichier));
- bin = malloc(sizeof(Fichier));
- varga = malloc(sizeof(Fichier));
- txt = malloc(sizeof(Fichier));
- ixxios = malloc(sizeof(Fichier));
- prog = malloc(sizeof(Fichier));
- nouveau_fichier(home, 0, "home", proot);
- nouveau_fichier(bin, 0, "bin", proot);
- nouveau_fichier(varga, 0, "varga", home);
- nouveau_fichier(txt, 1, "txt", varga);
- nouveau_fichier(ixxios, 0, "ixxios", home);
- nouveau_fichier(prog, 2, "help", bin);
- prog->pf = help;
- strcpy(txt->texte, "Je suis une phrase.");
- *ppwd = varga;
- }
- int main(void){
- int boucle=1;
- Fichier *pwd, root = {0, "root", "/", &root, 0};
- //auth();
- help();
- set_env(&pwd, &root);
- while (boucle){
- boucle = saisie_commande(&pwd, &root);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement