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
0
votes
2 answers

c++ unzip returns cannot create extraction directory

I have written a simple piece of code for extracting zip files using unzip. It works fine when output directory is not set but returns error is directory is set "Archive: /home/vishvesh.kumar/tempFolder/test.zip checkdir: cannot create…
Vishvesh
  • 512
  • 8
  • 21
0
votes
1 answer

How can i pass all files with full path in a directory to the argument array in execv?

I'm trying to read all files (pictures) in a given directory and pass it to execv. I want to start the framebuffer image viewer (fbi). For now it works when i start it from the directory the pictures are in. How can i manipulate the vector so it not…
user2037974
0
votes
1 answer

Add directory to the PATH for execvp()

I am writing a small console-like program in C, along with implementations for ls, tac and dir. I use execvp() function to override the forked children of the process, but when the user inserts one of the above 3 commands, I have to execute my own…
Cristian Holdunu
  • 1,880
  • 1
  • 18
  • 43
0
votes
1 answer

C++ invoke the java jar

I want use execvp to invoke java to execute a function, like the code below : #include #include using namespace std; int main() { char* const argv[]= { …
greatsea
  • 87
  • 1
  • 9
0
votes
1 answer

execvp not returning even though file doesn't exist

I'm writing a little shell for class. I call execvp for non-built-in commands. For a while in developing this, it returned normally with -1 and all was right. Now, however, I can type anything in to the shell, it starts a process which immediately…
TravisThomas
  • 611
  • 1
  • 8
  • 21
0
votes
1 answer

freopen and execvp in c

I am doing a minor shell implementation in c, and I am stuck. I want to be able to redirect stdin and stdout, but I am confused. In, my shell, when I want to start a program, I use the execvp function. Now I would like to be able to redirect stdout,…
user1090614
  • 2,575
  • 6
  • 22
  • 27
0
votes
1 answer

Set ignored signals to SIG_DFL before execvp()

I'm writing a library where I fork() and execvp() to start another program. As I've understood, execvX() resets "custom" signal handlers to SIG_DFL but it doesn't set any ignored signals handlers to SIG_DFL. Keeping in mind it is a library and I…
Víctor Fernández
  • 1,754
  • 1
  • 16
  • 15
0
votes
1 answer

execvp and me; How can I make it work for me?

I have created a shell, and it works when I use system(1), but the specs say not to. I am trying to use execvp at the end and I'm not really too sure on how to do it. Any chance of help would be appreciated. Code -> char *token = NULL; char…
Sam P
  • 453
  • 6
  • 19
0
votes
1 answer

execvp does not get argument

My code is: execvp(command, args); args is char* args[] command is char* command for example for ls -l, command is ls and args is -l but why after executing execvp(command, args)I get only result of ls not ls -l??
Sara
  • 2,308
  • 11
  • 50
  • 76
0
votes
1 answer

fork() and execvp()

I'm having problems trying to execute a process using fork() and execvp(). I have a struct Pcb which has an array of arguments (args): #define MAXARGS 2 struct pcb { pid_t pid; // system process ID char *args[MAXARGS]; //…
Milk
  • 647
  • 1
  • 6
  • 18
0
votes
1 answer

search for app inside PATH environment in c

I'm trying to execute apps in c by name, before the run I want to find out if the name are legal. is there any way to check in c, and find out if the app exist in the PATH environment? thanks
hillel_guy
  • 646
  • 6
  • 17
-1
votes
1 answer

Running a Python program from a C program using system()

I am tasked with creating a C program that writes a python program (which prints "hello world") and executes it. I have completed the first part (writing the python program to a file) but when I try to execute the python program from C, nothing…
-1
votes
1 answer

Why would my execvp() not work in following code?

I am trying to make use of execvp() to execute child processes but it gives me following error and I am unable to fix it can someone point me in direction what I am doing wrong? My code: #include "apue.h" #include static void …
tablemesa
-1
votes
1 answer

Why isn't my C shell outputting anything?

Working on a project for a class. We're supposed to write a C shell. I've found a bunch of good examples, but for the life of me, I can't get my version to generate any output. It keeps printing the prompt, but nothing else. I've followed along with…
ZeroKelvin
  • 89
  • 8
-1
votes
1 answer

Emulating execvp - Is There a Better Way To Do This?

I'm currently wrapping a command line tool (espeak) with Tcl/Tk, and I have figured this out so far: load ./extensions/system.so package require Tk package require Tclx set chid 0 proc kill {id} { exec kill -9 $id } proc speak {} { global…
new123456
  • 873
  • 1
  • 12
  • 21