Extracting command line / argv[] entries

Posted on 2000-03-01
Last Modified: 2010-04-02
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?



Question by:hillel

Expert Comment

ID: 2584985
// 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

Expert Comment

ID: 2588207
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

Expert Comment

ID: 2588486
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?

Expert Comment

ID: 2589445

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!

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.


Expert Comment

ID: 2604046

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.

Accepted Solution

jayesh_amin earned 50 total points
ID: 2605036

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 :)



Expert Comment

ID: 2606610
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

Expert Comment

ID: 2608646

Hi Pagerbak,

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



Author Comment

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

            Thank you

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now