[embedded]To receive upload file by cgi-bin
#include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_LEN 50 #define bufferSize 512 #define FPATH "../uploads/" #define STATUS_FILE "../uploadStatus.htm" #define SHELL_FILE "../shell/response_Firmware.sh" #define SYS_LOG "firmware.ini" /* int writeStatus(char *fileName,char *status){ FILE *fp; if((fp=fopen(fileName,"w"))==NULL){ return -1; // means error open file error } fwrite(status,sizeof(char),strlen(status),fp); fclose(fp); return 0; } */ void main(){ char *contentType; int contentLen; char *buffer; int temp; int i; int pos; char str[bufferSize]; char oldStr[bufferSize*2]; short oldStrFull; char *p; char fname[200]; char ffname[512]; int fSize; int bSize; FILE *fp; char boundry[100]; int completedSize; int scroll_current_pos; int scroll_old_pos; char strUploadFileStatus[20]; //int a,b,c,d,e; char str_ini[STR_LEN]; char str_htm[STR_LEN]; char str_c2h[STR_LEN]; get_file_path(str_ini, SYS_LOG); printf("Content-type: text/html \n\n"); contentType=getenv("CONTENT_TYPE"); contentLen=atoi(getenv("CONTENT_LENGTH")); pos=0; i=10; fgets(boundry,100,stdin); pos+=strlen(boundry); while(i--){ if(fgets(str,bufferSize,stdin)){ pos+=strlen(str); if(strstr(str,"filename=")){ p=strstr(str,"filename="); strcpy(fname,p+strlen("filename=\"")); *p=0; break; } } } fgets(str,bufferSize,stdin); pos+=strlen(str); fgets(str,bufferSize,stdin); pos+=strlen(str); fSize=contentLen-pos-strlen(boundry)-4; completedSize=0; bSize=4096; memcpy(ffname,FPATH,strlen(FPATH)); memcpy(ffname+strlen(FPATH),fname,strlen(fname)); *(ffname+strlen(FPATH)+strlen(fname)-strlen("\"\n\r"))=0; if(!(fp=fopen(ffname,"wb"))){ printf("file open error <br>"); return; } oldStrFull=0; scroll_old_pos=0; do{ if(fSize>bufferSize){ bSize=bufferSize; fSize-=bufferSize; }else{ bSize=fSize; fSize=0; } fread(str,sizeof(char),bSize,stdin); if(oldStrFull==0){ memcpy(oldStr, str, bSize); oldStrFull=1; if(fSize!=0){ continue; } } memcpy(oldStr+bufferSize,str,bSize); if(strstr(oldStr,boundry)){ pos=strstr(oldStr,boundry)-oldStr-2; /* the 2 means \r\n */ fwrite(oldStr, sizeof(char), pos, fp); break; } fwrite(oldStr, sizeof(char), bufferSize, fp); if(fSize<=0){ fwrite(oldStr+bufferSize, sizeof(char), bSize, fp); break; } // completedSize+=bufferSize; // sprintf(strUploadFileStatus,"%d",completedSize); // writeStatus(STATUS_FILE,strUploadFileStatus); memcpy(oldStr,str,bufferSize); }while(fSize>0); fclose(fp); sprintf(str, "%s", SHELL_FILE); if(system(str)!=0){ printf("linuxSystem(SHELL_FILE) error !"); } printf("<body>"); printf("<script>parent.location='/cache/Firmware.htm?jmp=true'; </script>"); printf("</body>"); // writeStatus(STATUS_FILE,"over");}
标签: embedded
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: