tags:

views:

294

answers:

3

I wonder if there are some API to get the current process' command line arguments on Linux.


I guess I was terribly unclear. The real purpose of the question was about passing unicode file names through command line arguments.

+3  A: 

checkout Getopt It's a command line parsing library that's implemented in many languages including C.

Otherwise:

int main(int argc, char *argv[])

argv is an array of arguments as char* and argc is the number of arguments.

argv[0] is always the executable filename itself.

Charles Ma
You mean Getopt? </typo>
fennec
ah yes, thanks fennec
Charles Ma
+2  A: 

Read from file /proc/self/cmdline

For example:

[wallyk@zf ~]$ od -a /proc/30651/cmdline
0000000   /   u   s   r   /   s   b   i   n   /   h   t   t   p   d nul
0000020

Or, for a satisfyingly refreshing self-reference:

[wally@zf images]$ od -c /proc/self/cmdline
0000000   o   d  \0   -   c  \0   /   p   r   o   c   /   s   e   l   f
0000020   /   c   m   d   l   i   n   e  \0
0000031
wallyk
Be careful of the null bytes (0x00) delimiting each token. Also, the text in /proc/self/cmdline will not be exactly what the user typed -- quotation marks will be stripped, etc.
Kevin Little
+1  A: 

Just use argc and argv. argv will be in the local encoding (which could be UTF-8), and from there you can convert to wchar_t (e.g. via mbtowc).

jamesdlin