兩個C語言造的cgi小例子

轉載+老子修改

這是一個最簡單的cgi,它根據瀏覽器的不同打印出一句話。

#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);
}

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

又一個

seek.html頁面

< html >
< head >
< title > Untitled Document </ title >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312" >
</ head >
  
< body >
< p > 請輸入查詢字符串 </ 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 *= 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, 
"%d.html", i);
    sprintf(cFileName, 
"/usr/local/apache/temp/%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

然後把輸出的a.out(還有seek.html)複製到tomcat(Linux)的webapps/ROOT目錄下,加上執行權限就行了~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值