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 103 104 105 106 107 108 109 110 111 112
| #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
//DEUX façons de déclarer le MAIN :
// - int main (int argc, char **argv)
// - int main (void)
// et non pas int main() !!!
//fais un éffort de présentation s'il te plait !
int main (void)
{
char *pStringSeg;
char *tampon;
char **tlien[10];
int nCmdTotal=0,j=0,s=0,acces=1,nbCommand=0;
int cmdCounter=0;
char *lien[40];
char *comdup;
char **tab[10];
//tu met tout les 'lien' de PATH dans path
#define path (char *) comdup
path = getenv("PATH");
pStringSeg = strtok_r(path,":",&tampon);
#undef path
//on test pStringSeg sinon on continuerai probablement avec des conneries...
if(!pStringSeg) return -1;
//mettre les lien de "PATH" dans le tableau tlien
while (pStringSeg != NULL)
{
//met le nCmdTotal'ieme' lien dans le tableau
tlien[nCmdTotal]=&pStringSeg;
nCmdTotal++;
//passe au lien suivant
pStringSeg = strtok_r(NULL,":",&tampon);
}
//lire la ligne de commande et mettre chaque commande dans le tableau tab;
#define command (char *)tampon
command=(char*)calloc(80,sizeof(char));
if(!command)return -1;
printf("my shell >>");
//si l'entrée standard et remplace le retour chariot par un 0.
fgets(command,79,stdin);
pStringSeg=strchr(command,'\n');
if(pStringSeg) pStringSeg='\0';
//vide le buffer d'entrée standard.
fflush(stdin);
//strdup retourne une adresse. donc pas *comdup=...
comdup=strdup(comand);
if(!comdup) return -1;
//on libère l'espace mémoire
free(command);
#undef command
pStringSeg = strtok_r(comdup,"\t;|\n",&tampon);
while (c != NULL)
{
tab[cmdCounter]=&pStringSeg;
printf("%s\n",pStringSeg);
printf("%s\n",*tab[cmdCounter]);
cmdCounter++;
pStringSeg = strtok_r(NULL,"\t;|\n",&tampon);
}
//vérification de l'existance des commandes tapées
memset(lien[nbCommand],'\0',20);
while (j != cmdCounter)
{
while ((s != nCmdTotal)&&(acces == 1))
{
printf("%d,%d,%d,%d\n",cmdCounter,j,s,nCmdTotal);
strncat(lien[nbCommand],*tlien[j],strlen(*tlien[j]));
strncat(lien[nbCommand],"/",strlen("/");
comdup = strdup(*tab[s]);
if (comdup==NULL) comdup = *tab[s];
pStringSeg = strtok_r(comdup," ",&tampon);
strncat(lien[nbCommand],pStringSeg,strlen(pStringSeg));
if (access(lien[nbCommand],F_OK)==0)
{
acces= 0;
nbCommand++;
}
memset(lien[nbCommand],'\0',20);
s++;//je met ça mais après moi je sais pas trop peut-être veux-tu continuer perpetuellement ?
}
if (acces == 0)
{
j++;
s=0;
acces = 1;
}
else
{
printf("commande inexistante \n");
break;
}
}
return 0;
} |