cgiWord格式.docx
《cgiWord格式.docx》由会员分享,可在线阅读,更多相关《cgiWord格式.docx(33页珍藏版)》请在冰点文库上搜索。
HEAD>
TITLE>
%s<
/TITLE>
/HEAD>
"
title);
voidhtmlBody(){
BODY>
voidhtmlFooter(){
/BODY>
/HTML>
voidaddTitleElement(char*title){
H1>
/H1>
title);
/*cgivars.c
*(C)Copyright2000,MoretonBay().
*seeHTTP(www.w3.org)andRFC
*/
stdlib.h>
signal.h>
errno.h>
unistd.h>
sys/stat.h>
sys/time.h>
sys/wait.h>
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
Child%ddied\n"
//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<
MAXARGS&
&
(myargv[j++]=strtok(NULL,"
))!
=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);
execvp(myargv[0],myargv);
printf("
%s:
%s\n"
myargv[0],(errno==ENOENT)?
Badcommandorfilename"
:
strerror(errno));
fprintf(fplog,"
_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){
[%d]\n"
pid);
fprintf(fplog,"
return;
perror(myargv[0]);
%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)-'
)+10:
(hex[0]-'
0'
));
char_value*=16;
char_value+=(hex[1]>
((hex[1]&
(hex[1]-'
returnchar_value;
/*unescape_url*/
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;
request_method=getenv("
REQUEST_METHOD"
if(request_method==NULL)
return-1;
strcmp(request_method,"
GET"
)||!
HEAD"
)){
form_method=GET;
}elseif(!
POST"
)){
form_method=POST;
}else{
/*wtfwasitthen?
!
returnform_method;
/*getGETvars
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<
paircount;
i++){
if(eqpos=strchr(pairlist[i],'
='
*eqpos='
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;
pairlist[i];
i++)
free(pairlist[i]);
free(pairlist);
free(getinput);
returngetvars;
/*getPOSTvars
postvars*/
char**getPOSTvars(){
inti,j,k;
intcontent_length;
char**postvars;
char*postinput;
char*content_type;
FILE*df;
FILE*fplog=fopen("
/log.txt"
w"
if(NULL==fplog){
%snichtgefunden!
"
log.txt"
exit
(1);
content_type=getenv("
CONTENT_TYPE"
content_type){
noContenttype\n"
fclose(fplog);
//df=fopen("
/content.html"
//fwrite(content_type,1,(int)strlen(content_type),df);
//fclose(df);
postinput=getenv("
CONTENT_LENGTH"
postinput){
noContentlength\n"
if(!
(content_length=atoi(postinput))){
(postinput=(char*)malloc(content_length+1))){
fread(postinput,content_length,1,stdin)){
postinput[content_length]='
/post.html"
//fwrite(postinput,1,content_length,df);
//fprintf(fplog,"
wrotepost.html\n"
charstringToFind[]="
multipart/form-data;
boundary="
//fornormalhtmlpostrequests
if(NULL==strstr(content_type,stringToFind)){
for(i=0;
postinput[i];
if(postinput[i]=='
postinput[i]='
pairlist=(char**)malloc(256*sizeof(char**));
nvpair=strtok(postinput,"
pairlist[paircount++]=strdup(nvpair);
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<
i++){
if(eqpos=strchr(pairlist[i],'
*eqpos='
unescape_url(postvars[i*2+1]=strdup(eqpos+1));
unescape_url(postvars[i*2+1]=strdup("
unescape_url(postvars[i*2]=strdup(pairlist[i]));
postvars[paircount*2]=0;
free(pairlist[i]);
free(postinput);
extractedallpairs\n"
unsignedlongledG=0;
//readvalue
unsignedlongledR=0;
unsignedlongbuttons=0;
unsignedlongswitches=0;
charlcd1[17]="
lcd1[16]=0x00;
charlcd2[17]="
lcd2[16]=0x00;
charsevseg[9]={'
'
0x00};
unsignedlongseg=0;
intlcd_changed=0;
intseg7_changed=0;
intledR_changed=0;
intledG_changed=0;
for(i=0;
postvars[i];
i+=2){
%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'
1;
3'
2;
4'
3;
5'
4;
6'
5;
7'
6;
8'
7;
9'
8;
9;
B'
10;
C'
11;
D'
12;
E'
13;
F'
14;
G'
15;
H'
16;
I'
17;
ledR_changed=1;
[%s]=[%s]:
LEDR=%08X\n"
i,postvars[i],postvars[i+1],ledR);
}elseif(strstr(postvars[i],"
LEDG"
ledG|=1<
}elseif(*(postvars[i