本文介绍了指针失去其价值+ execv编译警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我没有错过过类似的问题。

I hope I haven't missed a similar question.

我想code我自己的一个小型外壳,采用原始的C函数。

I'm trying to code a mini-shell of my own, using primitive C functions.

我得到的东西应该工作,但我有一个指针,使一切错误。

I got something that should work, but I have a pointer that makes everything bug.

我的 adrCmd 指针应该从 searchCmd()函数命令路径字符串,并保持相同的值在功能。

My adrCmd pointer should get the command-path string from the searchCmd() function and keep the same value in the main function.

在的事实:它指向右边值 searchCmd(),但不是在的main()

In fact: it points to the right value on searchCmd(), but not in the main().

这里的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
            return 1;
        }
    }
    return 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]);
    return 0;
}

下面是执行返回的内容:

Here's what the 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

虽然我在这里,当我编译, execv 返回警告:

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

我应该怎么做才能避免这种情况?

What should I do to avoid this?

推荐答案

C是传值。因此,执行此操作时

C is pass by value. So when doing this

 int searchCmd(char * cmd, char * adrCmd){

adrCmd 是怎样在了已经通过的副本。覆盖副本不会改变什么已经从来电者复制。

adrCmd is a copy of what had been passed in. Overwriting the copy won't change what it had been copied from in the caller.

要解决这个向下传递的地址 adrCmd

To fix this pass down the address of adrCmd:

 int searchCmd(char * cmd, char ** padrCmd){

和使用这样的:

    *padrCmd = adr;

呼叫 searchCmd()是这样的:

  if(!searchCmd(splited[0], &adrCmd)){

和定义和初始化 adrCmd 这样的;

and define and initialise adrCmd like this;

  char * adrCmd = NULL;

这篇关于指针失去其价值+ execv编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:16