cancel
Showing results for 
Search instead for 
Did you mean: 

help in C

 
SAMI AHMAD
Regular Advisor

help in C

can someone give me small example of a C program taking a string input and printing it?

thanks
19 REPLIES 19
Hoff
Honored Contributor

Re: help in C

Sure. See attached.

$ cc x
$ link x
$ run x
hi
hi
$ type x.c
#include
main()
{
char foo[255];
gets( foo );
printf( foo );
return 0;
}
$

The above code runs and does exactly what you've asked for, but it's not particularly secure nor entirely reliable. The gets call has vulnerabilities.

I've posted a far larger example C program (that does this prompting, and a whole lot more) in the newuser code in the zip archive available available via:

http://64.223.189.234/node/1135

The OpenVMS C manuals and the OpenVMS programming concepts manuals (all of which are required reading here) are available at:

http://www.hp.com/go/openvms/doc

The C manuals and the Programming Concepts manual introduce you to the language and to programming C on OpenVMS.


SAMI AHMAD
Regular Advisor

Re: help in C

hi Hoff !
I am sorry i think i explained it wrong, i dont want to input a string from command line but i want to be able to pass an argument to a procedure say x("test string")
and the program should print the string ?

thanks for help
RBrown_1
Trusted Contributor

Re: help in C

$ create demo.c
#include
void main ()
{
while (putchar (getchar ()) != EOF)
{}
}
^Z
$ cc demo
$ link demo
$ run demo
the quick brown fox jumps over the lazy dog!
the quick brown fox jumps over the laxy dog!
^Z

Figuring out how to get rid of the y-umlaut at the end is left as an exercise.
SAMI AHMAD
Regular Advisor

Re: help in C

ok here is what I am trying to do , below is my program and I want to pass 'msg' as parameter not on command line ..

SUNNY2$ type snmp.c
#include
#include

int main( char *msg)
{
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\" \"msg\"";

printf( " cmd: >%s<.\n", cmd);
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}
Hoff
Honored Contributor

Re: help in C

The C call you want here is sprintf().
RBrown_1
Trusted Contributor

Re: help in C

We have a problem with terminology here. Typically, "command line" means those lines in my and Hoff's replies like:

$ cc demo
$ link demo
$ run demo

Neither my nor Hoff's programs accepted input from the command line in that sense.

You say you want to "pass an argument to a procedure".

Do you mean like a function call? For example the printf function call:

printf ("test string\n");

Or you want to write a function that accepts a string argument and sends it to printf?

Or do you want to write a program that prints strings from the command line as in

$ demo "test string"
test string

?

In the latter case, refer to the DECC Language Reference Manual "Passing Arguments to the main Function", which is section 5.6.3 in the very old copy that I have.

You can find the DECC documentation in the usual places on the HP VMS documentation web site.
SAMI AHMAD
Regular Advisor

Re: help in C

the above program i gave is a perfectly working program ,but it has hard code string called 'msg' that it sends as snmp trap, all i want to do is to be able to replace 'msg' with a dynamic value ,
so from command prompt i should be able to do this
$sndsnmp :== $sys$login:snmp.exe
$sndsnmp "test msg"
$sndsnmp "another test"

run snmp.exe "test msg" and the prog should replace the 'msg' value in the program with "test msg" , do you know what iam saying ?
let me know otherwise i will explain further
RBrown_1
Trusted Contributor

Re: help in C

Yup. You need to read section 5.6.3.

Probably your solution will include a statement something like this:

sprintf (cmd, "long string prefix %s", argv[1]);
SAMI AHMAD
Regular Advisor

Re: help in C

hi Brown !

let me explain in a better way , I think what i need is very simple .

this is my working program now :

#include
#include

int main()
{
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\" \"msg\"";

printf( " cmd: >%s<.\n", cmd);
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}

I run it like this :

SNDSNMP == "$SYS$LOGIN:SNMP.EXE"
SUNNY2$ sndsnmp
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" "msg"<.
sts = %x00000001.


you see above its constructing a command with hard coded "msg" .
i want to be able to do this

$sndsnmp "test msg"
and it should construct the command

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" "test msg"<.


SAMI AHMAD
Regular Advisor

Re: help in C

one thing more ..is iam not intersted in printf..that is just for diagnosis. my need is to construct this string dynamically
(the part marked with ^^^)

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\" \"msg\"";
^^^
SAMI AHMAD
Regular Advisor

Re: help in C

to make things even simpler

#include
#include

main()
{
char *cmd ="write sys$output msg";
printf( " cmd: >%s<.\n", cmd);
}

SUNNY2$ xx :== $sys$login:x.exe
SUNNY2$ xx
cmd: >write sys$output msg<.

I want to be able to do this

main( argument)
{
char *cmd ="write sys$output printf( " cmd: >%s<.\n", cmd);
}

SUNNY2$ xx :== $sys$login:x.exe
SUNNY2$ xx "test msg"
cmd: >write sys$output test msg<.



SAMI AHMAD
Regular Advisor

Re: help in C

ok I found the solution :

#include
#include

int main(int argc, char *argv[])
{
int sts;
char msg;

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\" \"*msg\"";

printf("program: %s\n",argv[1]);
/* printf( " cmd: >%s<.\n", cmd); */
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}


SUNNY2$ sndsnmp testing
program: testing
sts = %x00000001.

but I cant replace the 'msg' with the argument passed .. any ideas ?
Hoff
Honored Contributor

Re: help in C

Use sprintf(). That's the formatted string conversion routine; it looks and works very much like the C printf() call, but dumps the results into a string buffer. Your command-level string would be a parameter passed into the sprintf() here.
SAMI AHMAD
Regular Advisor

Re: help in C

but Hoff I dont need to print anything i need to assign like below

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\" \"*msg\"";
^^^^

how can i do this by sprintf ?
RBrown_1
Trusted Contributor
Joseph Huber_1
Honored Contributor

Re: help in C

Sami, do You mean something like this ?

tsprintf.c:

#include stdio
int main(int argc, char *argv[]) {
char cmd[256];
sprintf(cmd,"mcr sys$system:tcpip$snmp_trapsnd.exe"\
" 0.0 local 0 0 0 -h %s v2c"\
"1.3.6.1.6.3.1.1.4.1.0 \"D\" \"*msg\"",argv[1]);

printf(" %s\n",cmd); /show result*/

}

Test the program:
$ mcr []tsprintf 10.1.222.1
mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.1.222.1 v2c1.3.6.1.
6.3.1.1.4.1.0 "D" "*msg"


http://www.mpp.mpg.de/~huber
Steven Schweda
Honored Contributor

Re: help in C

Boy, this is painful to watch.

> Sami, do You mean something like this ?

Probably more like this:

alp $ type snmp2.c
#include
#include

int main( int argc, char **argv)
{
int sts;
char cmd[ 1024];

sprintf( cmd, "mcr tcpip$snmp_trapsnd.exe\
1.3.6.1.6.3.1.1.4.1.0 local 0 0 0 -v2c -h 10.100.10.112 \"D\" \"%s\"",
argv[ 1]);

printf( " cmd: >%s<.\n", cmd);
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}

alp $ cc snmp2
alp $ link snmp2
alp $ mcr []snmp2 "my message text"
cmd: >mcr tcpip$snmp_trapsnd.exe 1.3.6.1.6.3.1.1.4.1.0 local 0 0 0 -v2c -h 10.1
00.10.112 "D" "my message text"<.
Invalid variable name OID syntax: D
Invalid variable list.
#snmp_trapsnd enterprise agent-address generic specific timeticks
[-v version] [-c community] [-h host] [-p port] [-tcp] {variable [type value]}
sts = %x00000001.
alp $

Looks as if the command itself may need a
little work.

The 'D' must be '"D"' to keep it upper-case
when snmp_trapsnd sees it.

Note that no one is checking to see if you
really have a command-line argument (argc >
1), so it'll behave badly if you don't
specify one.
Joseph Huber_1
Honored Contributor

Re: help in C

I know nothing about the snmp... command syntax, the question was how to use sprintf to replace a part by commandline arguments, and Sami
marked 10.100.18.245 by ^^^^, so I assumed this IP address should be the variable argument.
http://www.mpp.mpg.de/~huber
Jon Pinkley
Honored Contributor

Re: help in C

ITRC forum wrapping makes it appear that it is pointing to ip address, but it is really pointing at the "char".

Cut and paste into (for example notepad), and you will see.

But what does this whole topic have to do with VMS?

Sami, if you need to learn C, this isn't the place to learn.

See Hoff's response to your question about snmp traps.

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1325393
it depends