Solved

Extracting command line / argv[] entries

Posted on 2000-03-01
9
313 Views
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?

                    Thanks

                        Hillel.

0
Comment
Question by:hillel
9 Comments
 

Expert Comment

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

Expert Comment

by:arjenve
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;
EXPORT MYDATA=10

and then call
char * str = getenv("MYDATA");
int number = atoi( str );
or something similar
0
 
LVL 1

Expert Comment

by:prozak
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?
0
 

Expert Comment

by:jayesh_amin
ID: 2589445

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:pagerbak
ID: 2604046
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
 

Accepted Solution

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

Regards,
Jayesh

0
 
LVL 1

Expert Comment

by:pagerbak
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
pagerbak
0
 

Expert Comment

by:jayesh_amin
ID: 2608646

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
 

Author Comment

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

            Thank you
                      Hillel
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Why does my while loop in C run indefinitely? 10 90
Line meaning 9 77
An API detour question 7 69
Acrinis True image 2 18
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.

760 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

22 Experts available now in Live!

Get 1:1 Help Now