Questions tagged [execvp]

execvp, is a POSIX specified function from the exec* family, which replaces the current process with one specified. Questions using this tag should be related to the use and semantics of this function.

execvp is a POSIX function, from the exec* family, which replaces the current process with one specified.

It is available on most every UNIX-like platform, including Linux and Mac OS X, where a unistd.h file exists, and many languates, such as Python, emulate this function on platforms, such as Windows, where this function does not normally exist. Many languages, including C, C++, and Python, have interfaces to this function.

If it succeeds, it will not return and the current process will be replaced. If it does not succeed, then it will return a -1.


POSIX specifies that this function has the signature:

int execvp(const char *path, char *const argv[]);.

Its arguments are interpreted as follows.:

  • path: This is a NULL terminated string which holds the name of an executable. It is specified that execvp will search the PATH environment variable for this name if it is not a path.

    If it is a path, then execvp will look for the executable where specified, using an absolute path if this argument begins with a /, or a relative one if this argument doesn't.

Example: ping will resolve to /bin/ping.

  • argv: This is an array of NULL terminated strings, which make up the argv of the process in the path argument.

    This array should end with a NULL, and should start with the name of the executable passed in the path argument.

Example: {"ping", "-c", "5", "www.google.com", NULL}


POSIX includes the following complete example for reference:

The following example illustrates the use of execvp to execute the ls shell command:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main()
{    
     pid_t pid;
     char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};

     if ((pid = fork()) == -1)
          perror("fork() error");    
     else if (pid == 0) {
          execvp("ls", parmList);
          printf("Return not expected. Must be an execvp() error.\n");    
     }
}
368 questions
4
votes
3 answers

C++ const char* To const char* const

I am currently writing an assignment for my class that is supposed to act as a very basic shell. I am nearly finished, but I am running into an issue with execvp and my character array of parameters. Here is a light snippet of my code. //Split the…
Zerocaliber
  • 67
  • 1
  • 4
4
votes
4 answers

C Test For File Existence Before Calling execvp

I'm writing a UNIX minishell on ubuntu, and am trying to add built-in commands at this point. When it's not a built-in command I fork and then the child executes it, however for built-in commands I'll just execute it in the current process. So, I…
robins35
  • 653
  • 12
  • 37
3
votes
1 answer

Piping between several processes in C

I'm writing a shell in C and am trying to implement multiple pipes. I've done this by creating a two dimensional array with pipes and using a separate pipe everytime. All commands in between pipes are separated by a parsing function and put into a…
3
votes
1 answer

execvp() with grep that has quotation marks

I did 'grep' in my own little shell. Other commands, like 'ls -al', 'pwd' ..etc, is working. But When I put 'grep', there was some problems. char* f_grep[] = {"grep", "-n", "a", "a.txt", NULL}; char* s_grep[] = {"grep", "-n", "'a'", "a.txt",…
Jason
  • 33
  • 5
3
votes
1 answer

C: Shell program receiving anomolous extra operands

I am creating my own shell in C using fork and execvp. I am parsing the cmd and it's arguments using strtok. Printing the parsed tokens confirms to me that I am indeed receiving all the arguments, and the shell generally works, though it is very…
Totem
  • 7,189
  • 5
  • 39
  • 66
3
votes
1 answer

cannot convert ‘const char*’ to ‘char* const*’

I am trying to implement a shell. I will be creating a hist array to store the last 10 commands, that I want to be able to retrieve later on in order to execute. So, I am trying to find a way to get all the command line arguments back once I store…
H.K
  • 125
  • 3
  • 15
3
votes
2 answers

How to create hard link in Linux from a C program

We know we can create hard link in Linux using ln file1 file2 which will make file2 a hard link of file1. However when I try to do this by using a C program, I face issues. Below is the C…
sps
  • 2,720
  • 2
  • 19
  • 38
3
votes
1 answer

execvp failing with multiple or no arguments

I'm working on a pretty basic UNIX shell in C. In this project I am attempting to use fork() and execvp() to execute the actual shell commands. I'm running into an issue though, where it seems to work fine with commands that have a single argument…
sven
  • 121
  • 4
  • 11
3
votes
2 answers

execvp filepath clarification

I'm writing a shell in C, and I'm having trouble understanding the filepath parameter needed for execvp(filepath,argv). If the user typed wanted to run ls -a in their current directory ... let's say /home/user1 ... what would be the filepath and…
Peter Chappy
  • 1,165
  • 4
  • 22
  • 41
3
votes
3 answers

How to make find -name to return error if no file found

I have tried really hard but could not figure out a way to print an error if find -name \"filename" does not find any file. The code I am using so far is as follows: char *argv[]; argv[0]="find"; argv[1]="-name"; …
husnain
  • 31
  • 1
  • 2
3
votes
5 answers

Trying to use execvp() in C with user input in unix

I'm trying to make a program that will prompt the user for a command, then use exec to execute that command. For instance if they gave me "ls -la" I would have to execute that command. I've tried the following code: #include #include…
Nick
2
votes
1 answer

Does the argument list pass the string quotes to exec command in C?

I am using execvp for execing a new process for the command grep -l night *. Here is my code: char * argument[5]; char keyword[] = "night"; argument[0] = (char *) malloc (sizeof(char)*25); argument[1] = (char *) malloc…
sragvuw
  • 33
  • 5
2
votes
1 answer

why calling cd shell command through system() or execvp() from a child process won't work?

I understand that i am supposed to use chdir() but I just need an explanation as to why calling cd shell command through system or execvp() from a child process would not work? Thanks!!
pnizzle
  • 6,243
  • 4
  • 52
  • 81
2
votes
0 answers

How to best share an array between parent and child processes after execvp()?

Suppose you have an array A shared in memory between processes X and Y, where X is focused on the first half of the array and Y is focused on the second half. Could X sort its half of the array "in place" on A at the same time as Y sorts its half?…
zq_Aux
  • 21
  • 7
2
votes
1 answer

execvpe argv to parameter matching syntax help needed

I get "passing argument 2 of ‘execvp’ from incompatible pointer type" and expected ‘char * const*’ but argument is of type ‘const char **’ I'm wondering what the correct syntax is? Thanks! int main(int argc, const char* argv[]) { …
Josh
  • 142
  • 2
  • 11
1 2
3
24 25