?
Solved

Extracting command line / argv[] entries

Posted on 2000-03-01
9
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 

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
 
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 150 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

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!

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 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