execve - No such file or directory?

前端 未结 2 1930
臣服心动
臣服心动 2021-01-14 13:20

I\'m having some problems with execve. I\'m trying to make a shell that can function just like the bash shell, but I have problems with the forked child executing a command.

2条回答
  •  情话喂你
    2021-01-14 14:12

    The first thing I would do would be to insert:

    printf ("[%s]\n", path);
    

    before the call to execve. That should confirm that the executable is what you think it is.

    That code of yours looks okay as long as the input you're feeding into it is correct and the executable actually is available. For example, the following complete program works fine on my Debian box:

    #include 
    #include 
    #include 
    
    int main (int argc, char *argv[]) {
        if (argc > 1) {
            char * word = strtok (argv[1], " ");
            char path[128] = "/bin/";
            strcat (path, word);
    
            char * newenvp[] = { NULL };
            char * newargv[] = { path, NULL };
            printf ("[%s]\n", path);
            int ret = execve (path, newargv, newenvp);
            if (ret == -1) {
                perror("execve error");
            }
        }
        return 0;
    }
    

    outputting, when I run ./testprog ls, something along the lines of:

    [/bin/ls]
    kidsshares.ods  paxwords    birthdays    homeloantracking.gnumeric
    shares2011.ods  backup0.sh  development  lexar
    accounts.ods    backup1.sh  photos       testprog.c
    testprog
    

提交回复
热议问题