Petite consol

Description

Salut !
Je debute dans le C et pour m'apprendre a gérré toute sorte de fonctions, j'ai créé une petite consol, un espece de MS-DOS en beacoups plus petit car elle ne contient que 4 fonction ^^

Source / Exemple :


EXPLORER.C :
¯¯¯¯¯¯¯¯¯¯
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include "dir.h"

int main(int argc, char *argv[])
{
    char tmp[30],tmp2[20];
    char d2[200] = "c:/"; 
    printf("Tapez \"help\" pour connaitre la liste des fonctions disponibles\n");
    while(1)
    {
        printf("%s",d2);
        gets(tmp);
        
        if(tmp[0] == 'd' && tmp[1] == 'i' && tmp[2] == 'r')
        {
            dir(&d2[0],tmp);
        } else if(tmp[0] == 'c' && tmp[1] == 'd')
        {
            cd(&d2[0],tmp);
        } else if(!strcmp(tmp, "exit"))
        {
            exit(0);
        } else if(tmp[0] == 's' && tmp[1] == 'e' && tmp[2] == 'a' && tmp[3] == 'r'&& tmp[4] == 'c'&& tmp[5] == 'h')
        {
            search(&d2[0],tmp);
        } else if(tmp[0] == 'h' && tmp[1] == 'e' && tmp[2] == 'l' && tmp[3] == 'p')
        {
            help(&d2[0],tmp);
        }else
        {
            printf("\nCette commande n'existe pas\n");
        } 
        
    }       
    return 0;
}

DIR.H :
¯¯¯¯¯
#include "cmp.h"

help(char *d2, char tmpcd[30])
{
    char *tmpcd2;
    tmpcd2 = strchr(tmpcd,' ');
    if (tmpcd2 == NULL)
    {
        tmpcd2 = "rien";
    }
    else
    {    

  • tmpcd2++ = 0;
} if(!strcmp(tmpcd2, "rien")) printf("Liste des fonctions disponibles :\n- cd\n- dir\n- search\n- exit\nPour en savoir plus sur une de ces fonctions : \"help <nom_de_la_fonction>\"\n"); else if(!strcmp(tmpcd2, "cd")) printf("Fonction cd : \"cd <nom_du_dossier>\" ex : 'cd program files'\n"); else if(!strcmp(tmpcd2, "dir")) printf("Fonction dir : \"dir\" ou \"dir <Filtre>\" ex : 'dir' ou 'dir *.exe'\n"); else if(!strcmp(tmpcd2, "search")) printf("Fonction search : \"search <Filtre>\" ex : 'search *.exe'\n"); else if(!strcmp(tmpcd2, "exit")) printf("Fonction exit : \"exit\" ex : 'exit'\n"); } scan() { WIN32_FIND_DATA File; HANDLE liste; liste = FindFirstFile("*.*", &File); do { if(File.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // ==> Repertoire { printf("<REP>\t%s\n",File.cFileName); } else // ==> Fichier { printf("\t%s\t%d,%d Ko\n", File.cFileName, // Nom (File.nFileSizeHigh*MAXDWORD+File.nFileSizeLow)/1024, //taille (File.nFileSizeHigh*MAXDWORD+File.nFileSizeLow)%1024); } } while((FindNextFile(liste, &File))); // FindClose() ferme la recherche FindClose(liste); printf("\n"); } void parent(char *d2) { int a,b; a=b=0; while(d2[a] != 0) { if(d2[a] == d2[2] && d2[a+1] != 0) { b=a; } a=a+1; } d2[b+1]=0; } dir(char *d2, char tmpcd[30]) { char *tmpcd2; tmpcd2 = strchr(tmpcd,' '); if (tmpcd2 == NULL) { tmpcd2 = "*.*"; } else {
  • tmpcd2++ = 0;
} SetCurrentDirectory (d2); WIN32_FIND_DATA File; HANDLE liste; liste = FindFirstFile(tmpcd2, &File); do { if(File.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // ==> Repertoire { printf("<REP>\t%s\n",File.cFileName); } else // ==> Fichier { printf("\t%s\t%d,%d Ko\n", File.cFileName, // Nom (File.nFileSizeHigh*MAXDWORD+File.nFileSizeLow)/1024, //taille (File.nFileSizeHigh*MAXDWORD+File.nFileSizeLow)%1024); } } while((FindNextFile(liste, &File))); // FindClose() ferme la recherche FindClose(liste); printf("\n"); } cd(char *d2, char tmpcd[30]) { char *tmpcd2; tmpcd2 = strchr(tmpcd,' '); if (tmpcd2 == NULL) { printf("\nCette commande n'existe pas\n"); return(0); }
  • tmpcd2++ = 0;
strcat(d2, tmpcd2); strcat(d2, "/"); if ((SetCurrentDirectory (d2)) == 1 && strcmp(tmpcd2, ".")) { if (!strcmp(tmpcd2, "..")) { parent(&d2[0]); parent(&d2[0]); } } else { printf("Le dossier %s n'existe pas.\n", tmpcd2); parent(&d2[0]); } } search(char *d2, char tmpcd[30]) { char d3[2000][1000]; char *tmpcd2; int i,j,y; tmpcd2 = strchr(tmpcd,' '); if (tmpcd2 == NULL) { printf("Erreur de syntaxe, \"search <nom>\"\n"); return(0); } else {
  • tmpcd2++ = 0;
} strcpy(d3[0],d2); j=i=y=0; while(strcmp(d3[0], "")) { SetCurrentDirectory (d3[j]); WIN32_FIND_DATA File; HANDLE liste; liste = FindFirstFile("*", &File); //tmpcd2 est le fichier a trouvé do { if(File.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // ==> Repertoire { if(strcmp(File.cFileName, "..") && strcmp(File.cFileName, ".")) { i++; //printf("<REP>\t%s\n",File.cFileName); strcat(d3[i], d3[0]); strcat(d3[i], File.cFileName); strcat(d3[i], "/"); if(cmp(File.cFileName, tmpcd2)==0) { y++; printf("%s%s/\n",d3[0],File.cFileName); } } } else if(cmp(File.cFileName, tmpcd2)==0) // ==> Fichier { printf("%s%s\n",d3[0],File.cFileName); y++; } }while((FindNextFile(liste, &File))); FindClose(liste); while(strcmp(d3[j], "")) { strcpy(d3[j],d3[j+1]); j++; } j=0; i--; } printf("%d fichier(s) trouvé\n",y); printf("\n"); } CMP.H : ¯¯¯¯¯ int cmp(char a[],char b[]) { int i,j; j = 0; char *y, *c; for (i=0;i<strlen(a);i++) { if (a[i] > 64 && a[i] < 91) a[i] = a[i] + 32; } for (i=0;i<strlen(b);i++) { if (b[i] > 64 && b[i] < 91) b[i] = b[i] + 32; } for (i=0;i<strlen(b);i++) { if (b[i] == '*') { if (b[i+1]!=0) { if ((c = strchr(&b[i+1],'*')) == NULL) { if (!strcmp(&a[strlen(a)-strlen(&b[i+1])],&b[i+1])) return(0); else return(3); } } else { return(0); }
  • c = 0;
if( (y = strpbrk(&a[j], &b[i+1])) == NULL ) return(1); printf("\t%i",y); j=y-a+strlen(&b[i+1]); i=i+strlen(&b[i+1]); y=NULL; if (c != NULL)
  • c = '*';
} else { if ((c = strchr(&b[i+1],'*')) == NULL) { if(!strcmp(a,b)) return(0); else return(22); } if(a[j] != b[i]) return(2); j++; } } return(0); }

Conclusion :


J'attend avec impatience vos reactions ;)

Ps : Si l'un de vous sais comment ajouté une fonction pour se connecter a des FTP et pouvoir naviger sur les FTP comme je le fait ici dans le disque dur local, qu'il n'esite pas

Merci

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.