CGI C and HTML

S

Sergio

Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.

<HTML>
<BODY>

<FORM NAME="MyForm" ACTION="c:\PATH\MyProgram.exe" METHOD="GET"
ENCTYPE="application/x-www-form-urlencoded">

<INPUT TYPE="TEXT" NAME="MyString">
<INPUT TYPE="SUBMIT" NAME="Iaccept" VALUE="OK">
</FORM>
</BODY>
</HTML>
This code should have to produce a string("MyForm") and on SUBMIT,
this should be sent to MyProgram.exe to be processed.

Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.
Does't interest to tokenize and to unescape the string.
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).
Particularly, how could I capture by a C program a string comes from
another program.
For example, on command line by argv[], I can pass a string to the
program; unfortunately this procedure doesn't run between two
programs. Better I'm not able to exchange a string between programs,
without using any disk, only from RAM.

Greetings from Italy

Sergio
 
M

Martin Ambuhl

Sergio said:
Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){
^^^^
This is not C code/
/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");

Not checking the return from fopen() is poor form.
fprintf(filePtr,"%s",MyForm);

Trying to use the wild pointer MyForm is insane.
return NULL;

Returning a pointer is just stupid.
}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.

No, it should eat shit and die.
 
R

Richard Heathfield

Sergio said:
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).

It's quite easy to do CGI in standard C. I suggest that you get hold of a
good book on CGI.

Hints: you will need to use getenv to find out the value of the
"REQUEST_METHOD" environment variable. If it's "GET", then you'll need to
call getenv again to find out the value of the "QUERY_STRING" environment
variable (which contains your data). Otherwise, if it's "POST", you'll need
to use getenv() to query the value of the "CONTENT_LENGTH" environment
variable, which tells you how much data you can expect to find on stdin.

Easy, huh?
 
S

Sean Kenwrick

Sergio said:
Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.

<HTML>
<BODY>

<FORM NAME="MyForm" ACTION="c:\PATH\MyProgram.exe" METHOD="GET"
ENCTYPE="application/x-www-form-urlencoded">

<INPUT TYPE="TEXT" NAME="MyString">
<INPUT TYPE="SUBMIT" NAME="Iaccept" VALUE="OK">
</FORM>
</BODY>
</HTML>
This code should have to produce a string("MyForm") and on SUBMIT,
this should be sent to MyProgram.exe to be processed.

Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.
Does't interest to tokenize and to unescape the string.
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).
Particularly, how could I capture by a C program a string comes from
another program.
For example, on command line by argv[], I can pass a string to the
program; unfortunately this procedure doesn't run between two
programs. Better I'm not able to exchange a string between programs,
without using any disk, only from RAM.

Greetings from Italy

Sergio

To pass data between two programs you can read and write to stdin and
stdout. So if you have
prog1, prog2 and prog3 that all read from stdin and write to stdout then on
the command line you can
do something like:

prog1 | prog2 | prog3

Sean
 
K

Keith Thompson

Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.
[...]

Sorry, no, I don't think I will.
Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}

Ok.

I doubt that the above code would compile (since you try to return a
value from a void function). If you're going to post C code here,
please make sure it compiles and cut-and-paste it exactly; don't try
to paraphrase, or we'll get stuck on any errors you introduce by
paraphrasing it.

main() returns int, not void.

You fail to check the result of fopen(). What happens if it fails?

MyForm is never initialized, so you're passing garbage to fprintf().

Returning NULL from main makes no sense. The values you can portably
return from main() (assuming you declare it properly to return int)
are 0, EXIT_SUCCESS (both indicating success) and EXIT_FAILURE
(indicating failure). EXIT_SUCCESS and EXIT_FAILURE are macros
defined in <stdlib.h>. (Other values can be used, but not in portable
code; for example, the commonly used "exit(1);" indicates failure on
Unix, but success on VMS.) The NULL macro expands to a null pointer
constant; in some cases, the compiler may fail to warn you if you use
"return NULL;" in main(), but it's still incorrect.

Any questions about CGI and HTML belong in another newsgroup. If you
have questions about C, such as how to read from standard input or how
to obtain the values of environment variables, they're appropriate
here, but first consult your C textbook, your online documentation,
and the C FAQ at <http://www.eskimo.com/~scs/C-faq/top.html>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top