Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "ssh_admin.h"
  6.  
  7. struct Fichier{
  8.     //0: Dossier, 1: Texte, 2; Programme
  9.     int type;
  10.  
  11.     char nom[20];
  12.     char chemin[50];
  13.  
  14.     //type = 0
  15.     Fichier *pere;
  16.     int nb_fichier;
  17.     Fichier *contenu[30];
  18.  
  19.     //type = 1
  20.     char texte[200];
  21.  
  22.     //type = 2
  23.     void (*pf)();
  24. };
  25.  
  26. void cls(){
  27.     #if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
  28.         system("clear");
  29.     #endif
  30.  
  31.     #if defined(_WIN32) || defined(_WIN64)
  32.         system("cls");
  33.     #endif
  34. }
  35.  
  36. void printc(char *format, char *string, color c){
  37.     switch(c){
  38.         case red:
  39.             printf("\033[1;31m");
  40.             break;
  41.         case green:
  42.             printf("\033[1;32m");
  43.             break;
  44.         case blue:
  45.             printf("\033[1;34m");
  46.             break;
  47.         case none:
  48.             break;
  49.     }
  50.     printf(format, string);
  51.     printf("\033[0m");
  52. }
  53.  
  54. void wait(int second){
  55.     for (int i=0; i<3; i++){
  56.         printf(".");
  57.         fflush(stdout);
  58.         sleep(second);
  59.     }
  60. }
  61.  
  62. void auth(){
  63.     cls();
  64.     printf("Connexion au serveur codeanon:22");
  65.     wait(1);
  66.     printf("\nConnexion au serveur réussie !\n");
  67.  
  68.     printf("\nAuthentification en cours");
  69.     wait(1);
  70.     printf("\nConnexion en tant que admin réussie !\n");
  71.     sleep(1);
  72.     cls();
  73. }
  74.  
  75. void help(){
  76.     printf("\nListe des commandes disponibles:\n\n");
  77.     printf("\thelp\tAffiche ce message\n");
  78.     printf("\tcd\tChange de répertoire courant\n");
  79.     printf("\tls\tAffiche le contenu du répertoire courant\n");
  80.     printf("\tclear\tEfface le contenu de la console\n");
  81.     printf("\tcat\tAffiche le contenu d'un fichier texte\n");
  82.     printf("\texec\tExecute un programme\n");
  83.     printf("\tquit\tFerme la connexion avec le serveur\n");
  84.  
  85.     printf("\nLes");
  86.     printc(" %s ", "dossiers", blue);
  87.     printf("sont en bleu, les fichiers sont en blanc, les");
  88.     printc(" %s ", "programmes", green);
  89.     printf("sont en vert.\n\n");
  90. }
  91.  
  92. void ls(Fichier *pwd){
  93.     color couleur;
  94.    
  95.     for (int i=0; i < pwd->nb_fichier; i++){
  96.         switch ((pwd->contenu[i])->type){
  97.             case 0:
  98.                 couleur = blue;
  99.                 break;
  100.             case 1:
  101.                 couleur = none;
  102.                 break;
  103.             case 2:
  104.                 couleur = green;
  105.                 break;
  106.         }
  107.         printc("%s  ", (pwd->contenu[i])->nom, couleur);
  108.     }
  109.     if (pwd->nb_fichier != 0)
  110.         printf("\n");
  111. }
  112.  
  113. void cd(char *arg, Fichier **ppwd, Fichier *proot){
  114.     int argc=0, valide=1, j;
  115.     Fichier *wdtmp;
  116.    
  117.     if (arg[0] == '/')
  118.         wdtmp = proot;
  119.     else
  120.         wdtmp = *ppwd;
  121.  
  122.     char *arg_path[50];
  123.     argc = split(arg, arg_path, "/");
  124.    
  125.     for (int i=0; valide && i < argc; i++){
  126.         if (!strcmp(arg_path[i], ".."))
  127.             wdtmp = wdtmp->pere;
  128.         else
  129.         {
  130.             j=0;
  131.             while (j < wdtmp->nb_fichier && strcmp((wdtmp->contenu[j])->nom, arg_path[i]))
  132.                 j++;
  133.             if (j == wdtmp->nb_fichier)
  134.                 valide = 0;
  135.             else
  136.                 wdtmp = wdtmp->contenu[j];
  137.         }
  138.     }
  139.    
  140.     if (valide)
  141.         *ppwd = wdtmp;
  142.     else
  143.         printf("Chemin non valide\n");
  144. }
  145.  
  146. void cat(Fichier *pwd, char *arg){
  147.     int i=0;
  148.     Fichier *fichier_texte;
  149.  
  150.     while (i < pwd->nb_fichier && strcmp((pwd->contenu[i])->nom, arg))
  151.         i++;
  152.     if (i == pwd->nb_fichier)
  153.         printf("Fichier introuvable\n");
  154.     else{
  155.         fichier_texte = pwd->contenu[i];
  156.         if (fichier_texte->type != 1)
  157.             printf("Ce fichier n'est pas un fichier texte\n");
  158.         else
  159.             printf("%s\n", fichier_texte->texte);
  160.     }
  161. }
  162.  
  163. void exec(Fichier *pwd, char *arg){
  164.     int i=0;
  165.     Fichier *fichier_prog;
  166.  
  167.     while (i < pwd->nb_fichier && strcmp((pwd->contenu[i])->nom, arg))
  168.         i++;
  169.     if (i == pwd->nb_fichier)
  170.         printf("Fichier introuvable\n");
  171.     else{
  172.         fichier_prog = pwd->contenu[i];
  173.         if (fichier_prog->type != 2)
  174.             printf("Ce fichier n'est pas un programme\n");
  175.         else
  176.             fichier_prog->pf();
  177.     }
  178. }
  179.  
  180. int split(char *chaine, char **argv, char *delim){
  181.     int argc=0;
  182.  
  183.     char *pToken = strtok(chaine, delim);
  184.     while (pToken != NULL){
  185.         argv[argc] = pToken;
  186.         argc ++;
  187.         pToken = strtok(NULL, delim);
  188.     }
  189.     argv[argc] = NULL;
  190.     return argc;
  191. }
  192.  
  193. int saisie_commande(Fichier **ppwd, Fichier *proot){
  194.     int argc, boucle = 1;
  195.     char commande[100], *argv[50];
  196.      
  197.     printc("%s", "admin@codeanon", red);
  198.     printf(":");
  199.     printc("%s", (*ppwd)->chemin, blue);
  200.     printf("# ");
  201.    
  202.     fgets(commande, sizeof(commande), stdin);
  203.     argc = split(commande, argv, " \n");
  204.    
  205.     if (argc == 0)
  206.         return boucle;
  207.     else if (!strcmp(argv[0], "quit")){
  208.         if (argc != 1)
  209.             printf("Trop d'arguments !\n");
  210.         else
  211.             boucle = 0;
  212.     }
  213.     else if (!strcmp(argv[0], "help")){
  214.         if (argc != 1)
  215.             printf("Trop d'arguments !\n");
  216.         else
  217.             help();
  218.     }
  219.     else if (!strcmp(argv[0], "clear")){
  220.         if (argc != 1)
  221.             printf("Trop d'arguments !\n");
  222.         else
  223.             cls();
  224.     }
  225.     else if (!strcmp(argv[0], "ls")){
  226.         if (argc != 1)
  227.             printf("Trop d'arguments !\n");
  228.         else
  229.             ls(*ppwd);
  230.     }
  231.     else if (!strcmp(argv[0], "cd")){
  232.         if (argc == 1){
  233.             printf("La commande cd prend en argument un chemin, exemple:\n");
  234.             printf("\tcd /home/Varga\tpour aller dans le dossier nommé Varga\n");
  235.             printf("\tcd ..\t\tpour aller dans le dossie parent (précédent)\n");
  236.         }
  237.         else if (argc != 2)
  238.             printf("Trop d'arguments !\n");
  239.         else
  240.             cd(argv[1], ppwd, proot);
  241.     }
  242.     else if (!strcmp(argv[0], "cat")){
  243.         if (argc == 1){
  244.             printf("La commande cat prend en argument un fichier texte\n");
  245.         }
  246.         else if (argc != 2)
  247.             printf("Trop d'arguments !\n");
  248.         else
  249.             cat(*ppwd, argv[1]);
  250.     }
  251.     else if (!strcmp(argv[0], "exec")){
  252.          if (argc == 1){
  253.              printf("La commande exec prend en argument un fichier programme\n");
  254.          }
  255.          else if (argc != 2)
  256.              printf("Trop d'arguments !\n");
  257.          else
  258.              exec(*ppwd, argv[1]);
  259.     }
  260.     else
  261.         printf("Commande inconnue\n");
  262.  
  263.     return boucle;
  264. }
  265.  
  266. void nouveau_fichier(Fichier *pfichier, int type, char nom[], Fichier *pparent){
  267.     pfichier->type = type;
  268.    
  269.     strcpy(pfichier->nom, nom);
  270.    
  271.     strcpy(pfichier->chemin, pparent->chemin);
  272.     if (strcmp(pparent->nom, "root"))
  273.         strcat(pfichier->chemin, "/");
  274.     strcat(pfichier->chemin, pfichier->nom);
  275.  
  276.     if (type == 0){
  277.         pfichier->pere = pparent;
  278.         pfichier->nb_fichier = 0;
  279.     }
  280.     pparent->contenu[pparent->nb_fichier] = pfichier;
  281.     pparent->nb_fichier++;
  282. }
  283.  
  284. void set_env(Fichier **ppwd, Fichier *proot){
  285.     Fichier *home, *bin, *varga, *ixxios, *prog, *txt;
  286.     home = malloc(sizeof(Fichier));
  287.     bin = malloc(sizeof(Fichier));
  288.     varga = malloc(sizeof(Fichier));
  289.     txt = malloc(sizeof(Fichier));
  290.     ixxios = malloc(sizeof(Fichier));
  291.     prog = malloc(sizeof(Fichier));
  292.  
  293.     nouveau_fichier(home, 0, "home", proot);
  294.     nouveau_fichier(bin, 0, "bin", proot);
  295.     nouveau_fichier(varga, 0, "varga", home);
  296.     nouveau_fichier(txt, 1, "txt", varga);
  297.     nouveau_fichier(ixxios, 0, "ixxios", home);
  298.     nouveau_fichier(prog, 2, "help", bin);
  299.  
  300.     prog->pf = help;
  301.     strcpy(txt->texte, "Je suis une phrase.");
  302.  
  303.     *ppwd = varga;
  304. }
  305.  
  306. int main(void){
  307.     int boucle=1;
  308.     Fichier *pwd, root = {0, "root", "/", &root, 0};
  309.    
  310.     //auth();
  311.     help();
  312.     set_env(&pwd, &root);
  313.  
  314.     while (boucle){
  315.         boucle = saisie_commande(&pwd, &root);
  316.     }
  317.    
  318.     return 0;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement