Sunday 15 July 2012

c - Pointer losing its value + execv compilation warning -



c - Pointer losing its value + execv compilation warning -

i hope haven't missed similar question.

i'm trying code mini-shell of own, using primitive c functions.

i got should work, have pointer makes bug.

my adrcmd pointer should command-path string searchcmd() function , maintain same value in main function.

in fact: points right value on searchcmd(), not in main().

here's code:

int searchcmd(char* cmd, char* adrcmd){ char* path = getenv("path"); if(debug)printf("path : %s\n", path); int nbpath = (comptelettre(path, ':')+1); char** pathtab = malloc(nbpath*sizeof(char*)); decompose(path, pathtab, 2048, ':'); int i; char* adr = malloc(sizeof(char*)); for(i=0; i<nbpath; i++){ sprintf(adr, "%s/%s", pathtab[i], cmd); if(debug)printf(" source : %s \n", adr); int fs = open(adr, o_rdonly); // si on peut ouvrir le fichier, c'est qu'il existe ! if(fs != -1){ // si le fichier existe, on le renvoie; if(debug){ printf("commande trouvée dans path ! \n"); printf("%s \n", adr); } adrcmd = adr; printf("%s ?= %s \n",adrcmd, adr );// oui homecoming 1; } } homecoming 0; } /**********************\ main \**********************/ int main(int argc, char** argv){ printf("mini-shell : ok \n"); char cmd[cmdsize]; char** splited = malloc(cmdsize*sizeof(char*)); char* adrcmd = malloc(sizeof(char*)); char* params; while(printf("$ : ") && gets(cmd) && (strcmp(cmd, "exit")!=0 && strcmp(cmd, "quit")!=0)){ // on boucle tant que la commande != "exit" ou "quit" printf("votre commande : %s \n", cmd); decompose(cmd, splited, cmdsize, ' '); if(debug)affichecmd(splited, cmdsize); if(!searchcmd(splited[0], adrcmd)){ printf("commande n'existe pas, essayez apt-get install %s\n", splited[0]); }else{ if(debug)printf("execution de la commande '%s' : \n", adrcmd); params = splited[1]; // params = array(splited[1], splited[2], ...... ) if(execv(adrcmd, params) == -1){ printf("erreur d'exection de la commande\n"); } } } printf("fin du programme %s \n", argv[0]); homecoming 0; }

here's execution returns:

$ ./a.out mini-shell : ok $ : ls /var votre commande : ls /var cmd[0] = ls cmd[1] = /var path : /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games source : /usr/lib/lightdm/lightdm/ls source : /usr/local/sbin/ls source : /usr/local/bin/ls source : /usr/sbin/ls source : /usr/bin/ls source : /sbin/ls source : /bin/ls commande trouvée dans path ! /bin/ls /bin/ls ?= /bin/ls execution de la commande '' : erreur d'exection de la commande

and while i'm here, when compile, execv returns warning:

$ gcc shell.c shell.c: in function ‘main’: shell.c:113:4: attending : passing argument 2 of ‘execv’ incompatible pointer type [enabled default] /usr/include/unistd.h:564:12: note: expected ‘char * const*’ argument of type ‘char *’

what should avoid this?

c pass value. when doing this

int searchcmd(char * cmd, char * adrcmd){

adrcmd re-create of had been passed in. overwriting re-create won't alter had been copied in caller.

to prepare pass downwards address of adrcmd:

int searchcmd(char * cmd, char ** padrcmd){

and utilize this:

*padrcmd = adr;

call searchcmd() this:

if(!searchcmd(splited[0], &adrcmd)){

and define , initialise adrcmd this;

char * adrcmd = null;

c pointers execv

No comments:

Post a Comment