[cs631apue] Question about execvp(3)
kthompso
kthompso at stevens.edu
Fri Dec 16 02:41:16 EST 2016
Hey Bo,
Even using the -c option, I think the first argument passed to the
command (via
the second parameter to execvp(3)) should be the program name. So, at
the minimum
you probably want to create a **char whose first element is the program
name and
then pass it to execvp(3).
Just a side note -- the documentation for bash(1) looks to be incorrect.
It
reads:
-c string If the -c option is present, then commands are read
from
string. If there are arguments after the string, they
are
assigned to the positional parameters, starting with $0.
Seems to imply that the -c option doesn't pass the program name as the
first
argument, since that's normally in position $0. But a little testing
reveals:
$ bash -c "./printargs foo bar baz"
argv[0]: ./printargs
argv[1]: foo
argv[2]: bar
argv[3]: baz
ksh(1) and dash(1) display the same behavior, although the dash(1)
documentation
is better:
-c Read commands from the command_string operand
instead of from the standard input. Special
parameter 0 will be set from the command_name op
-erand and the positional parameters ($1, $2, etc.)
set from the remaining argument operands.
Hope that helps a little.
Best,
Kyle
On 12/16/2016 1:21 AM, bzhang41 wrote:
> Dear Prof.
>
> I have a question about the final assignment.
>
> When I tried to implement the -c options,
> I try to use fork(3) and then execvp(3),
>
> But I can't get the result as I expected, first argument of the execvp
> needs the
> real path of the executable file, and the second argument of the
> execvp needs
> some arguments with type char **.
>
> But the command from -c options doesn't have any arguments
> how can I pass a empty char ** to execvp, and let it just executes the
> command without any arguments.
>
> Or can I just call system(3) for -c options, I think that's a cheat
> right ?
>
> Thanks
> Bo
>
> _______________________________________________
> cs631apue mailing list
> cs631apue at lists.stevens.edu
> https://lists.stevens.edu/mailman/listinfo/cs631apue
More information about the cs631apue
mailing list