cgi.docx
- 文档编号:12673236
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:33
- 大小:21.09KB
cgi.docx
《cgi.docx》由会员分享,可在线阅读,更多相关《cgi.docx(33页珍藏版)》请在冰豆网上搜索。
cgi
/*cgi.c*/
#include
#include
#include"cgivars.h"
#include"htmllib.h"
#include"template.h"
intmain(){
char**postvars=NULL;/*POSTrequestdatarepository*/
char**getvars=NULL;/*GETrequestdatarepository*/
intform_method;/*POST=1,GET=0*/
form_method=getRequestMethod();
if(form_method==POST){
getvars=getGETvars();
postvars=getPOSTvars();
}elseif(form_method==GET){
getvars=getGETvars();
}
htmlHeader("DemoWebPage");
htmlBody();
template_page(postvars,form_method);
htmlFooter();
cleanUp(form_method,getvars,postvars);
fflush(stdout);
return(0);
}
/*htmllib.c
*HTMLcommonlibraryfunctionsfortheCGIprograms.*/
#include
#include"htmllib.h"
voidhtmlHeader(char*title){
printf("Content-type:
text/html\n\n
title);
}
voidhtmlBody(){
printf("
");}
voidhtmlFooter(){
printf("");
}
voidaddTitleElement(char*title){
printf("
%s
",title);}
/*cgivars.c
*(C)Copyright2000,MoretonBay().
*seeHTTP(www.w3.org)andRFC
*/
#include"cgivars.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
staticvoid
catchchild()
{
charbuf[40];
pid_tpid;
intstatus;
/*signal(SIGCHLD,catchchild);*//*Unneeded*/
pid=wait4(-1,&status,WUNTRACED,0);
if(WIFSTOPPED(status))
sprintf(buf,"sh%d:
Child%dstopped\n",getpid(),pid);
else
sprintf(buf,"sh%d:
Child%ddied\n",getpid(),pid);
//if(intcrlf)
//write(STDOUT,"\n",1);
write(STDOUT,buf,strlen(buf));
setenv("NIOSSHELL",buf,1);
}
intexecfile(cmd,bg,fplog)
char*cmd;
intbg;
FILE*fplog;
{
intpid;
char*myargv[MAXARGS];
structsigactionact;
intj=0;
//myargv[j++]=strtok("/bin/nios2iowd80681070127","");
myargv[j++]=strtok(cmd,"");
while(j =NULL); if(bg==0) signal(SIGCHLD,SIG_DFL); /* *Nomagiccharactersintheexpandedcommand,sodotheforkand *execourself.IfthisfailswithENOEXEC,thenrunthe *shellanywaysinceitmightbeashellscript. */ if(! (pid=vfork())){ intci; /* *Wearethechild,soruntheprogram. *Firstcloseanyextrafiledescriptorswehaveopened. *besurenottomodifyanyglobalsafterthevfork! */ signal(SIGINT,SIG_DFL); signal(SIGQUIT,SIG_DFL); signal(SIGCHLD,SIG_DFL); execvp(myargv[0],myargv); printf("%s: %s\n",myargv[0],(errno==ENOENT)? "Badcommandorfilename": strerror(errno)); fprintf(fplog,"%s: %s\n",myargv[0],(errno==ENOENT)? "Badcommandorfilename": strerror(errno)); _exit(0); } if(pid<0){ memset(&act,0,sizeof(act)); act.sa_handler=catchchild; act.sa_flags=SA_RESTART; sigaction(SIGCHLD,&act,NULL); perror("vforkfailed"); fprintf(fplog,"vforkfailed\n"); return; } if(bg! =0){ printf("[%d]\n",pid); fprintf(fplog,"[%d]\n",pid); return; } perror(myargv[0]); fprintf(fplog,"%s\n",myargv[0]); exit (1); } /*localfunctionprototypes*/ charhex2char(char*hex); voidunescape_url(char*url); charx2c(char*what); voidWriteNios(unsignedlongaddr,unsignedlongvalue); unsignedlongReadNios(unsignedlongaddr); voidnios2command(char*command); /*hex2char*/ /*RFC*/ charhex2char(char*hex){ charchar_value; char_value=(hex[0]>='A'? ((hex[0]&0xdf)-'A')+10: (hex[0]-'0')); char_value*=16; char_value+=(hex[1]>='A'? ((hex[1]&0xdf)-'A')+10: (hex[1]-'0')); returnchar_value; } /*unescape_url*/ /*RFC*/ voidunescape_url(char*url){ intn,k; for(n=0,k=0;url[k];++n,++k){ if((url[n]=url[k])=='%'){ url[n]=hex2char(&url[k+1]); k+=2; } } url[n]='\0'; } /*getRequestMethod *retn: from_method(GETorPOST)onsuccess, *-1onfailure.*/ intgetRequestMethod(){ char*request_method; intform_method; request_method=getenv("REQUEST_METHOD"); if(request_method==NULL) return-1; if(! strcmp(request_method,"GET")||! strcmp(request_method,"HEAD")){ form_method=GET; }elseif(! strcmp(request_method,"POST")){ form_method=POST; }else{ /*wtfwasitthen? ! ! */ return-1; } returnform_method; } /*getGETvars *retn: getvars*/ char**getGETvars(){ inti; char**getvars; char*getinput; char**pairlist; intpaircount=0; char*nvpair; char*eqpos; getinput=getenv("QUERY_STRING"); if(getinput) getinput=strdup(getinput); /*Changeallplussesbacktospaces*/ for(i=0;getinput&&getinput[i];i++) if(getinput[i]=='+') getinput[i]=''; pairlist=(char**)malloc(256*sizeof(char**)); paircount=0; nvpair=getinput? strtok(getinput,"&"): NULL; while(nvpair){ pairlist[paircount++]=strdup(nvpair); if(! (paircount%256)) pairlist=(char**)realloc(pairlist,(paircount+256)*sizeof(char**)); nvpair=strtok(NULL,"&"); } pairlist[paircount]=0; getvars=(char**)malloc((paircount*2+1)*sizeof(char**)); for(i=0;i if(eqpos=strchr(pairlist[i],'=')){ *eqpos='\0'; unescape_url(getvars[i*2+1]=strdup(eqpos+1)); }else{ unescape_url(getvars[i*2+1]=strdup("")); } unescape_url(getvars[i*2]=strdup(pairlist[i])); } getvars[paircount*2]=0; for(i=0;pairlist[i];i++) free(pairlist[i]); free(pairlist); if(getinput) free(getinput); returngetvars; } /*getPOSTvars *retn: postvars*/ char**getPOSTvars(){ inti,j,k; intcontent_length; char**postvars; char*postinput; char**pairlist; intpaircount=0; char*nvpair; char*eqpos; char*content_type; FILE*df; FILE*fplog=fopen("/log.txt","w"); if(NULL==fplog){ printf("%snichtgefunden! \n","log.txt"); exit (1); } content_type=getenv("CONTENT_TYPE"); if(! content_type){ fprintf(fplog,"noContenttype\n"); fclose(fplog); exit (1); } //df=fopen("/content.html","w"); //fwrite(content_type,1,(int)strlen(content_type),df); //fclose(df); postinput=getenv("CONTENT_LENGTH"); if(! postinput){ fprintf(fplog,"noContentlength\n"); fclose(fplog); exit (1); } if(! (content_length=atoi(postinput))){ fprintf(fplog,"noContenttype\n"); fclose(fplog); exit (1); } if(! (postinput=(char*)malloc(content_length+1))){ fprintf(fplog,"noContenttype\n"); fclose(fplog); exit (1); } if(! fread(postinput,content_length,1,stdin)){ fprintf(fplog,"noContenttype\n"); fclose(fplog); exit (1); } postinput[content_length]='\0'; //df=fopen("/post.html","w"); //fwrite(postinput,1,content_length,df); //fclose(df); //fprintf(fplog,"wrotepost.html\n"); charstringToFind[]="multipart/form-data;boundary="; //fornormalhtmlpostrequests if(NULL==strstr(content_type,stringToFind)){ for(i=0;postinput[i];i++) if(postinput[i]=='+') postinput[i]=''; pairlist=(char**)malloc(256*sizeof(char**)); paircount=0; nvpair=strtok(postinput,"&"); while(nvpair){ pairlist[paircount++]=strdup(nvpair); if(! (paircount%256)) pairlist=(char**)realloc(pairlist,(paircount+256)*sizeof(char**)); nvpair=strtok(NULL,"&"); } pairlist[paircount]=0; postvars=(char**)malloc((paircount*2+1)*sizeof(char**)); for(i=0;i if(eqpos=strchr(pairlist[i],'=')){ *eqpos='\0'; unescape_url(postvars[i*2+1]=strdup(eqpos+1)); }else{ unescape_url(postvars[i*2+1]=strdup("")); } unescape_url(postvars[i*2]=strdup(pairlist[i])); } postvars[paircount*2]=0; for(i=0;pairlist[i];i++) free(pairlist[i]); free(pairlist); free(postinput); fprintf(fplog,"extractedallpairs\n"); unsignedlongledG=0;//readvalue unsignedlongledR=0; unsignedlongbuttons=0; unsignedlongswitches=0; charlcd1[17]=""; lcd1[16]=0x00; charlcd2[17]=""; lcd2[16]=0x00; charsevseg[9]={'0','0','0','0','0','0','0','0',0x00}; unsignedlongseg=0; intlcd_changed=0; intseg7_changed=0; intledR_changed=0; intledG_changed=0; for(i=0;postvars[i];i+=2){ fprintf(fplog,"<%i>DEBUG: [%s]=[%s]\n",i,postvars[i],postvars[i+1]); if(strstr(postvars[i],"LEDR")){ if(*(postvars[i]+4)=='1'){ //ledR&=~(1<<0); ledR|=1<<0; }elseif(*(postvars[i]+4)=='2'){ ledR|=1<<1; }elseif(*(postvars[i]+4)=='3'){ ledR|=1<<2; }elseif(*(postvars[i]+4)=='4'){ ledR|=1<<3; }elseif(*(postvars[i]+4)=='5'){ ledR|=1<<4; }elseif(*(postvars[i]+4)=='6'){ ledR|=1<<5; }elseif(*(postvars[i]+4)=='7'){ ledR|=1<<6; }elseif(*(postvars[i]+4)=='8'){ ledR|=1<<7; }elseif(*(postvars[i]+4)=='9'){ ledR|=1<<8; }elseif(*(postvars[i]+4)=='A'){ ledR|=1<<9; }elseif(*(postvars[i]+4)=='B'){ ledR|=1<<10; }elseif(*(postvars[i]+4)=='C'){ ledR|=1<<11; }elseif(*(postvars[i]+4)=='D'){ ledR|=1<<12; }elseif(*(postvars[i]+4)=='E'){ ledR|=1<<13; }elseif(*(postvars[i]+4)=='F'){ ledR|=1<<14; }elseif(*(postvars[i]+4)=='G'){ ledR|=1<<15; }elseif(*(postvars[i]+4)=='H'){ ledR|=1<<16; }elseif(*(postvars[i]+4)=='I'){ ledR|=1<<17; } ledR_changed=1; fprintf(fplog,"<%i>DEBUG: [%s]=[%s]: LEDR=%08X\n",i,postvars[i],postvars[i+1],ledR); }elseif(strstr(postvars[i],"LEDG")){ if(*(postvars[i]+4)=='1'){ ledG|=1<<0; }elseif(*(postvars[i]+4)=='2'){ ledG|=1<<1; }elseif(*(postvars[i]+4)=='3'){ ledG|=1<<2; }elseif(*(postvars[i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- cgi