Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access command line arguments without using char **argv in main

Is there any way to access the command line arguments, without using the argument to main? I need to access it in another function, and I would prefer not passing it in.

I need a solution that only necessarily works on Mac OS and Linux with GCC.

like image 836
Jeffrey Aylesworth Avatar asked Mar 18 '10 16:03

Jeffrey Aylesworth


1 Answers

In Linux, you can open /proc/self/cmdline (assuming that /proc is present) and parse manually (this is only required if you need argc/argv before main() - e.g. in a global constructor - as otherwise it's better to pass them via global vars).

More solutions are available here: http://blog.linuxgamepublishing.com/2009/10/12/argv-and-argc-and-just-how-to-get-them/

Yeah, it's gross and unportable, but if you are solving practical problems you may not care.

like image 67
RCL Avatar answered Oct 08 '22 00:10

RCL