Cgic implementation of the input file name, the ability to open files

Source: Internet
Author: User

A.C file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "cgic.h"

void Handlesubmit ();//Submit
void Address ();//Multiline text
void ShowForm ();//Display of the page
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;
Special character substitution for HTML files
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;
}
}
//File Error logger
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 ("[Email protected]@${action}", cgiscriptname);//Replace action action for Cgic

replacestring ("[ email protected]@${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 ("[email protected]@${ntp.conf}", buff);
}
Void Handlesubmit ()//Submit
{
Address ();
}

Address void ()
{
Char filename[2048];
Char command[80];
Char Buff[trans_buffer_len];
memset (buff,trans_buffer_len,0);

Cgiformstringnonewlines ("file_name", FileName, 81);
Cgihtmlescape (filename);//output to page file name

FILE * File_fa =fopen (FileName, "R");
if (file_fa==null)//ntp.txt file Error alert
Cgihtmlescape ("Open Ntp.txt is Wrrong");

Fread (Buff, 1, Trans_buffer_len, FILE_FA);

Show (buff);

Fclose (FILE_FA);
}

Content in global.html

<!--2.0:multipart/form-data is required for file uploads. -
<form method= "POST" enctype= "Multipart/form-data" action= "[Email protected]@${action}" >
<p>ntpq-p </p>
<textarea cols= "rows=" 5 ">[email protected]@${ntpq_out}</textarea>
<p>ntpdate-d 192.168.5.21 </p>
<textarea cols= "rows=" >[email protected]@${ntpdate_out}</textarea>
<p> current monitoring IP and restricted network segments </p>
<textarea cols= "rows=" 3 ">[email protected]@${monitip}</textarea>
<p> clear routing for current settings </p>
<textarea cols= "rows=" 3 ">[email protected]@${monitip}</textarea>
</form>
</body>

Cgic implementation of the input file name, the ability to open files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.