首页 > 代码库 > cgic实现输入文件名,打开文件的功能
cgic实现输入文件名,打开文件的功能
a.c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "cgic.h"
void HandleSubmit();// 提交
void Address();//多行文本
void ShowForm();//页面的显示
void ReplaceConf();
void show(char *file);
/*{{{*/
#define TRANS_BUFFER_LEN 0x100000
char gBuffer1[TRANS_BUFFER_LEN];
char gBuffer2[TRANS_BUFFER_LEN];
char* gBufferSource = gBuffer1;
char* gBufferDest = gBuffer2;
int firsttime = 1;
//对html文件特殊字符替换
void replaceString(char* substrBefore, char* substrAfter)
{
int offsetTrans = 0;
int offsetDest = 0;
char* srcPtr = gBufferSource;
char* dstPtr = gBufferDest;
char* pStrPad = srcPtr;
char* pStrPadOld = srcPtr;
int totalLength = strlen(srcPtr);
memset(dstPtr, 0, sizeof(dstPtr));
while((pStrPad = strstr(pStrPad, substrBefore)) != NULL){
memcpy(dstPtr+offsetDest, srcPtr+offsetTrans, (pStrPad - pStrPadOld));
offsetDest += pStrPad - pStrPadOld;
offsetTrans = pStrPad - srcPtr + strlen(substrBefore);
memcpy(dstPtr+offsetDest, substrAfter, strlen(substrAfter));
offsetDest += strlen(substrAfter);
pStrPad += strlen(substrBefore);
pStrPadOld = pStrPad;
}
memcpy(dstPtr+offsetDest, srcPtr+offsetTrans, totalLength - offsetTrans);
gBufferSource = dstPtr;
gBufferDest = srcPtr;
}
//will malloc outputBuffer
//for files less than 1MB
char* getFileContent(const char* filename)
{
int maxLen = 0x100000;
int lengthRead = 0;
char* tmpBuffer = NULL;
char* outputBuffer = NULL;
FILE* fd;
if((fd=fopen(filename,"r+"))!=NULL)
{
tmpBuffer = (char*)malloc(sizeof(char) * maxLen);
lengthRead = fread(tmpBuffer, 1, maxLen, fd);
outputBuffer = strdup(tmpBuffer);
free(tmpBuffer);
return outputBuffer;
}
else
{
return NULL;
}
}
//文件出错记录程序
void wlog(const char* fmt, ...)
{
FILE* pFileLog = NULL;
if (firsttime)
pFileLog = fopen("cgi-debug.log", "wt");
else
pFileLog = fopen("cgi-debug.log", "at");
firsttime = 0;
if (NULL == pFileLog)
return;
va_list args;
va_start(args, fmt);
vfprintf(pFileLog, fmt, args);
va_end(args);
fclose(pFileLog);
}
int cgiMain()
{
FILE* fa;
char szTemp[255]={};
char FileName_ntsm[80]="../var/ntpconf.html";
cgiHeaderContentType("text/html");
if (cgiFormSubmitClicked("apply") == cgiFormSuccess)
{
HandleSubmit();
}
show(szTemp);
return 0;
}
void show(char *file)
{
FILE* fa;
char szTemp[255];
char FileName_ntsm[80]="../var/ntpconf.html";
int lengthRead = 0;
if((fa=fopen(FileName_ntsm,"r+"))!= NULL)
{
memset(gBufferSource, 0, TRANS_BUFFER_LEN);
lengthRead = fread(gBufferSource, 1, TRANS_BUFFER_LEN, fa);
replaceString("$@@${action}", cgiScriptName);//替换action动作为cgic
replaceString("$@@${ntp.conf}", file);
printf("%s", gBufferSource);
}
}
void ReplaceConf()
{
FILE* file_fa;
char buff[TRANS_BUFFER_LEN];
char FileName[80]="/etc/ntp.conf";
if((file_fa=fopen(FileName,"r+"))==NULL)
{
fprintf(cgiOut, "filename is wrong");
}
fread(buff, 1, TRANS_BUFFER_LEN, file_fa);
replaceString("$@@${ntp.conf}", buff);
}
void HandleSubmit()// 提交
{
Address();
}
void Address()
{
char FileName[2048];
char command[80];
char buff[TRANS_BUFFER_LEN];
memset(buff,TRANS_BUFFER_LEN,0);
cgiFormStringNoNewlines("file_name", FileName, 81);
cgiHtmlEscape(FileName);//输出到页面文件名
FILE * file_fa = fopen(FileName,"r");
if(file_fa==NULL)//ntp.txt 文件出错警报
cgiHtmlEscape("open ntp.txt is wrrong");
fread(buff, 1, TRANS_BUFFER_LEN, file_fa);
show(buff);
fclose(file_fa);
}
global.html中内容
<html><head>
<!-- 2.0: multipart/form-data is required for file uploads. -->
<form method="POST" enctype="multipart/form-data" action="$@@${action}">
<p>ntpq -p </p>
<textarea cols="86" rows="5">$@@${ntpq_out}</textarea>
<p>ntpdate -d 192.168.5.21 </p>
<textarea cols="86" rows="28">$@@${ntpdate_out}</textarea>
<p>当前监控IP和限制网段</p>
<textarea cols="86" rows="3">$@@${MonitIP}</textarea>
<p>当前设置的明晰路由</p>
<textarea cols="86" rows="3">$@@${MonitIP}</textarea>
</form>
</body></html>
cgic实现输入文件名,打开文件的功能