📄 main.c
字号:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#define CR 0x0D
#define LF 0x0A
char *g_pBoundAry;
char *g_pUpLoadBuff;
char *g_pEndString;
char* GetUpLoadFileTail(char *pStart,int nSize)
{
char *pBound=g_pBoundAry;
char *pFileTail=NULL;
char *pFilePos=pStart;
int nBoundSize=strlen(g_pBoundAry);
int i;
for(i=0;i<nSize-nBoundSize+1;i++)
{
while((pBound!=NULL)&&(*pBound==*pFilePos))
{
pBound++;
pFilePos++;
}
if(*pBound!=NULL)
{
pBound=g_pBoundAry;
pFilePos=pStart+i;
}
else
{
pFileTail=pFilePos-nBoundSize;
break;
}
}
return pFileTail;
}
int main(int argc, char* argv[])
{
int i,nLength,bIsFirst;
char ch,ch1,ch2;
FILE *pFileOut,*pFileDebug;
char *pFileHead,*pFileTail,*pFilePos;
char *pEndString;
nLength=0;
pFileDebug=fopen("d:\\debug.txt","wb");
pFileOut=fopen("d:\\out.txt","wb");
//get content length
/*
if(getenv("CONTENT_LENGTH"))
{
nLength=atoi(getenv("CONTENT_LENGTH"));
}
*/
nLength=1000;
g_pUpLoadBuff=(char*)malloc(sizeof(char)*(nLength+1));
memset(g_pUpLoadBuff,0,nLength+1);
//copy content to g_pUpLoadBuff;copy div str to g_pBoundAry
bIsFirst=1;
i=0;
while(1)
{
ch=getc(stdin);
*(g_pUpLoadBuff+i)=ch;
i++;
if(bIsFirst==1)
{
if(ch=='\n')
{
g_pBoundAry=(char*)malloc(sizeof(char)*(i));
g_pEndString=(char*)malloc(sizeof(char)*(i+2));
memset(g_pBoundAry,'\0',i);
memset(g_pEndString,'\0',i+2);
strncpy(g_pBoundAry,g_pUpLoadBuff,i-1);
strcpy(g_pEndString,g_pBoundAry);
strcat(g_pEndString,"--");
bIsFirst=0;
pEndString=g_pEndString;
}
}
else
{
while((pEndString!=NULL)&&(*pEndString==ch))
{
pEndString++;
ch=getc(stdin);
*(g_pUpLoadBuff+i)=ch;
i++;
putc(ch,pFileDebug);
}
if(*pEndString!=NULL)
{
pEndString=g_pEndString;
}
else
{
break;
}
}
putc(ch,pFileDebug);
}
//find the start postion
pFileHead=strchr(g_pUpLoadBuff,'\n')+1;
for(i=0;i<3;i++)
{
pFileHead=strchr(pFileHead,'\n')+1;
}
if(*pFileHead==LF)
{
pFileHead++;
}
//find the end position
/* strstr only can be used to upload txt file
pFileTail=strstr(g_pUpLoadBuff+1,g_pBoundAry);
*/
pFileTail=GetUpLoadFileTail(pFileHead,nLength-(pFileHead-g_pUpLoadBuff));
pFilePos=pFileHead;
//write file to disk
while(pFilePos!=pFileTail)
{
putc(*pFilePos,pFileOut);
pFilePos++;
}
printf("Content-Type:text/html\n\n");
printf("<html>");
printf("<br>");
printf("total length=%d",nLength);
printf("<br>");
printf("bound string=%s",g_pBoundAry);
printf("<br>");
printf("head position=%d",(pFileHead-g_pUpLoadBuff));
printf("<br>");
printf("tail position=%d",pFileTail-g_pUpLoadBuff);
printf("\n</html>");
fflush(stdout);
fclose(pFileDebug);
fclose(pFileOut);
free(g_pBoundAry);
free(g_pEndString);
free(g_pUpLoadBuff);
return 0;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -