• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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?

                    Thanks

                        Hillel.

0
hillel
Asked:
hillel
1 Solution
 
oconnoraCommented:
//
// check for command line parameters and set various flags
//
void
    parseCommandLine (int argc, char* argv[])
{
    for(int i=1; i<argc; i++)
        if(!strcmpi(argv[i], "-install"))
        {
                //do what ever
                exit(0);
        }
        else
        if(!strcmpi(argv[i], "-remove"))
        {
            //do what ever
            exit(0);
        }
        else
        if(!strcmpi(argv[i], "-d"))
        {
            //do what eevr
           
        }
        else
        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
0
 
arjenveCommented:
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;
EXPORT MYDATA=10

and then call
char * str = getenv("MYDATA");
int number = atoi( str );
or something similar
0
 
prozakCommented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jayesh_aminCommented:

Here is my suggestion:

Step 1)
use:
    #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!


0
 
pagerbakCommented:
Hi

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.
pagerbak
0
 
jayesh_aminCommented:

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

Regards,
Jayesh

0
 
pagerbakCommented:
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
pagerbak
0
 
jayesh_aminCommented:

Hi Pagerbak,

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

Regards,
Jayesh

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

            Thank you
                      Hillel
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now