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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#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=NULL;
char *tampon=NULL;
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];
char *buffer;
char *c;
char sep[]="\t;|\n";
printf("my shell >>");
scanf("%[^\n]",comand);
char *comdup=strdup(comand);
c = strtok_r(comdup,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,a=0;
char chaine[20]="";
char *lien[40];
while (j != i)
{
while ((s != k)&&(acces == 1))
{
printf("%d,%d,%d,%d\n",i,j,s,k);
strcat(chaine,tlien[j]);
strcat(chaine,"/");
printf("%s\n",chaine); //****************** juste pour le test
char *p;
char *buf;
char *dup;
if (tab[s]!=NULL)
dup = strdup(tab[s]);
else
break;
p = strtok_r(dup," ",&buf);
strcat(chaine,p);
printf("%s\n",chaine); //********************** juste pour le test
if (access(chaine,F_OK)==0)
{
acces= 0;
lien[a]=chaine; //la commande existe ************************** lien[a]=&chaine
a++;
}
strcpy(chaine,"");
s++;
}
if (acces == 0)
{
j++;
s=0;
acces = 1;
}
else
{
printf("commande inexistante \n");
break;
}
}
return 0;
} |
Partager