By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Perl and C in CGI program

saranjegan
P: 51
Hello, Though i can make my script work well with perl , when am supposed write a script in C its giving error named 500 internal server error i have even tried with some example programs from net am getting the same error from error log i found its Premature End Of script headers ,while am trying it run on mozila browser it just asking for the download of the exe file, am posting the code below
HTML code :

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <Head>
  3. <title>Test Programs </title>
  4. <FORM ACTION="http://192.168.10.160/cgi-bin/mult.exe">
  5. <P>Please specify the multiplicands:
  6. <INPUT NAME="m" SIZE="5">
  7. <INPUT NAME="n" SIZE="5"><BR>
  8. <INPUT TYPE="SUBMIT" VALUE="Multiply!">
  9. </FORM>
  10.  
C-code :

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void)
  4. {
  5.     char *data;
  6.     long m,n;
  7.     printf("Content-Type:text/html \n \n");
  8.     printf("<HTML>\n");
  9.     printf("<Head>\n");
  10.     printf("<TITLE>Multiplication results</TITLE>\n");
  11.     printf("</Head>\n");
  12.     printf("<H3>Multiplication results</H3>\n");
  13.     data = getenv("QUERY_STRING");
  14.     if(data == NULL)
  15.         printf("<P>Error! Error in passing data from form to script.");
  16.     else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
  17.         printf("<P>Error! Invalid data. Data must be numeric.");
  18.     else
  19.         printf("<P>The product of %ld and %ld is %ld.",m,n,m*n);
  20.     printf("</Html>");
  21.     return 0;
  22. }
  23.  
can any suggest what might be the reason , i think this problem is not language (C )related, OS-windows server-Apache,But perl based scripts working fine in the same server
Aug 14 '07 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
ask C coding question in the C forum.
Aug 14 '07 #2

miller
Expert 100+
P: 1,089
Moving from Perl Forum to C/C++ Forum.

- MODERATOR
Aug 14 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You might look at this code:

sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
data is declared as a char*. However, it doesn't point to anything.
Aug 14 '07 #4

RRick
Expert 100+
P: 463
The data variable is set later in the program by getenv and should be valid with non NULL values.

You should be able to run this program as a standalone program, separate from Apache. Before you run it, set the system variable with your values (which might be a little tricky with embedded \n's).

Now you can use gdb or some debugger to find the problem. That way you'll really know where the problem is.
Aug 14 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
The data variable is set later in the program by getenv and should be valid with non NULL values.
Thanks. I didn't see that.
Aug 15 '07 #6

saranjegan
P: 51
Thanks. I didn't see that.
Hello,
As per your points i tried with very simple program to print a hello statement but it gives the same error , i think the problem is not with the code since it's copied from a Net source , the problem might be with apache configuration for running C based CGI scripts,Do any one have an idea with this can afford your suggestion or suggest me an right place to ask
Aug 16 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.