Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

system() call question

SAMI AHMAD
Regular Advisor

system() call question

in using system() call in C how can I pass the parameter ?:

int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\" \"< \"";

sts = system( cmd);
11 REPLIES
RBrown_1
Trusted Contributor

Re: system() call question

This question was answered in your other thread <>. Steven Schweda gave you a complete example to do this.
SAMI AHMAD
Regular Advisor

Re: system() call question

hi Robert !

passing parameter question was never answered so I thought posting the question here since this is the relevant group .

thanks
SAMI AHMAD
Regular Advisor

Re: system() call question

hi Robert !
I went back and looked at my post and here is what I wrote "
I fixed the issue as follows :

char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\" \"< \"";

now my second problem ... i need to pass the message part as parameter .. how can I do this ? "

I didnt get any reply to this question ..where do you see the parameter passing way someone told me .. please point me to it.

thanks
RBrown_1
Trusted Contributor

Re: system() call question

Go back and reread Steven's post in your previous thread "help in C". Steven's post begins "Boy, this is painful to watch".

Here are the important things to note:

1. The arguments argc and argv to the main function give you access to parameters on the command line.

2. Do not initialize cmd in your char statement as you insist on doing, just allocate space.

3. Use the sprintf function to load text into the cmd array that you previously allocated. The format is very similar to the printf statement, and those devious unix people included the letters "print" in the name, BUT IT DOES NOT PRINT ANYTHING, IT JUST PUTS TEXT INTO THE CMD ARRAY.

4. Use the debugger to see what happens to cmd before you pass it to the system() function.

Before Steven gave you a working example, several other answers in the same thread gave you all the information you needed make your own working example.
Hoff
Honored Contributor

Re: system() call question

Sami, do you want us to provide you with help with your OpenVMS and C programming questions and to help you learn the environment, or do you want us to give you a finished solution specifically tailored for your homework or for your specific business requirements? These two are not the same, and I'm presuming you're looking for help in learning here.

Here is an example of some C code that performs:

- system()
- sprintf()
- a DCL command
- C argument processing

It's extremely close to what you're asking for, but it's not an exact answer.

$ cc z
$ link z
$ z:==$sys$login:z
$ z USER
OpenVMS User Processes at 31-MAR-2009 17:04:44.44
Total number of users = ....

$ z TIME
31-MAR-2009 17:04:55
$ type z.c
#include
#include
main( int argc, char **argv ) {
char CmdBuffer[512];
sprintf( CmdBuffer, "SHOW %s", argv[1] );
system( CmdBuffer );
return 1;
}
$


I've posted up a very large C source code example (zip archive) at the HoffmanLabs site; the newuser source code package uses lib$spawn and argument processing and RMS and a whole lot more. Here are the current bits. A BSD-style license is in place.

http://64.223.189.234/node/1260

And FWIW, an update to this source code is underway. The new stuff uses argc and argv and getopt and various other features of OpenVMS. If you're interested in a preliminary copy, contact me offline.

SAMI AHMAD
Regular Advisor

Re: system() call question

ofcoure i dont want solutions , iam here to learn and thats how I solved the snmp trap solution myself using libspawn in C , with ofcourse yours n everyone's advise.
thanks for your help
SAMI AHMAD
Regular Advisor

Re: system() call question

btw I have no assignments from my boss I am learning openVMS for my own. as a profession iam a DBA :)
Hoff
Honored Contributor

Re: system() call question

Repost: ITRC tipped over.

A slightly updated version of the C code posted above is now posted at:

http://labs.hoffmanlabs.com/node/1261

If you're interested in learning C on your own, then one of the options is to borrow or to purchase the K&R C book Second Edition, or a similar book on the language.

http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)

And also look at the C programming shelf available within the HP OpenVMS manuals. There are a gazillion C examples on the OpenVMS Freeware distributions, as well.
SAMI AHMAD
Regular Advisor

Re: system() call question

I solved this problem by concatanating strings together with the arguments as follows:

#include
#include
#include

#define size 512
static char cmdbuffer[size];

main(int argc, char **argv )
{
int i;
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\"";

/* argv[0] is program name */

strncat (cmdbuffer, cmd,size);
strncat (cmdbuffer, " \"",size);
strncat (cmdbuffer, argv[1],size);
strncat (cmdbuffer,"\"",size);
printf ("%s\n",cmdbuffer);
sts = system(cmdbuffer);
printf( " sts = %%x%08x.\n", sts);
}

$ commarg "two parameters"
mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 "D" "two parameters"
sts = %x00000001.

Joseph Huber_1
Honored Contributor

Re: system() call question

Fine, You now know that there are several ways in C to accomplish the same result.

Last week in another thread You learned already how to replace the series of strncat by a single sprintf :-)
http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: system() call question

Here's a different answer to the homework.


#include
#include
#include

#define CMDBUFFERSIZE 512
static char cmdbuffer[CMDBUFFERSIZE+1];

main(int argc, char **argv )
{
int sts;
char *cmd = "mcr tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\" \"%.*s\"";
sprintf( cmdbuffer, cmd, CMDBUFFERSIZE, argv[1] );
system( cmdbuffer );
}