1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main ()
{//mettre les lien de "PATH" dans le tableau tlien
char *path = getenv("PATH");
char *p,*tampon;
p = strtok_r(path,":",&tampon);
char **tlien[10];int k=0;
while (p != NULL)
{ tlien[k]=&p;
//printf("%s\n",*tlien[k]);
k++;
p = strtok_r(NULL,":",&tampon);
} //lire la ligne de commande et mettre chaque commande dans le tableau tab;
char comand[80],*buffer,*c,sep[]="\t;|\n";
printf("my shell >>");
scanf("%[^\n]",&comand);
c = strtok_r(comand,sep,&buffer);
char **tab[10];int i=0;
while (c != NULL)
{
tab[i]=&c;
//printf("%s\n",c);
//printf("%s\n",*tab[i]);
i++;
c = strtok_r(NULL,sep,&buffer);
}
//vérification de l'existance des commandes tapées
int j=0,s=0,acces=1;
char chaine[20];
while (j != i)
{
while ((s != k)&&(acces == 1))
{
strcat(chaine,*tlien[j]);
strcat(chaine,"/");
char *p,*buf,*dup;
dup = strdup(*tab[s]);
if (dup==NULL) dup = *tab[s];
p = strtok_r(dup," ",&buf);
strcat(chaine,p);
printf("%s\n",chaine);
if (access(chaine,F_OK)==0) acces= 0; //la commande existe
//for(int r=0;r<20;r++) chaine=NULL; *************************
}
if (acces == 0) { j++; s=0;acces = 1;}
else {printf("commande inexistante \n");break;}
}
return 0;
} |
Partager