I want a utility function that returns the full command line of the program from which it is called. In other words,
std::string cmd_line();
int main(int argc, char**argv)
{
std::cout << "the current program command line is \""
<< cmd_line() << "\"\n";
}
(or the equivalent C program) compiled with c++ -o my_program test.cc
should produce upon running with my_program x=7
the current program command line is "my_program x=7"
Now, under linux, I manage to simply read this off /proc/pid/cmdline
where the pid
is obtained by getpid()
(from unistd.h
). What can I do under Mac OS? Is there a more portable way?
Note 1 added in Edit In the above example, one could of course simply use argv[]
. But I want cmd_line()
to be useable from third party library function w/o knowledge of argv[]
. So, one would need to provide the argv[]
information to the library providing cmd_line()
at initialisation. Can be done, but is somewhat cumbersome. I'm looking for a way to exploit the program initialisation of the run-time system itself.
Note 2 added in Edit The related problem of obtaining the (absolute or relative) path of the running executable is subject of this SO question.