Discussion:
execvp with no arguments
(too old to reply)
Ben
2004-09-22 14:20:07 UTC
Permalink
Hello,

I'm having a little bit of trouble with the execvp command. If my
understanding is correct, it takes in two parameters: the first is the
command that the user wants to execute and the second is an array of
arguments which that command will use.

It makes sense in cases where there are arguments, but what about when
there are not? For instance, what do I feed in as the second
parameter if I want to do a 'ls'?

I'd appreciate any help.

Thanks,
Ben
Eric Sosman
2004-09-22 14:51:05 UTC
Permalink
Post by Ben
Hello,
I'm having a little bit of trouble with the execvp command. If my
understanding is correct, it takes in two parameters: the first is the
command that the user wants to execute and the second is an array of
arguments which that command will use.
It makes sense in cases where there are arguments, but what about when
there are not? For instance, what do I feed in as the second
parameter if I want to do a 'ls'?
First off, normal practice calls for the presence of
at least one argument: argv[0] is, by convention, the
name of the program. So you'd have something like

char *argv[] = { "ls", NULL };
int ret = execvp("ls", argv);

However, if you really truly wanted no arguments at
all, you could try

char *argv[] = { NULL };
int ret = execvp("ls", argv);
--
***@sun.com
Ralf Fassel
2004-09-22 14:51:36 UTC
Permalink
* ***@gmail.com (Ben)
| [execvp]
| For instance, what do I feed in as the second
| parameter if I want to do a 'ls'?

man execvp:

The execv and execvp functions provide an array of point-
ers to null-terminated strings that represent the argument
list available to the new program. The first argument, by
convention, should point to the file name associated with
the file being executed. The array of pointers must be
terminated by a NULL pointer.

So the minimum requirement would be a char* array of size 2 where the
first element is a pointer to the program name and the second is NULL.

R'
Jonathan Adams
2004-09-22 15:02:08 UTC
Permalink
Post by Ben
Hello,
I'm having a little bit of trouble with the execvp command. If my
understanding is correct, it takes in two parameters: the first is the
command that the user wants to execute and the second is an array of
arguments which that command will use.
It makes sense in cases where there are arguments, but what about when
there are not? For instance, what do I feed in as the second
parameter if I want to do a 'ls'?
In Unix, by convention, the 'zeroth' argument to a command is the name
of the command. For exec*p(2), this is usually the same as the first
argument. So the code typically looks like:

err = execlp("ls", "ls", NULL);

or for the case you asked:

char *args[2];
args[0] = "ls";
args[1] = NULL;

err = execvp("ls", args);

Actual command line arguments go after the command name, like:

err = execlp("ls", "ls", "-l", NULL);

for the same effect as typing "ls -l" into your shell.

Not following this convention is liable to lead to core dumps and other
confusion, since many utilities use argv[0] without first checking that
it is non-NULL.

Cheers,
- jonathan
Bård Skaflestad
2004-09-22 15:56:47 UTC
Permalink
Post by Jonathan Adams
Post by Ben
Hello,
I'm having a little bit of trouble with the execvp command. If my
understanding is correct, it takes in two parameters: the first is
the command that the user wants to execute and the second is an
array of arguments which that command will use.
It makes sense in cases where there are arguments, but what about
when there are not? For instance, what do I feed in as the second
parameter if I want to do a 'ls'?
In Unix, by convention, the 'zeroth' argument to a command is the
name of the command. For exec*p(2), this is usually the same as the
err = execlp("ls", "ls", NULL);
Almost, but not quite. To be *truly* pedantic about it, execlp() is a
variadic function requiring at least two arguments. Any further
arguments *must* have the correct type to avoid undefined behaviour.

In this case, all additional argument *must* be char*-pointers. In
particular this means that the terminating NULL argument must be a
char*-pointer. However, in general you cannot know the exact type of
the null pointer constant represented by NULL so you will have to
convert the pointer to a proper char*-pointer.

This all amounts to casting NULL to (char *) in the function call, so
the above line should be written

err = execlp("ls", "ls", (char *)NULL);

or (small) variations thereof.

In the case of execvp(), however, the second argument is as the OP
correctly noted an array of char*-arguments terminated by NULL. Then
NULL will be implicitly converted to a proper null char*-pointer and
the cast is no longer required.

In practice, the call without the cast may (and in many cases will)
work correctly all the time, but the cast is required in order to be
portable to other systems.


Regards,
--
Bård Skaflestad <***@math.ntnu.no>
Department of mathematical sciences, NTNU
Alfred Getz v. 1, No-7034 Trondheim, Norway
Tel: +47 7359 3546 <URL:http://www.math.ntnu.no/~bardsk/>
Loading...