linux 下的execle()函数

程序中见到这么一句话

    if (interp == NULL) {
      execle(cmd, cmd, (char *) 0, envp); /* (char *) 0 to squash warning */
    } else {
/*changed start*/
 interp = "/home/linaro/http/tool/uav.cgi";
 /*envp[20] = '\0'; */
/*changed end*/
 execle(interp, interp, cmd, (char *) 0, envp);
    }
这句话的意思应该是如果interp为空,执行cmd,并传递环境参数,否则执行cgi.

那么来看看这个execle()函数的原型,

int execle(const char *path, const char *arg, ..., char * const envp[]);



示例程序:execle.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    char * const envp[] = {"AA=11", "BB=22", NULL};
    printf("Entering main ...\n");
    int ret;
    //ret =execl("./hello", "hello", NULL);
    ret =execle("./hello", "hello", NULL, envp);
    if(ret == -1)
        perror("execl error");
    printf("Exiting main ...\n");
    return 0;
}

hello.c

#include <unistd.h>
#include <stdio.h>
extern char** environ;

int main(void)
{
    printf("hello pid=%d\n", getpid());
    int i;
    for (i=0; environ[i]!=NULL; ++i)
    {
        printf("%s\n", environ[i]);
    }
    return 0;
}

12235438-4c0f2b75cc77413da14b1db3d3bd5141.png


sitemap