Just set up your file descriptors as the exec
-d process shall find them and then do the exec.
For that you need open
, dup2
and close
.
All functions in the exec
-family just replace the current process with whatever one you say.
質問
In a C program, let's say i wann use Exec functions for executing a given program, for example if i wanna just try ls -l i'll do something like
args[0]="ls";
args[1]="-l";
args[2]=NULL;
...
execvp("ls", args);
and it's all fine. Now what if i wanna also add the redirection to a file (or to stderr)? I'm stuck, it's obvious that adding >log.txt as a 3rd entry in the array won't work, but I don't know how to proceed.
And also, what if I wanna pass some Input parameters? What if i wanna execute a GCC command like "gcc -o out in redirection>log.txt" ?
[update from comment:]
It's a C program that simulate a shell which can "run strings", string that contains a command, a list o parameters, input and a redirection.
解決
Just set up your file descriptors as the exec
-d process shall find them and then do the exec.
For that you need open
, dup2
and close
.
All functions in the exec
-family just replace the current process with whatever one you say.
他のヒント
Run the command in a shell:
char * args[] = {
"sh",
"-c",
"ls -l >out.ls 2>err.ls <in.ls",
NULL
};
...
execvp(args[0], args);
perror("execvp() failed");