Two small CGI examples made by C language

Source: Internet
Author: User

Modify zookeeper + Lao Tzu

This is the most simple CGI, And it prints a sentence based on the differences in the browser.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Void main (void)
...{
Char * http_user_agent;
Printf ("Content-Type: text/plain ");
Http_user_agent = getenv ("http_user_agent ");
If (http_user_agent = NULL )...{
Printf ("Oops! Your browser failed to set the http_user_agent ");
Printf ("environment variable! ");
} Else if (! Strncmp (http_user_agent, "Mosaic", 6 ))...{
Printf ("I guess you are sticking with the original, huh? ");
} Else if (! Strncmp (http_user_agent, "Mozilla", 7 ))...{
Printf ("well, you are not alone. A majority of the people are ");
Printf ("using Netscape Navigator! ");
} Else if (! Strncmp (http_user_agent, "Lynx", 4 ))...{
Printf ("lynx is great, but go get yourself a graphic browser! ");
} Else ...{
Printf ("I see you are using the % s browser.", http_user_agent );
Printf ("I don't think it's as famous as Netscape, mosaic or Lynx! ");
}
Exit (0 );
}

========================================================== ====================================

Another

Seek.html faces

<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
  
<Body>
<P> Please refer to the query string </P>
<Form name = "form1" Action = "./A. Out">
<P> </P>
<P>
<Input name = "keyname" value = "" type = text>
</P>
<P>
<Input type = "Submit" value = "">
</P>
</Form>
<P> </P>
</Body>
</Html>

 

Mycgi. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <fcntl. h>
# Include <time. h>
# Include <string. h>

Int main (INT argc, char * argv [])... {
Int FD;
Int status;
Time_t I;
Char cfilename [64];
Char ctempname [64];
Char cbuffer [1024];
Char * P = cbuffer;
Char ccontent [10240];

Char * data;
Char keyword [1024];
Data = getenv ("QUERY_STRING ");
If (Data = NULL )...{
Printf ("Content-Type: text/html ");
Printf ("not found! ");
Exit (1 );
}

Sscanf (data, "keyname = % S &", keyword );

P + = sprintf (P, "locate '% s' | sed-E's # ^/usr/local/Apache/htdocs # http: // 127.0.0.1 # '| sed-E's # ^. * $ # <a href = & >&</A >#'> ", keyword );
I = Time (null );

Sprintf (ctempname, "shortd.html", I );
Sprintf (cfilename, "/usr/local/Apache/temp/export d.html", I );
Strcat (cbuffer, cfilename );
Cbuffer [1024-1] = 0;

System (cbuffer );
FD = open (cfilename, o_rdwr );
Status = read (FD, ccontent, sizeof (ccontent)-1 );
Close (FD );

Printf ("Content-Type: text/html ");
Printf ("<meta http-equiv = Refresh content = 0; url = http: // 127.0.0.1/temp/% S>", ctempname );
// Printf ("% s", ctempname );
Printf ("waiting .......................");
Printf ("% s", keyword );

Return 0;
}

 

GCC mycgi. c

Actually, just copy the.out(includes seek.html) exported to the webapps/root directory of Tomcat (Linux), and add the limit of privilege ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.