I know execvp can be used to execute simple commands as follows:
char* arg[] = {"ls", "-l", NULL};
execvp(arg[0],arg);
I want to know what goes on in here when I run execvp. In man page it says execvp replaces the image of the process image with the new one. However here I am running a command not an executable.
To be specific, say there is a command that specifically requires an input e.g. cat. If I have a text file text.txt which contains the file name expected for cat and I redirect stdin to the file stream of the file, would the output of execle("cat","cat",NULL) or execvp("cat", arg) (obviously where arg stores "cat" and NULL) result in the output in the console as the cat /filename would? My intuition is I have to read the file and may be parse it to store the arguments in the arg. However I want to make sure.
Thanks in advance!
Here’s what happens in an
execvpcall:PATH, if applicable, for the file that is to be executed. Most, if not all, commands in UNIX-like systems are executables. What will happen if it is not? Try it. Have a look at how glibc does it.execvewill be made. Parts ofexecvemay be implemented in libc or it may be a system call (like in Linux).execvpcall, finds a handler appropriate for loading the binary, and sets the current task (theexecvpcaller) as not executing. You can find its implementation here.All steps above conform to the requirements set by POSIX which are described in the relevant manual pages.