execve函数的介绍与使用

#include<stdio.h>
#include<unistd.h>
int main()
{
        char *filename[]={"./BP",NULL};//BP是c文件编译链接后产生的可执行文件,目的是打印一条语句
        char *envp[]={0,NULL};//如果依赖于新环境变量,这里才需要改
        execve("/root/BP",filename,envp);//字符串是文件的路径需要具体到该运行文件名,直到文件夹貌似识别不了,或者命令的路径
        /*注意,把下面的三个注释遇到去掉,后面的ls命令也无法执行,因为*execve执行后,相当于结束了当前的程序,切换到execve里面的那个程序*去了*/
//      char *filename1[]={"ls","-al","/root",NULL};//命令和参数,记得要加NULL
//      char *envp1[]={0,NULL};
//      execve("/bin/ls",filename1,envp1);
        return 0;
}

//BP.c
#include<stdio.h>
int main()
{
        printf("this is c process
");
        return 0;
}

运行结果如下
没修改前的运行结果
[22:41:10] gcc execvetest.c -o execvetest
[22:41:18] gcc BP.c -o BP
[22:41:24] ./execvetest
this is c process
注释掉前三行,并且去掉后面的注释,运行结果如下
[22:45:05] gcc execvetest.c -o execvetest
[22:45:11] gcc BP.c -o BP
[22:45:23] ./execvetest
total 64
dr-xr-x—. 15 root root 4096 May 10 21:06 .
dr-xr-xr-x. 17 root root 224 Apr 30 02:52 ..
-rw——-. 1 root root 314 Apr 30 07:08 .ICEauthority
-rw——-. 1 root root 3769 May 10 18:33 .bash_history
-rw-r–r–. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r–r–. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r–r–. 1 root root 208 May 10 18:19 .bashrc
drwx——. 10 root root 194 Apr 30 07:09 .cache
drwx——. 15 root root 276 Apr 30 07:09 .config
-rw-r–r–. 1 root root 100 Dec 28 2013 .cshrc
drwx——. 3 root root 25 Apr 30 06:53 .dbus
-rw——-. 1 root root 16 Apr 30 07:08 .esd_auth
drwx——. 3 root root 19 Apr 30 07:08 .local
-rw-r–r–. 1 root root 129 Dec 28 2013 .tcshrc
-rwxr-xr-x. 1 root root 8512 May 10 20:55 BP
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Desktop
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Documents
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Downloads
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Music
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Pictures
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Public
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Templates
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Videos
-rw——-. 1 root root 2742 Apr 30 02:52 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 May 10 22:45 bp
-rw-r–r–. 1 root root 2750 Apr 30 06:55 initial-setup-ks.cfg
-rw——-. 1 root root 2033 Apr 30 02:52 original-ks.cfg

原文地址:https://www.cnblogs.com/biaopei/p/7730653.html