// remove some files, capturing any error messagesstd::vector<std::string>argv;std::vector<std::string>errors;argv.push_back("rm");argv.push_back("./foo.txt");argv.push_back("./bar.html");redi::ipstreamin("rm",argv,pstreambuf::pstderr);std::stringerrmsg;while(std::getline(in,errmsg)){errors.push_back(errmsg);}
Should "rm" be pushed onto the vector? It is provided again in the ipstream declaration. Why twice?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The example is correct. The first argument to the ipstream constructor is the name of the executable to run (which will be looked up in PATH). The argv strings are passed to the new process as its argv[] array. By convention, argv[0] is the name of the executable that was run, but it doesn't have to be. You can actually pass some other string in argv[0].
So it's correct that there are two "rm" strings. One is used by the OS to find the program to run, and the other is passed to that program as its argv[0] argument.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
From https://pstreams.sourceforge.net/ :
Should "rm" be pushed onto the vector? It is provided again in the ipstream declaration. Why twice?
The example is correct. The first argument to the
ipstream
constructor is the name of the executable to run (which will be looked up in PATH). Theargv
strings are passed to the new process as its argv[] array. By convention,argv[0]
is the name of the executable that was run, but it doesn't have to be. You can actually pass some other string inargv[0]
.So it's correct that there are two
"rm"
strings. One is used by the OS to find the program to run, and the other is passed to that program as itsargv[0]
argument.This matches the API of POSIX
execv
:https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html