execlp()
头文件
#include <unistd.h>
函数原型
int execlp(const char * file,const char * arg, ...)
unix环境高级编程的例子
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[4096];
pid_t pid;
int status;
printf("%% ");
while(fgets(buf, 4096,stdin) != NULL)
{
if(buf[strlen(buf) - 1] == '\n')
{
buf[strlen(buf) - 1] = 0;
}
if((pid = fork()) <0)
{
printf("fork error\n");
}
else if(pid == 0)
{
execlp(buf, buf, (char *)0);
printf("couldn't execute: %s", buf);
exit(127);
}
if((pid = waitpid(pid, &status, 0)) < 0)
{
printf("waitpid error");
}
printf("%% ");
}
exit(0);
}
功能
execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名, 找到后便执行该文件, 然后将第二个以后的参数当做该文件的argv[0]、argv[1]……, 最后一个参数必须用空指针(NULL)作结束。