嵌入式系统如何通过CGI支持简单asp

来源:互联网 发布:2017新款男鞋推荐知乎 编辑:程序博客网 时间:2024/05/06 16:12

将下面代码,编译成cgi-bin


#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>

extern FILE * stdout;

#define MAX_BUF_LINE   1024
#define cgiOut stdout

int runscript(char * script,int newLine)
{
 FILE * fp;
 char result[512];
 int len;
 
 fp = popen(script,"r");
 if( fp == NULL )
 {
  fprintf(cgiOut, "%s error<p>/n",script);
  return (-1);
 }
 while( fgets(result,sizeof(result),fp) != NULL )
 {
    fputs(result,cgiOut);
    if(newLine)
      fputs("<p>",cgiOut);
 }
  
 pclose(fp);

 return 0;
}

 

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

        char * scriptname = getenv("QUERY_STRING");
        FILE * fp;
        char * script_head = NULL;
        char * script_tail = NULL;
        char * script = NULL;
  int scriptlen = MAX_BUF_LINE;
       
   //     char strFileName[256];
        char buf[MAX_BUF_LINE];

 fprintf(cgiOut, "Content-type: text/html/r/n/r/n");
 
    if( argc == 2 )
    {
         scriptname = argv[1];
 // fprintf(stdout,scriptname);
 }
 
 if( scriptname == NULL )
 {
  fprintf(stdout,"none page is specified</br>");
  return 0;
 }
 getcwd(buf,sizeof(buf));
 
 //snprintf(strFileName,sizeof(strFileName),"%s",scriptname);
 fp = fopen(scriptname,"r");
 if( fp == NULL )
 {
  fprintf(stdout,"cannot find page %s in %s</br>",scriptname,buf);
  return 0;
 }
 while( (fgets(buf,sizeof(buf),fp)) != NULL )
 {
 
   if( script  == NULL )
   {
      script_head = strstr(buf,"<%");
   if( script_head )
   {
        *script_head++ ='/0';
        *script_head++ ='/0';
     fputs(buf,stdout);
     script = realloc(script,scriptlen);
     script[0] = '/0';
   }
   }
  
   if( script!= NULL )/*header is found*/
   {
      if(script[0] != '/0') /*<% %> in different line*/
      {
        script_head = buf;
     scriptlen += MAX_BUF_LINE;
     script = realloc(script,scriptlen);      
      }
  
      script_tail = strstr(script_head,"%>");
      if(script_tail)
      {
       *script_tail++ ='/0';
       *script_tail++ ='/0';  
     strcat(script,script_head); 
   //  fprintf(stdout,"$$$$script<%s>",script);
     runscript(script,0);
     free(script);
     script = NULL;
     fputs(script_tail,stdout);
    
      }
   else
   {
    strcat(script,script_head); /*add the whole line*/
   }    
    
   }
   else
   {  
    fputs(buf,stdout);
   }
 }
 fclose(fp);
 
        
 return 0;
}

 

 

原创粉丝点击