转自:http://bachue.is-programmer.com/posts/21611.html
http://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/execlp.htm
execlp(从PATH 环境变量中查找文件并执行)
/* 执行ls -al /etc/passwd execlp()会依PATH 变量中的/bin找到/bin/ls */ #include<unistd.h> main() { execlp(“ls”,”ls”,”-al”,”/etc/passwd”,(char *)0); } //执行 //-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
然后Google了下,看到了www.lslnet.com/linux/f/docs1/i34/big5260214.htm中别人同样的质疑。
这张帖子的楼主索性猜测:execlp函数在实际执行的时候根本没有用到第二个参数。
说真的,这个猜测过于草率了。
楼下有一张回帖给出了一个反例:execlp("ls","flw","-?",(char *)0)。
应该是execlp("ls","flw","--help",(char *)0)吧,ls是Linux命令,-?是Windows的写法,不能同时运用,疑为他的笔误。这句语句在我电脑上的效果:
Usage: flw [OPTION]... [FILE]...
int main(int num_args,const char* args[]) { for(int i=0;i<num_args;++i) { std::cout << args[i] << std::endl; } return 0; }
而execlp("ls","flw","--help",(char *)0)出现Usage: flw,是因为ls命令在显示帮助的时候会动用args[0], 这么设计很正常,因为如果用户打错了命令,总希望用最接近他命令的方式指导他怎么做才对,直接使用他的命令是最方便的。平时args[0]几乎总是等于 ls,这是因为ls在PATH中,没必要指示他的位置,但如果使用/bin/ls这样的命令,就可以看出来了。
execlp -- Overlay Calling Process and Run New Program
SYNOPSIS
#include <unistd.h> int execlp(const char *path, const char *arg0, ..., NULL);
DESCRIPTION
Like all of the exec
functions, execlp
replaces the calling process image with a new process image. This has the effect of running a new program with the process ID of the calling process. Note that a new process is not started; the new process image simply overlays the original process image. The execlp
function is most commonly used to overlay a process image that has been created by a call to the fork
function.
path
- identifies the location of the new process image within the hierarchical file system (HFS). If the
path
argument contains a slash (/
), it is assumed that either an absolute or a relative pathname has been specified. If thepath
argument does not contain a slash, the directories specified by thePATH
environment variable are searched in an attempt to locate the file. arg0, ..., NULL
- is a variable length list of arguments that are passed to the new process image. Each argument is specified as a null-terminated string, and the list must end with a
NULL
pointer. The first argument,arg0
, is required and must contain the name of the executable file for the new process image. If the new process image is a normal SAS/Cmain
program, the list of arguments will be passed toargv
as a pointer to an array of strings. The number of strings in the array is passed to themain()
function asargc
.ARG_MAX
specifies the maximum number of bytes, including theNULL
terminator at the end of the string, that can be passed as arguments to the new process image. The value ofARG_MAX
is obtained by calling thesysconf
function with the_SC_ARG_MAX
symbol.
RETURN VALUE
A successful call to execlp
does not have a return value because the new process image overlays the calling process image. However, a -1
is returned if the call to execlp
is unsuccessful.
EXAMPLE
The following example illustrates creating a new process and executing an HFS file called newShell
. The path /u/userid/bin
is added at the end of the PATH
environment variable before calling execlp
.
Note: You must specify the posix
option when compiling this example.
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> main() { pid_t pid; char *pathvar; char newpath[1000]; pathvar = getenv("PATH"); strcpy(newpath, pathvar); strcat(newpath, ":u/userid/bin"); setenv("PATH", newpath); if ((pid = fork()) == -1) perror("fork error"); else if (pid == 0) { execlp("newShell", "newShell", NULL); printf("Return not expected. Must be an execlp error.n"); } }
RELATED FUNCTIONS
execl
, execvp