开发者

execvp function working without path

i am writing a running shell script with C programming. I have read about exec function although didn't understand much but I have read an example in which execvp is used like this

execvp(*argv, argv) ;

/* here argv is a char pointer array containing commands like ls -l

argv[0]-> ls argv[1]-> -l

*/

but it开发者_StackOverflow is used without giving file name as argument i idn't get it how it is working then . any one can please explain this as in the description it of execvp it is given to specified file name thanks so much


In your case where you are actually passing Arg1: ls Arg2: ls -l After ensuring that your arguments are not NULL, this check is done

/* If it's an absolute or relative path name, it's easy. */
if (strchr(argv[0], '/')){
    execve(argv[0],argv,environ);
}
//In your case this would fail because argv[0] is not an absolute path.
//So now the search for argv[0] begins in the PATH
path = getenv("PATH")
//Now for each directory specified in path, it will attempt an execve call as
//For simplicity, I am calling each directoryname in PATH to be dir
execve(strcat(dir,argv[0]),argv,environ)
//If this generates an error called [ENOEXEC][1], then it's okay to continue searching in other directories, else quit searching and return the errorcode

I have provided a simplified and abstract view of execvp's working. You must look through the source code to understand the internal workings better

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜