Extracting command line / argv[] entries

Hello there.

My "C"  functions are linked into a 3rd party environment which
provides its own "main".  The 3rd party source  code is not accessible.

In one of these functions  I'm interested in the command line
arguments - namely - those which are passed to "main"  via  **argv.

An additional technique may rely on extracting the full command
line , as implemented in "GetCommandLine" Win32 API.

How can either of these 2 approches be implemented in "C" Unix?



Who is Participating?
jayesh_aminConnect With a Mentor Commented:

Hi Pagerbak,

I think this is unfair. You just turned
my suggestion into a piece of trivial
code and gave it as a proposed answer.
I thought that the normal protocol is to
post comments till the the asker finds
something that works and then the
right person gets the points by
converting to proposed answer.
But if you are so keen on getting
the expert points...please keep them :)


// check for command line parameters and set various flags
    parseCommandLine (int argc, char* argv[])
    for(int i=1; i<argc; i++)
        if(!strcmpi(argv[i], "-install"))
                //do what ever
        if(!strcmpi(argv[i], "-remove"))
            //do what ever
        if(!strcmpi(argv[i], "-d"))
            //do what eevr
        if(!strcmpi(argv[i], "-s"))
               /do what ever

//make the call from the main function like this

parseCommandLine(argc, argv);

hope this help,
best of luck
Aiden O' Connor
As irish as they get
If you do not have access to the main
function. It is impossible to
retrieve the command line arguments, unless a function is provided by your
3rd pary software package.

If you need some external settings
you can also use the environment
(see getenv function in the manual pages )

In a bash shell use;

and then call
char * str = getenv("MYDATA");
int number = atoi( str );
or something similar
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

There is the API call GetCommandLine which will give you the command line arguments passed to the the process when it was started.

As far as UNIX is concerned, I've always used the argc, and argv parameters passed to the main function. I know that it is possible to see the command line argument though because if you view a process with ps you see the command line arguments passed to the program. Even if you had to kludge something together pass your process id into ps and create a temp file and then parse that (that obviously would be a last resort); I'll bet there's a system call you could use to retrieve the arguments, I'm just not really sure what that call would be. There are the getenv and setenv function that arjenve mentioned in UNIX so you could definitely use those.

What C compiler are you using?

Here is my suggestion:

Step 1)
    #include <unistd.h>
     pid_t getpid(void);

to get your your process id
(assuming your part of code was not forked by the
main of third-party, in that case use
getppid() and so on....).
For more info, of course, see the man
page for getpid :)

step 2)

read the file /proc/<pid>/psinfo, which
has information on the initial
command-line arguments passed to
the process.
man -s 4 proc should give you more info
and the fields in this file.

My info is based on the Suns. Your
implementation of UNIX may have /proc
mounted in a slightly different way.

Good luck!


This code will access the processinfo from /proc and display the arguments given to the process. It's running on Digital Unix.
Maybe you can do something like this on your Unix.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <sys/procfs.h>
int main(void)
        int     pid = getpid();
        char    fname[31];
        int     fd;

        prpsinfo_t      prpsinfo;

        sprintf(fname, "/proc/%5.5d", pid);
        fd = open(fname, O_RDONLY);
        ioctl(fd, PIOCPSINFO, &prpsinfo);

        printf("ARG: %s\n",prpsinfo.pr_psargs);

        return 0;

Best of luck.
Hi Jayesh Amin

Please forgive me. Now that I read your comment closer, I can see your point.
But in my defence, I would like to say, that I did take the time to write a working piece of (trivial) code.
As you can see I have withdrawn my proposed answer.

Kind regards

Hi Pagerbak,

I am impressed! Thanks for being a nice person....if your code
helps hillel out, please do keep the points :)


hillelAuthor Commented:
Well - It is not enough to make justice.
Justice must be seen.
The points are all yours.

            Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.