aaa
aaa

Reputation: 433

In C programing how to get the commandline arguments passed to a program

We can get the arguments passed to a process using command "cat /proc/pid/cmdline". But how to get this progrmmatically.

Upvotes: 0

Views: 55

Answers (2)

Petr Skocik
Petr Skocik

Reputation: 60163

Open the /proc file:

int fd = open("/proc/$pid/cmdline", O_RDONLY);

and read from it. (The arguments are delimited by '\0'.)

The point of exposing this info in the filesystem is so that you don't need special functions for obtaining it.

Upvotes: 1

junoon53
junoon53

Reputation: 41

Command line arguments are passed to main() as a character array. Try this simple program:

int main(int argc, const char *argv[])                                          
{                                                                                  
    int i;                                                                         



for(i=0;i<argc;i++)                                                            
{                                                                              
    printf("%s\n",argv[i]);                                                    
}                                                                              

return 0;    
}

Upvotes: 0

Related Questions