0

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.

Community
  • 1
  • 1
Walter
  • 44,150
  • 20
  • 113
  • 196
  • 1
    Isn't there a `/proc/pid/cmdline` in Mac OS X? – Cheers and hth. - Alf Jul 07 '16 at 08:37
  • @Cheersandhth.-Alf not on my macbook. But perhaps it's not at `/`? – Walter Jul 07 '16 at 08:38
  • @LPs An implementation in C or C++ would be acceptable – Walter Jul 07 '16 at 08:39
  • http://stackoverflow.com/a/1024933/2836621 – Mark Setchell Jul 07 '16 at 08:40
  • My first attempt to find the answer would be to look up how it works on FreeBsd - as that is basically the closest relative to OS X... – BitTickler Jul 07 '16 at 08:40
  • 5
    Is there some reason why you can't just use the contents of `argv[]` ? – Paul R Jul 07 '16 at 08:41
  • @PaulR Well, for my simple example above this could be done. But I want `cmd_line()` to be callable form other library functions which know nothing about `argv[]`. So, for this solution one would need to pass `argv[]` (at program initialisation) to the library providing the function `cmd_line()`. Can be done, but is cumbersome and requires a re-write of the initialisation process(es). – Walter Jul 07 '16 at 08:47
  • https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/sysctl.3.html – Richard Hodges Jul 07 '16 at 08:50
  • @MarkSetchell That question is concerned about the (absolute) path of the running executable. This is a related problem, but AFAICS, it does not prove the command line arguments. – Walter Jul 07 '16 at 08:53
  • @RichardHodges That seems relevant, but is rather opaque. Do you have a solution based on `sysctl()`? – Walter Jul 07 '16 at 09:02
  • @Walter i'm afraid i don't. I found this by googling "/proc equivalent on OSX". You will find all Apple documentation opaque and cryptic I'm afraid. They seem to hate their developer community :) – Richard Hodges Jul 07 '16 at 10:08
  • @PaulR FYI some relevant code link in the answer to the post you pointed at as original seems dead. – user3078414 Jul 07 '16 at 13:08
  • @user3078414: yes, that's the trouble with link-based answers - the links tend to go stale after a while. The accepted answer looks good though. – Paul R Jul 07 '16 at 13:12

0 Answers0