⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.c

📁 嵌入式linux的一个cgi网站
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/config.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "cgivars.h"
#include "htmllib.h"
#include "config_phase.h"
#include "sysconf.h"

#define DEBUG        0

#define ID_NTPFLAG          147
#define ID_NTPADD	       148
#define ID_NTPTM             149

static config_item ** items;
static int config_list[] = {147,148,149};
static char *config_name[] = {"NTPFLAG", "NTPADD", "NTPTM"};


void date_set(char **postvars, int form_method)
{
	int config_num = sizeof(config_list)/sizeof(int);
	int i;
	int r_fd, w_fd;

	int temp_int, day, hour, min, sec;
	char *temp_string;
	time_t the_time;
	struct tm *tm_ptr;
	char data_string[MAX_LINE_LENGTH];
	
	char java[]="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<HTML><HEAD>
<META http-equiv=Content-Type content=\"text/html; charset=gb2312\">
<SCRIPT language=JavaScript src=\"js/javascript.js\">
</SCRIPT>

<SCRIPT language=javascript>
<!--  
function manual_time()
{
	parent.frames[\"msgFrame\"].location.replace(\"/action?action=settime&lang=chs&systime=\" + document.getElementById('systime').value)
}

function auto_time()
{	
	parent.frames[\"msgFrame\"].location.replace(\"/action?action=settime&lang=chs&systime=\" + document.getElementById('pcsystime').value)
}
var  timerID  =  null
var  timerRunning  =  false  
function  stopclock(){
if(timerRunning)
clearTimeout(timerID)
timerRunning  =  false
}  
function  startclock(){
stopclock()
showtime()
}  
function  showtime(){
var  now  =  new  Date()
var  years = now.getFullYear()
var  months = now.getMonth() + 1
var  days = now.getDate()
var  hours  =  now.getHours()
var  minutes  =  now.getMinutes()
var  seconds  =  now.getSeconds()
var  timeValue  = "" + years
timeValue  +=  ((months  <  10)  ?  \"-0\"  : \"-\")  +  months
timeValue  +=  ((days  <  10)  ?  \"-0\"  :  \"-\")  +  days
timeValue  +=  \" \"+((hours  <  10)  ?  \"0\"  : \"\")  +  hours
timeValue  +=  ((minutes  <  10)  ?  \":0\"  :  \":\")  +  minutes
timeValue  +=  ((seconds  <  10)  ?  \":0\"  :  \":\")  +  seconds
document.getElementById('pcsystime').value  =  timeValue  
timerID  =  setTimeout(\"showtime()\",1000)
timerRunning  =  true
}

//-->
</SCRIPT>

<STYLE type=text/css>BODY {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
TD {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
FORM {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
OPTION {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
TEXTAREA {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
P {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
OL {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
INPUT {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
SELECT {
	FONT-SIZE: 10pt; FONT-FAMILY: Arial
}
.style1 {
	FONT-WEIGHT: bold; FONT-SIZE: 14pt
}
</STYLE>

<META content=\"MSHTML 6.00.6000.16414\" name=GENERATOR></HEAD>";

	items = init_config_item(items, config_num, config_list);


	printf("%s\n", java);
		//	r_fd = open_read_fifo();

	// If we got post, write it into flash
	if(form_method == POST)
	{
	GetConfValues(config_list, items, config_num);
     SaveConfig(postvars, items, config_list, config_name, config_num);
     /*
		if(SaveConfig(postvars, items, config_list, config_name, config_num))
		{
			w_fd = open_write_fifo();
			notify_config(w_fd);
			close_write_fifo(w_fd);

			read_fifo(r_fd);

		}
     */
		for (i=0; postvars[i]; i+= 2)
		{
			if(!strcmp(postvars[i], "PCTIME"))
			{
				char MM[2],DD[2],hh[2],mm[2],YY[4],ss[2];
				temp_string = strchr(postvars[i+1], '-'); 
				temp_string -= 4;
				strncpy(YY,  temp_string, 4);
				temp_string += 5;
				strncpy(MM, temp_string, 2);
				temp_string += 3;
				strncpy(DD, temp_string, 2);
				temp_string += 3;
				strncpy(hh, temp_string, 2);
				temp_string += 3;
				strncpy(mm, temp_string, 2);
				temp_string += 3;
				strncpy(ss, temp_string, 2);

				memset(data_string, 0, MAX_LINE_LENGTH);
				strcpy(data_string, "date ");
				strncat(data_string,  MM,  2);
				strncat(data_string,  DD,  2);
				strncat(data_string,  hh,  2);
				strncat(data_string,  mm,  2);
				strncat(data_string,  YY,  4);
				strcat(data_string,  ".");
				strncat(data_string,  ss,  2);
			
				if(system(data_string)<0)
				{
					;
				}
			}
		}
	}

	GetConfValues(config_list, items, config_num);

	printf("<BODY leftMargin=30 topMargin=24 onload=startclock() rightMargin=30>\n");
	printf("<P><SPAN class=style1>时间设置</SPAN></P>\n");
	printf("<TABLE style=\"BORDER-COLLAPSE: collapse\" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width=\"100%\" border=1>\n");
	printf("<TBODY><TR bgColor=#cccccc><TD colSpan=3><STRONG>系统时间</STRONG></TD></TR>\n");

	memset(data_string, 0, MAX_LINE_LENGTH);
	time(&the_time);
	tm_ptr = localtime(&the_time);
	sprintf(data_string, "%04d-%02d-%02d %02d:%02d:%02d\n", tm_ptr->tm_year+1900, tm_ptr->tm_mon+1, tm_ptr->tm_mday,
		  tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
	
	printf("<FORM name=settime action=\"/date.cgi\" method=post target=msgFrame>\n");
  	printf("<TR><TD noWrap width=\"32%\">系统时间       (%s)</TD>\n", data_string);
    	printf("<TD><INPUT style=\"WIDTH: 180px\" type=submit value=自动调整为PC的当前时间></TD>\n");
    	printf("<TD width=\"30%\">&nbsp;<INPUT id=pcsystime maxLength=19 size=19 value=\"PC TIME\" name=PCTIME></TD></TR>\n");
  	printf("</FORM>");

	printf("<FORM name=date action=\"/date.cgi\" method=post target=msgFrame>\n");
	printf("<TR bgColor=#cccccc><TD colSpan=3><STRONG>NTP服务器设置</STRONG></TD></TR>\n");

	//NTPFLAG
	printf("<TR><TD noWrap width=\"29%\">NTP是否启用</FONT></TD>\n");
	printf("<TD align=middle colspan=3><DIV align=left><SELECT size=1 name=NTPFLAG>\n");

	if(temp_string = getdata(ID_NTPFLAG, items, config_num))
	{
		temp_int = atoi(temp_string);
		if(temp_int == 0)
		{
			printf("<option value=\"0\" selected>否</OPTION> \n");
			printf("<option value=\"1\">是</OPTION> \n");
		}
		else
		{
			printf("<option value=\"0\">否</OPTION> \n");
			printf("<option value=\"1\" selected>是</OPTION> \n");
		}
	}
	printf("</SELECT></DIV></TD></TR>\n");

	//ID_NTPADD
	printf("<TR><TD noWrap width=\"29%\">NTP服务器地址</FONT></TD>\n");
	if(temp_string = getdata(ID_NTPADD, items, config_num))
		printf("<TD align=middle colspan=3><DIV align=left><INPUT size=15 name=NTPADD value=%s></DIV></TD></TR>\n", temp_string);
	else
		printf("<TD align=middle colspan=3><DIV align=left><INPUT size=15 name=NTPADD></DIV></TD></TR>\n");

	//ID_NTPTM
	printf("<TR><TD noWrap width=\"29%\">时间同步间隔(以小时为单位)</FONT></TD>\n");
	if(temp_string = getdata(ID_NTPTM, items, config_num))
		printf("<TD align=middle colspan=3><DIV align=left><INPUT size=15 name=NTPTM value=%s></DIV></TD></TR>\n", temp_string);
	else
		printf("<TD align=middle colspan=3><DIV align=left><INPUT size=15 name=NTPTM></DIV></TD></TR>\n");	

	printf("<TR><TD colSpan=3><P align=left><INPUT type=submit value=应用><INPUT type=reset value=清除></P>\n");

	printf("</TD></TR></FORM></TBODY></TABLE></BODY></HTML>\n");


	free_config_item(items, config_num);
	//char *loginName = getenv("REQUEST_METHOD");
	//printf("loginName :%s\n",loginName);
	//int  second = time((time_t *)NULL);
	//printf("the time is :%d  \n   ",second);
	//printf("%d\n",getpid());
	//close_read_fifo();
}


////////////////////////////////////////////////////////////////////////////
// Function: main
////////////////////////////////////////////////////////////////////////////
int main(void)
{
    char **postvars = NULL;  // POST request data repository
    char **getvars = NULL;   // GET request data repository
    int form_method;         // POST = 1, GET = 0

    // CGI part start here
    adminCheck();
    form_method = getRequestMethod();

    if(form_method == POST)
    {
       // getvars = getGETvars();
        postvars = getPOSTvars();
    }
    else if(form_method == GET)
    {
        getvars = getGETvars();
    }

    // CGI start here, set COM port
    date_set(postvars, form_method);

    //htmlFooter();
    cleanUp(form_method, getvars, postvars);


    fflush(stdout);
    exit(0);

}  // end of main


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -